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
...