• 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

Label association doubt

 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Labeled continue and break statements must be inside the loop that
has the same label name; otherwise, the code will not compile.
the above statment is copied for K&B.

public class Test {
public static void main(String[] args) {
label1:{
if(1 > 0){
break label1;
}
System.out.println("main...");
}
}
}
this code works fine.
in the above sample,label2 is not associated with loop.
Please explain how it is compiling.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do K and B say that labels are used only for loops? For a label used for a loop what they say is true. But for a general labeled statement you can certainly use a labeled break to break out from that labeled statement.

By the way, what label2 are you meaning?

If you read the beginning of the Labeled Statements section once more, then you will see that they are restricting the scope of the discussion to labeled loops.
[ February 05, 2007: Message edited by: Barry Gaunt ]
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sentil, I remember that you can take the rule as:

continue with or without label must be used inside loop statement.

break with or without label must be used inside loop + switch statement.
break with label can also be used in if statement.

(loop = for, do-while, while)

hope it helps, and welcome to comment if it's wrong.
 
reply
    Bookmark Topic Watch Topic
  • New Topic