在 while 循环中的 continue 语句之前使用递增运算符有什么区别? (JavaScript)
What difference does it make to use the increment operator before the continue statement in a while loop? (JavaScript)
我正在尝试编写有关在 while 循环中使用“continue”语句的教程。在教程中,代码是这样写的,效果很好。
...var x = 1;
document.write("Entering loop");
while (x < 20) {
x++;
if (x == 5) {
continue;
}
document.write(x + "<br />");
}
document.write("Exiting the loop");...
但我尝试了不同的方法,当我将增量语句放在“if”块之后时,它导致了无限循环,如下所示。
...
var x = 1;
document.write("Entering loop");
while (x < 20) {
if (x == 5) {
continue;
}
x++;
document.write(x + "<br />");
}
document.write("Exiting the loop");
...
我已经想了想,但还是想不通。为什么会这样?
if (x == 5) {
continue;
}
单独意味着x一旦达到5就永远不会改变。把x++放在这之前意味着x会改变。
在 x++ 之后,循环将 continue
每次无限循环。
我正在尝试编写有关在 while 循环中使用“continue”语句的教程。在教程中,代码是这样写的,效果很好。
...var x = 1;
document.write("Entering loop");
while (x < 20) {
x++;
if (x == 5) {
continue;
}
document.write(x + "<br />");
}
document.write("Exiting the loop");...
但我尝试了不同的方法,当我将增量语句放在“if”块之后时,它导致了无限循环,如下所示。
...
var x = 1;
document.write("Entering loop");
while (x < 20) {
if (x == 5) {
continue;
}
x++;
document.write(x + "<br />");
}
document.write("Exiting the loop");
...
我已经想了想,但还是想不通。为什么会这样?
if (x == 5) {
continue;
}
单独意味着x一旦达到5就永远不会改变。把x++放在这之前意味着x会改变。
在 x++ 之后,循环将 continue
每次无限循环。