This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes labels for break statement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "labels for break statement" Watch "labels for break statement" New topic
Author

labels for break statement

Chun Wang
Ranch Hand

Joined: Jan 24, 2001
Posts: 42
I found this question on Jargon.
=========================================
11) A label for the break statement
a) must be before a for, do or while loop (other than white space(s) and comments)
b) must be before an if, else, for, do or while statements (other than white space(s) and comments)
c) should not be before another label
d) must be before a for,do,while loop or a block inside a loop (other than white space(s) and comments)
e) None of the above
Select the most appropriate answer
==========
the answer given is D. but I don't think it's right to say "must be" because you can put a label in front of any block of code and put break label inside that block of code. you can do:
label1: {int www, uuu; break label1;}
it will compile.
am I correct or am I missing something? please help!
thanks a lot!
chun
Charlie Swanson
Ranch Hand

Joined: Jan 29, 2001
Posts: 213
Hello, I ran your code and it worked with no problem. It works with the label right outside the block.
There is this point from Sun Certified Java Programmer page 139:
"Labels have meaning only for break and continue statements that
occur inside the block of code with whic the label is associated"
regarding break and continue: The break and continue statements enable the programmer to terminate execution of an iteration of a loop. (Break is for terminating loops)
Since labels are only for break and continue statements, and break and continue statements are for breaking loops. I am inferring that labels are used in loops.
All the above aside, the code you created runs. Isn't that the bottom line.
Charlie Swanson
Ranch Hand

Joined: Jan 29, 2001
Posts: 213
Just for clarity breaks can be used for breaking loops and switches.
Don Zur
Greenhorn

Joined: Apr 16, 2000
Posts: 4
I think you are correct. Here is an excerpt from the form JLS:
A break statement with label Identifier attempts to transfer control to the enclosing labeled statement (�14.6) that has the same Identifier as its label; this statement, which is called the break target, then immediately completes normally. In this case, the break target need not be a while, do, for, or switch statement.

Code that uses a label break outside context of while, do, for or switch:

Chun Wang
Ranch Hand

Joined: Jan 24, 2001
Posts: 42
thanks guys!
this is really helpful!
chun
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: labels for break statement
 
Similar Threads
Label association doubt
Exam Questions -Req. Clarification
Label for the continue/break statement
a question from Dan's topic mock exam
Can label be placed before switch?