0

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

lczapski
  • 4,026
  • 3
  • 16
  • 32
  • "*I do not understand why the value of j is 3 as well.*" - You initialized it with `3` in the head of the loop. When the inner loop ends, `j` goes out of scope. If the inner loop is entered again later on, a new `j` is created. – Turing85 Jul 31 '19 at 19:03
  • 2
    Step through the code step by step in a debugger to see exactly how it goes, it will show it the best to you – Sami Kuhmonen Jul 31 '19 at 19:04
  • Possible duplicate of [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Turing85 Jul 31 '19 at 19:05
  • Also, please read [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – Turing85 Jul 31 '19 at 19:06
  • You dont even need a debugger. Take a pen and a piece of paper, and run your algorithm yourself. That is how you "debug" problems that are so small (that you can easily run them manually). – GhostCat Jul 31 '19 at 19:08
  • The question is pure theoretical, I'm studying for the OCA, the point here is understand the code, not starting a debate about optimisation or unreachable lines. Thank's to everybody. – Penny Tration Jul 31 '19 at 22:22

5 Answers5

3

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.

Nexevis
  • 4,647
  • 3
  • 13
  • 22
  • No point in writing `if (condition) continue X1;` as the last statement in the X1 loop: it will do exactly the same without. – Andy Turner Jul 31 '19 at 19:14
  • @Andy Turner Yeah I left it in to try and keep it similar with the `X1` so he can see the important changes since you can really reduce it all the way to `int i = 3, j = 3;`. But I'll make a change for it. – Nexevis Jul 31 '19 at 19:17
0

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.

gpunto
  • 2,573
  • 1
  • 14
  • 17
0
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;
omoshiroiii
  • 643
  • 5
  • 11
0

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.

iansumm
  • 31
  • 4
0

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.

wolfenblut
  • 131
  • 1
  • 11
  • Thank you, I'm sorry but I've to disagree. In some context breaching statements are needful, and by the way I'm studying for the OCA, I'm asking cause I have to clarify each shady doubts I might have. – Penny Tration Jul 31 '19 at 22:20
  • Actually, no. You don't need them: -You can add another condition to your loop instead of break and -You can add an if/else block instead of continue in your loop. – wolfenblut Aug 01 '19 at 16:42