[英]JavaScript switch with logical operators?
for (var count = 1; count < 6; count++) {
switch (count) {
case (2): document.write("hi"); break;
case (count > 3): document.write("bye"); break;
case (count >= 4): document.write("lol"); break;
}
}
Because it's not working the way I expect, not printing bye and lol, it makes me think this is invalid in JavaScript. 因为它没有按照我期望的方式工作,而不是打印再见和大声笑,这让我觉得这在JavaScript中是无效的。 I tried looking for some examples to see if people do this on Google, and I saw none.
我试着寻找一些例子来看看人们是否在Google上这样做,我没有看到。 So is this valid or not?
这是否有效? or Why might this not work?
或者为什么这不起作用?
When switch
is interpreted, the expression in the parentheses is compared to values of the particular cases. 解释
switch
,将括号中的表达式与特定情况的值进行比较。
So in your case the value of count
would be compared to the values of 2
, count > 3
and count >= 4
. 因此,在您的情况下,
count
的值将与2
的值进行比较, count > 3
且count >= 4
。 And that won't work. 那是行不通的。 Although you can rewrite it and compare to
true
to get it working: 虽然你可以重写它并将其与
true
进行比较以使其正常工作:
switch (true) {
case (count == 2):
document.write("hi");
break;
case (count > 3):
document.write("bye");
break;
case (count >= 4):
document.write("lol");
break;
}
But that's not how switch
is supposed to be used. 但这并不是应该如何使用
switch
。
Use if
statements instead: 改为使用
if
语句:
if (count == 2) {
document.write("hi");
} else if (count > 3) {
document.write("bye");
} else if (count >= 4) {
document.write("lol");
}
Edit Since you use the switch
cases exclusively (break if a case matches), my switch
-to- if
/ else
translation is correct. 编辑由于您只使用
switch
案例(如果案例匹配则中断),我的switch
到if
/ else
转换是正确的。
But the count >= 4
case/branch will never be applied since count > 3
is true (also) for count
values greater or equal 4. 但是
count >= 4
case / branch永远不会被应用,因为count > 3
对于大于或等于4的count
数值是真的(也是)。
To fix this problem (write “bye” and “lol” for values greater or equal 4), remove the last else
to make the last if
statement independent from the preceding: 要解决此问题(对大于或等于4的值写“bye” 和 “lol”),删除最后一个
else
以使最后一个if
语句独立于前面的:
if (count == 2) {
document.write("hi");
} else if (count > 3) {
document.write("bye");
}
if (count >= 4) {
document.write("lol");
}
This is a correction to Gumbo's answer . 这是对Gumbo答案的修正。 I'm writing a separate answer only because this won't fit as a comment.
我正在写一个单独的答案,因为这不适合作为评论。
Edit: Gumbo suggested in a comment that I may have misread Doug's intention. 编辑: Gumbo在评论中建议我可能误读了Doug的意图。 If the OP really wants both "bye" and "lol" to be printed out for count >= 4, then we need to remove a
break
from the switch
. 如果OP真的希望打印出“bye”和“lol”for count> = 4,那么我们需要从
switch
删除一个break
。 The cases are now back in the original order, so that "bye" and "lol" are printed in that order (which is apparently the OP's intent.) 这些案件现在按照原始顺序返回,因此“bye”和“lol”按此顺序打印(这显然是OP的意图。)
switch (true) {
case (count == 2):
document.write("hi");
break;
case (count > 3):
document.write("bye");
// No break here; just fall through.
case (count >= 4):
document.write("lol");
break;
}
In this case, I agree with Gumbo that the revised if
statement is correct. 在这种情况下,我同意Gumbo修改后的
if
语句是正确的。
Original answer follows (assumes that the OP really wanted either "lol" or "bye" to print, but not both.) 原来的答案如下(假设OP真的想无论是 “笑”或“再见”来打印,但不能同时使用。)
The switch
statement that Gumbo wrote won't work for count >= 4, for much the same reason that Gumbo's original if
statement won't work: Because the cases are evaluated in sequence, count >= 4 implies that the second case (count > 3) will be executed; Gumbo写的
switch
语句对count> = 4不起作用,原因与Gumbo的原始if
语句不起作用的原因相同:因为案例是按顺序计算的,count> = 4意味着第二种情况(count > 3)将被执行; so the script will never reach the test for count >= 4. To fix this, the tests should be executed in the reverse order, from highest to lowest: 所以脚本永远不会达到count> = 4的测试。为了解决这个问题,测试应该以相反的顺序执行,从最高到最低:
switch (true) {
case (count >= 4):
document.write("lol");
break;
case (count > 3):
document.write("bye");
break;
case (count == 2):
document.write("hi");
break;
}
The corrected if
statement is still not right either, because for count >= 4 it will produce both bye
and lol
on the output. 校正
if
声明仍然没有任何的权利,因为计数> = 4会产生两种 bye
,并lol
对输出。 Again, the tests within the if
ladder should be arranged to go from highest to lowest values: 同样,
if
梯形图中的测试应该安排从最高值到最低值:
if (count >= 4) {
document.write("lol");
} else if (count > 3) {
document.write("bye");
} else if (count == 2) {
document.write("hi");
}
This isn't an ideal example, because if count
is an integer, then evaluating count >= 4
and count > 3
will produce the same results -- true
for count >= 4, false
otherwise. 这不是一个理想的例子,因为如果
count
是一个整数,那么count >= 4
和count > 3
将产生相同的结果 - 对于count> = 4则为true
,否则为false
。 That wouldn't be the case if count
is a floating-point value (but then, a floating-point value named "count" would raise other concerns.) 如果
count
是一个浮点值,那就不是这种情况(但是,一个名为“count”的浮点值会引起其他问题。)
You use the case clause in the wrong way. 您以错误的方式使用case子句。 You should provide a value that will be compared to the value in the switch clause ... and not a boolean expression like this
count>2
您应该提供一个值,该值将与switch子句中的值进行比较...而不是像此
count>2
这样的布尔表达式
In this case this boolean expression will be cast to true or false (1 or 0) and compared to your value count and sometimes may work, sometimes - not. 在这种情况下,这个布尔表达式将被强制转换为true或false(1或0)并与您的值计数进行比较,有时可能有效 - 有时 - 不是。
You should consider replacing it with if statements. 您应该考虑将其替换为if语句。
您应该更换最后两个案例。
The switch
normally needs a fixed condition/value; switch
通常需要固定的条件/值; because your count
variable changes every time, it goes against that. 因为你的
count
变量每次都在变化,所以它与之相反。 Use if-else
condition instead. 请改用
if-else
条件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.