简体   繁体   English

为什么Java在break语句后不重置内部循环的值?

[英]Why Java doesn't reset the values of the inner loop after a break statement?

I'm currently struggling understanding this loop: 我目前正在努力理解此循环:

class Test{
    public static void main(String args[]){
        int i=0, j=0;
        X1: for(i = 0; i < 3; i++){
            X2: for(j = 3; j > 0; j--){
                if(i < j) continue X1;
                else break X2;
            }
        }
        System.out.println(i+" "+j);
    }
}

So far I know that the values of the variable will be: 到目前为止,我知道变量的值将是:

 0 3 
 1 3 
 2 3 

and finally will print 3 3 . 最后将打印3 3

After the third iteration the condition on X1 will be false resulting in an interruption of the loop statement. 第三次迭代后, X1上的条件将为false,从而导致循环语句中断。 While it's clear to me why the value of i is equal to 3, I do not understand why the value of j is 3 as well. 虽然我很清楚为什么i的值等于3,但我不明白为什么j的值也等于3。 Initially the value of j is 0 , when we enter in the loop is 3 , but in the last iteration we do not enter really in the X2 loop, since i<3 evaluate false. 最初,j的值为0 ,当我们进入循环时为3 ,但是在最后一次迭代中,由于i<3假,所以我们并未真正进入X2循环。 So the question is why the compiler "save" the value of k ? 所以问题是为什么编译器“保存” k的值? And even if the compiler save the value of j from the previous iteration should be 2 ... 即使编译器保存了前一次迭代的j值,也应为2 ...

j-- is dead code here and will never be reached. j--在这里是无效代码,将永远无法到达。 Think about how the code works for a moment here: 在这里考虑一下代码如何工作:

X2: for(j = 3; j > 0; j--){
    if(i < j) continue X1;
    else break X2;
}

If one situation you continue to the outer loop, in the other situation you break out of this loop. 如果一种情况您继续进入外部循环,则在另一种情况下您会退出该循环。 This loop actually never even goes past a single iteration so you might as well just write this like this: 实际上,该循环甚至从未经过一次迭代,因此您最好像这样编写:

int i=0, j=0;
X1: for(i = 0; i < 3; i++){
    j = 3;
    if(i < j) continue X1;  //This line does nothing at this point as well since the loop will iterate anyway
}

This is exactly the same as your current code, which clearly shows j will stay at 3 . 这与您当前的代码完全相同,其中清楚地显示j将保持在3

for(j = 3; j > 0; j--)
You are setting j=3 . 您正在设置j=3 j-- is not run until the next j loop, that never occurs, so it cannot be 2. j--直到下一个j循环才运行,该循环永远不会发生,因此不能为2。

else break X2;

and

j--

are never being reached. 永远不会达到。

'i' can never be 3 within the loop since the outer loop's condition is i < 3, and therefor the inner loop can only perform “ i”在循环内永远不能为3,因为外部循环的条件是i <3,因此内部循环只能执行

if(i < j) continue X1;

since 'j' always starts at 3 and i <= 2. is always true. 因为'j'总是从3开始且i <= 2始终为真。 So 'j' never changes value, and the outer loop breaks when i = 3, resulting in, "3 3". 因此,“ j”永远不会改变值,并且当i = 3时外部循环会中断,从而导致“ 3 3”。

i j
0 3
1 3
2 3
break occurs;
print i + j;

Initially the value of j is 0, when we enter in the loop is 3, but in the last iteration we do not enter really in the X2 loop, since i<3 evaluate false. 最初,j的值为0,当我们进入循环时为3,但是在最后一次迭代中,由于i <3为假,因此我们并未真正进入X2循环。 So the question is why the compiler "save" the value of k ? 所以问题是为什么编译器“保存” k的值?

j is declared at the first line in main. j在main的第一行声明。 This means that it will remain in scope and retain any modifications until main ends and the variable is destroyed. 这意味着它将保留在范围内,并保留所有修改,直到主要目的和变量被破坏为止。

And even if the compiler save the value of j from the previous iteration should be 2. 即使编译器保存了前一次迭代的j值,也应为2。

As you said above, the value of j from the last iteration of the loop was 3 not 2. When you continue X1 the j-- was never executed. 如上所述,循环最后一次迭代的j值为3而不是2。当您继续执行X1时,从未执行过j--。

It is because of the dead code as others mentioned. 这是因为其他人提到的无效代码。 You should debug your program by going step by step I don't know which IDE you are using but it probably provides this feature. 您应该逐步进行调试程序,我不知道您使用的是哪个IDE,但它可能提供了此功能。

However, I want to advice you to not use continue and break statements. 但是,我想建议您不要使用继续和中断语句。 It is highly discouraged by the instructors. 辅导员强烈建议不要这样做。 They cause spaghetti programming and confusions like you have. 它们会导致像您一样的意大利面条编程和混乱。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM