简体   繁体   中英

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 .

After the third iteration the condition on X1 will be false resulting in an interruption of the loop statement. 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. 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. So the question is why the compiler "save" the value of k ? And even if the compiler save the value of j from the previous iteration should be 2 ...

j-- is dead code here and will never be reached. 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 .

for(j = 3; j > 0; j--)
You are setting j=3 . j-- is not run until the next j loop, that never occurs, so it cannot be 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

if(i < j) continue X1;

since 'j' always starts at 3 and i <= 2. is always true. So 'j' never changes value, and the outer loop breaks when i = 3, resulting in, "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. So the question is why the compiler "save" the value of k ?

j is declared at the first line in 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.

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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