Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

For Stament with continue

 
sumaraghavi ragha
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi This is code snippet form K&B page no 343.Plese explain me why the outer is not printing hear???


outer:
for (int i=0; i<5; i++) {
for (int j=0; j<5; j++) {
System.out.println("Hello");
continue outer;
} // end of inner loop
System.out.println("outer"); // Never prints
}
System.out.println("Good-Bye");

output is
Hello
Hello
Hello
Hello
Hello
Good-Bye

Thnaks in advance
 
Ashish Hareet
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the jls - "A continue statement with label Identifier attempts to transfer control to the enclosing labeled statement (�14.7) that has the same Identifier as its label; that statement, which is called the continue target, then immediately ends the current iteration and begins a new one"
- http://java.sun.com/docs/books/jls/third_edition/html/statements.html#14.16

Based on the above statement, in your code example, every time "continue outer" is called, the control is transferred to the for statement with the label "outer" to start the next iteration. The code never reaches the print statement.

Try the same example with "continue;" instead of "continue outer;"

HTH
Ashish Hareet
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic