This week's book giveaway is in the Design forum.
We're giving away four copies of Building Microservices and have Sam Newman on-line!
See this thread for details.
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 Building Microservices this week in the Design 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’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com
 
subject: For Stament with continue
 
It's not a secret anymore!