-
use Continue with label
The prog given in below prog is very very simple but i am having hard time understanding the sequence of flow of control plus increment of i & j at different stage. Can some1 take time explain it with differnt incrementing stage of i & j.
// Using continue with a label.
Code:
class ContinueLabel
{
public static void main(String args[])
{
outer: for (int i=0; i<10; i++)
{
for(int j=0; j<10; j++)
{
if(j > i)
{
System.out.println();
continue outer;
}
System.out.print(" " + (i * j));
}
}
System.out.println();
}
}
the output is:
0
0 1
0 2 4
0 3 6 9
0 4 8 12 16
0 5 10 15 20 25
0 6 12 18 24 30 36
0 7 14 21 28 35 42 49
0 8 16 24 32 40 48 56 64
0 9 18 27 36 45 54 63 72 81
-
You can read 'continue outer' as 'stop doing everything you're doing and continue the next loop iteration pointed to by the "outer" label'. So here the inner loop stops every time j > i and the whole thing continues from the top with the next value for variable i.
kind regards,
Jos
-
In other words, the continue statement skips the current iteration of loop, such as for, while , or do-while loop.
In outer for loop, variable i initialize to zero. And then enter to inner for loop, initialize variable j to zero there as well.
Then it checks the conditions j > i. Since the condition fail, following print statement execute, and print the value zero. After that j increment by one. Now it's two. Check the condition and success. Means that body of the if is execute. Print a new line to the console. Then mover to continue outer; which instruct to VM to stop the current iteration and move to the place which you define as a label, outer
From there start the execution again, initialize values from zero and so on as above.
Hope it's helpful to you.