# Flow control

Andrew Parker
Ranch Hand
Posts: 178
Hi,
I have 2 codes that don't understand:
public class flow
{
public static void main(String[] args)
{
outer: for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
if(i==j)
{
continue outer;
}
System.out.println("i = " + i + " j = " + j + "\n" );
}
}
}
}
Why the outcome is i=1 j=0?
Also,
public class flow
{
public static void main(String[] args)
{
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
if(i==j)
{
continue;
}
System.out.println("i = " + i + " j = " + j + "\n" );
}
}
}
}
Why the results are
i=0 j=1
j=1 j=0
i=0 j=2
i=1 j=2
if (i==j) then continue.
So, i != j then println all combinations??
Thanks for help.
Andrew

Art Metzer
Ranch Hand
Posts: 241

Hi, Andrew.
Here are the steps the code in your first example goes through:
1. You enter the "outer" loop in line 5, and go on to line 6.
2. i initializes to 0 in line 6.
3. j initializes to 0 in line 8.
4. Java executes the test in the "if" statement on line 10. Since i does equal j, we continue outer, which advances i from 0 to 1 at line 6.
5. At line 8, we're starting over in a new j loop, so j is once again initialized to 0.
6. The test at line 10 fails, so we don't continue this time. Instead, we print output: i = 1, j = 0.
7. Since we're at the end of the inner for loop (j) and we've no reason to leave it, j advances in line 8 from 0 to 1.
8. i still equals 1, and j equals 1. The test in line 10 evaluates to TRUE, so we once again continue outer, which advances i from 1 to 2 on line 6.
9. Since the condition i > 2 evaluates fall, the outer for loop, and the program, terminates.

Looking over what's been printed to the screen, Andrew, only one line has been: i = 1, j = 0.
You can follow similar step-by-step logic to analyze the second example, keeping in mind that "continue" without the label refers to the innermost loop.
Good luck,
Art

Andrew Parker
Ranch Hand
Posts: 178
Hi Art,
Your explanation is deeply clear. But, I don't understand one point for the second code without the outer label:
1. i initializes to 0.
2. j initializes to 0.
3. Java executes the test in the "if" statement. Since i does equal j, it continues and should println i = 0 and j = 0.
4. A new j loop, turns j from 0 to 1. As i != j, it should not print it.
5. The next j loop again until j<3, it turns j from 1 to 2 and they are not equal. So, it will not be printed.
6. i advances to 1, it prints i = 1, j = 1.
7. terminates.
So, the result is
i=0 j=0
i=1 j=1
But, why is it the opposite?
Thanks
Andrew

Colin Kenworthy
Ranch Hand
Posts: 88
Continue doesn't mean "continue with the next statement", in this case it means go back to the for loop end execute the next iteration through it. So basically you only print something if i != j. If i == j you repeat the inner loop.
It's as if you had labelled the innermost for loop 'inner:' and your statement was 'continue inner;'.

Andrew Parker
Ranch Hand
Posts: 178
Thanks for your great help. I understand it now.
"continue inner" - good explanation.
Regards
Andrew