英文:
Why is it that the output of this code is 234 and not 23?
问题
所以我可能对while循环的工作原理没有很清楚的理解。但我预期这段代码的输出会是23,而不是234。我认为语句x++在打印x之前,会使得循环在x变为4时立即结束,但事实并非如此,它只是继续执行,通过打印x(x变为4),然后结束。
我猜这些只是Java的规则,但这似乎有点违反直觉。也许应该使用break语句?
void printNums(){
int x = 1;
int y = 0;
while(x != 4){
while(y < 0){
System.out.print(y);
y++;
}
x++;
System.out.print(x);
}
System.out.print(" ");
}
英文:
So I may not have a very clear understanding of how while loops work. But I expected the output of this code would be 23 instead of 234. I thought that the statement x++; being before the print of x would make it so that the loop would end right when x becomes 4, but it doesn't it just keeps going, executes by printing x which becomes 4 and then it ends.
I guess these are just the rules of Java but it seems sort of counter-intuitive. Maybe a break statement would be in order?
void printNums(){
int x = 1;
int y = 0;
while(x != 4){
while(y < 0){
System.out.print(y);
y++;
}
x++;
System.out.print(x);
}
System.out.print(" ")
}
答案1
得分: 3
> 我认为语句 x++; 在打印 x 之前,会使得循环在 x 变为 4 的时候立即结束。
这是你的误解。循环的整个主体在每次迭代中始终都会运行(除非你在循环内部使用 break 或 return,或以其他方式退出循环)。
while (x != 4) {
...
x++; -- Java 不会“监视” x。如果 x 变为 4,下一条语句仍然会运行
System.out.print(x);
}
循环条件(x != 4
)在每次迭代开始时执行。只有当整个循环主体被执行时,条件才会被再次评估,以检查是否应该再次运行。x
不会被“监视”,以至于每当它变为 4 时,就会自动执行 break;
。如果你需要这样的效果,你应该明确地自己结束循环。
英文:
> I thought that the statement x++; being before the print of x would make it so that the loop would end right when x becomes 4
This is what you misunderstand. The entire body of the loop always runs in each iteration (unless you break or return out of it, or exit it a different way).
while (x != 4) {
...
x++; <-- Java doesn't "watch" x. If x becomes 4, next statement will still run
System.out.print(x);
}
The loop condition (x != 4
) is executed at the beginning of each iteration. It's only when the entire body of the loop is executed that the condition is evaluated again to check whether it should be run once again. x
is not being "watched" such that whenever it becomes 4, an automatic break;
is executed. If you need that, you should explicitly end the loop yourself.
答案2
得分: 0
循环条件不会被持续地检查。它只在每次新的迭代即将开始时进行检查。
当 x
为 3 且执行了 x++
后,x
确实变为了 4,但循环条件并不会立即被检查。循环的其余部分(System.out.println
那一行)仍然会被执行。只有在当前迭代完成(到达 }
),并且新的迭代即将开始时,循环条件才会被检查。在那个时候,发现 x
是 4,循环停止。
如果你希望每次改变 x
时都检查循环条件,你需要手动编写检查:
while(x != 4){
x++;
if (x == 4) break;
System.out.print(x);
}
System.out.print(" ");
请注意,y
永远不会小于 0,因此内部的 while 循环永远不会被执行。
英文:
The loop condition is not constantly checked. It is only checked every time a new iteration is about to start.
When x
is 3, and x++
is executed, x
does become 4, but the loop condition is not immediately checked. The rest of the loop (the System.out.println
line) will still be executed. It is only when the current iteration finishes (}
is reached), and a new iteration is about to start, that the loop condition is checked. And at that point, x
is found to be 4, and the loop stops.
If you want the loop condition to be checked every time you change x
, you need to write the checks manually:
while(x != 4){
x++;
if (x == 4) break;
System.out.print(x);
}
System.out.print(" ");
Note that y
is never less than 0, so the inner while loop is never executed.
答案3
得分: 0
在进入for循环之前
x = 1
y = 0
第一次迭代 -
y = 0,因此不会进入内部while循环
x++
x = 2,因此打印2
第二次迭代 -
x = 2
y = 0
y = 0,因此不会进入内部while循环
x++
x = 3,因此打印3
第三次迭代 -
x = 3
y = 0
y = 0,因此不会进入内部while循环
x++
x = 4,因此打印4
但现在x = 4,意味着4!= 4为假,因此外部循环结束...
因此在控制台中,您将看到输出 2 3 4。
英文:
Before going to for loop
x =1
y=0
Fist iteration -
y= 0 , so never go to internal while loop
x++
x=2 , so print 2
second iteration -
x =2
y=0
y= 0 , so never go to internal while loop
x++
x=3 , so print 3
Third iteration -
x =3
y=0
y= 0 , so never go to internal while loop
x++
x=4 , so print 4
but now x=4 , means 4!=4 false, so outer loop ends...
hence in the console , u will see output 2 3 4.
答案4
得分: 0
-
第一次迭代中,x = 1,因为y = 0,所以while循环(y < 0)从未执行。然后x++增加了x的值,现在x等于2。
-
第二次迭代中,x = 2,与4不同,所以while循环(y < 0)再次被忽略。然后x++增加了x的值,现在x等于3。
-
第三次迭代中,x = 3,与4不同,所以while循环(y < 0)再次被忽略。然后x++增加了x的值,现在x等于4。
然后,由于x = 4,while循环(x != 4)的条件计算为假,循环停止。
因此,你得到234。
英文:
-
In the first iteration x = 1, since y = 0 the while loop for (y <0) is never executed. Then x++ increments the value of x and now x is equal to 2.
-
In the second iteration x = 2 which is different than 4, the while loop for (y<0) is ignored again. Then x++ increments the value of x and now x is equal to 3.
-
In the third iteration x = 3 which is different than 4, the while loop for (y<0) is ignored again. Then x++ increments the value of x and now x is equal to 4.
Then since x = 4 the while loop (x!=4) evaluates to false and the loop stops.
Therefore you get 234.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论