这是我无法理解的行为.
class loop1 {
public static void main(String args[]) {
int i = 10;
do
while(i++ < 15) {
System.out.println(i);
i = i + 20;
System.out.println(i);
}
while(i<2);
System.out.println(i);
}
}
Run Code Online (Sandbox Code Playgroud)
我希望它能打印出来
11
31
31
Run Code Online (Sandbox Code Playgroud)
但它打印
11
31
32
Run Code Online (Sandbox Code Playgroud)
我无法理解为什么这个"32"出现在输出中.
这是我对流程的理解
有人能告诉我我错过了什么吗?
在第一次while循环的最终评估期间,即使由于条件失败而未执行循环,i++仍然会增加i循环.
class loop1 {
public static void main(String args[]) {
//1. i = 10
int i = 10;
do
// 2. while loop condition = (10 < 15), i = 11
// 6. while loop condition = (31 < 15), i = 32
while(i++ < 15) {
System.out.println(i); //3. prints 11
i = i + 20; //4. i = 31
System.out.println(i); //5. prints 31
}
while(i<2); //this really has no effect on the codes execution, given i values
System.out.println(i); //7. Prints 32
}
}
Run Code Online (Sandbox Code Playgroud)