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 Flow Control 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 "Flow Control" Watch "Flow Control" New topic
Author

Flow Control

Arun Pai
Ranch Hand

Joined: Mar 11, 2002
Posts: 143
public class TestClass
{
public static void main(String[] args)
{
String: for(int i = 0; i< 10; i++)
{
for (int j = 0; j< 10; j++)
{
if ( i+ j > 10 ) break String;
}
System.out.println( "hello");
}
}
}
I have 2 questions for this code from JQ+?
1. Why should this code compile if a keyword String is used a label identifier in break.
2. Why the output should print hello twice.


-Arun
Jon Huhtala
Greenhorn

Joined: Apr 10, 2002
Posts: 17
String is a class name and not a keyword. It is OK, but poor coding technique, to use it as a label.
Note that the inner loop does no printing. In fact it prevents the outer loop from printing when i+j is greater than 10 because it breaks to the next iteration of the outer loop. Printing only happens when i is 0 or 1 because j takes on values as high as 9 and forces the break.


- Jon Huhtala, SCPJ2<p> "Nothing worthwhile is ever easy..."
Arun Pai
Ranch Hand

Joined: Mar 11, 2002
Posts: 143
Thanks, Jon
oops... used String as keyword.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Flow Control
 
Similar Threads
Question from jqplus
loop
Flow Control -JQ+
Some problem
I am not getting the required output...