File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Can label be placed before switch? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Can label be placed before switch?" Watch "Can label be placed before switch?" New topic
Author

Can label be placed before switch?

Gary Kevin
Ranch Hand

Joined: Jul 24, 2006
Posts: 43
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?


SCJP 5.0 72%
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36453
    
  15
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

Joined: Jul 24, 2006
Posts: 43
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

Joined: Oct 31, 2007
Posts: 100
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:


<a href="http://faq.javaranch.com/java/UseRealWords" target="_blank" rel="nofollow">Use Real Words</a> <a href="http://faq.javaranch.com/java/UseCodeTags" target="_blank" rel="nofollow">!!!Use Code Tags!!!</a> <a href="http://faq.javaranch.com/java/SayThanks" target="_blank" rel="nofollow">Say Thanks</a><br />scjp6
Gary Kevin
Ranch Hand

Joined: Jul 24, 2006
Posts: 43
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

Joined: Oct 31, 2007
Posts: 100
well I guess you could say it like that. but that is the point.
Gary Kevin
Ranch Hand

Joined: Jul 24, 2006
Posts: 43
Thinks all!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can label be placed before switch?
 
Similar Threads
Help with loop label problem
compile time error
pls consider this program i don't know how h value is incremented in Label1:
Labeled continue in do-while loop
Dan's Exam on Flow control