This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes doubt on continue loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "doubt on continue loop " Watch "doubt on continue loop " New topic

doubt on continue loop

R .sourav nayak
Ranch Hand

Joined: May 14, 2006
Posts: 67

could someone explain me how will be the out put 3 4 5 from the following code

1. int i, j;
2. lab: for( i = 0; i < 6; i++ ){
3. for( j = 5; j > 2; j-- ){
4. if( i == j ) {
5. System.out.print(" " + j ) ;
6. continue lab ;
7. }
8. }
9. }

i think, the statement at line 5 never there is no output.

thanks in advance,
Tim LeMaster
Ranch Hand

Joined: Aug 31, 2006
Posts: 226
Just step through it

1st through outer loop i=0
inner loop tests i == j for j = 5 4 3

2nd through outer loop i=1
inner loop tests i == j for j = 5 4 3


All the continue does is once a match is found jumps to the next interation of the outer loop. The label is needed to keep continue from just acting upon the inner loop.

Bonus point - whats the output if the continue is removed?
[ September 19, 2006: Message edited by: Tim LeMaster ]
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023

it will reach only when i = 3, 4, 5. Add one println statement and you will find out why.
R .sourav nayak
Ranch Hand

Joined: May 14, 2006
Posts: 67
thank you very much Tim and wise for the immediate replay
Don't get me started about those stupid light bulbs.
subject: doubt on continue loop
Similar Threads
for loop!
Please solve this program and help me out clearly
For Loop
Cave In