Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

labels for break statement

 
Chun Wang
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just for clarity breaks can be used for breaking loops and switches.
 
Don Zur
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks guys!
this is really helpful!
chun
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic