• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Flow Control

 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Arun Pai
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Jon
oops... used String as keyword.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic