wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes For Stament with continue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "For Stament with continue" Watch "For Stament with continue" New topic
Author

For Stament with continue

sumaraghavi ragha
Ranch Hand

Joined: Nov 17, 2006
Posts: 118
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

Joined: Jul 14, 2001
Posts: 375
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: For Stament with continue
 
Similar Threads
Label (Continue)
unreachable code-K&B p343
labeled continue statement?
Nested for loops
continue outer loop