Results 1 to 3 of 3
Thread: use Continue with label
- 04-03-2010, 05:19 PM #1
Member
- Join Date
- Mar 2010
- Posts
- 9
- Rep Power
- 0
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.
the output is:Java 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(); } }
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 81Last edited by Eranga; 04-04-2010 at 01:15 PM. Reason: added code tags
- 04-03-2010, 05:56 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
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
- 04-04-2010, 01:24 PM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
Similar Threads
-
Continue or not?
By kliane in forum New To JavaReplies: 3Last Post: 01-19-2010, 02:42 PM -
Program doesn't continue...
By Reiyn in forum New To JavaReplies: 7Last Post: 10-07-2008, 10:28 PM -
How to use Continue with a label
By Java Tip in forum java.langReplies: 0Last Post: 04-17-2008, 07:46 PM -
How to use Continue
By Java Tip in forum java.langReplies: 0Last Post: 04-17-2008, 07:46 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks