[英]How to make an else if and else in this If Statement
I have this if statement below.我在下面有这个 if 语句。 It is possible to add / make it in
if - else if - else
?.可以在
if - else if - else
? 中添加/制作它。 If not tell me the reason.如果没有告诉我原因。 Thanks.
谢谢。 Cheers
干杯
var num1; var num2 = 1; num1 = num2 != 0 ? num2 : ""; alert(num1);
Sure but our (or at least my) human brain doesn't like reading it ;-)当然,但我们的(或至少是我的)人脑不喜欢阅读它;-)
num1 = num2 != 0
? num2
: num3 != 0
? num3
: num4 != 0
? num4
: num5 != 0
? num5
: ""
spacing is not required just makes it more readable.不需要间距只是使其更具可读性。
If all you want is to translate the ternary operator (the ? :
) into an if else
then here goes:如果您只想将三元运算符(
? :
)转换为if else
那么这里是:
var num1; var num2 = 1; if (num2 != 0) { num1 = num2; } else { num1 = ""; } alert(num1);
You're describing the ternary operator, and it's pretty much你在描述三元运算符,它几乎是
if(part before ?) {
(part before colon)
} else {
(part after colon)
}
Like so:像这样:
if( num2 != 0 ) {
num1 = num2;
} else {
num1 = "";
}
In general, if you ever have anything more complicated than [a = (b==x) ?一般来说,如果你有比 [a = (b==x) 更复杂的东西? a : b], use an if block instead or risk pissing off your fellow programmers :-).
a : b],请改用 if 块,否则可能会激怒您的程序员同事 :-)。 Also use parens a lot to disambiguate, as order of operations can sometimes get weird...
还经常使用括号来消除歧义,因为操作顺序有时会变得奇怪......
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.