File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can label be placed before switch?

 
Gary Kevin
Ranch Hand
Posts: 43
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
K&B book:
"it's most common to use labels with loop statements like for or while,in conjunction with break and continue statements.A label statement must be placed just before the statement being labeled,and it consists of a valid identifier that ends with a colon( "



This code compiles and runs well.Can label be placed before switch?
 
Campbell Ritchie
Sheriff
Pie
Posts: 47292
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A label is not executable code, so you can put labels in all sorts of places.

Why you should want a label, now that's a different matter, unless it's "I just wanted to see what would happen," which is an adequate justification for anything, never mind how daft.
 
Gary Kevin
Ranch Hand
Posts: 43
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Campbell Ritchie:
A label is not executable code, so you can put labels in all sorts of places.

Why you should want a label, now that's a different matter, unless it's "I just wanted to see what would happen," which is an adequate justification for anything, never mind how daft.


I don't think so,label can't be put any sorts of places:

it compiles error.label can only be put before for or while.
This is a SCJP question,so I'm confused about that.
 
Ivan Ivanic
Ranch Hand
Posts: 100
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can put label before any statement or block of code.
but you cannot call break labelX if you are not in its scope.
you are trying to call break label1 in switch case 3 but it is out of scope.
look this code about scopes:
 
Gary Kevin
Ranch Hand
Posts: 43
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ivan Ivanic:
you can put label before any statement or block of code.
but you cannot call break labelX if you are not in its scope.
you are trying to call break label1 in switch case 3 but it is out of scope.
look this code about scopes:


Ivan: is this means that if i put label just before for or while like

this label1 will implicitly add block?
 
Ivan Ivanic
Ranch Hand
Posts: 100
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well I guess you could say it like that. but that is the point.
 
Gary Kevin
Ranch Hand
Posts: 43
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thinks all!
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic