This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes a labeled break outside of an if statement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "a labeled break outside of an if statement" Watch "a labeled break outside of an if statement" New topic
Author

a labeled break outside of an if statement

Rick Reumann
Ranch Hand

Joined: Apr 03, 2001
Posts: 281
I know you normally don't see labeled breaks outside of if statements but it came up on a test. What I don't really understand is there a purpose for them? If I try to do anything after the break you get a compile error "unreachable statement."
For example:

If you uncomment out the here2 print stmt you'll get a compile error. So my question is what is the point of ever having a labeled break in an if statement if you can't have anything after the labeled break?
[ February 02, 2002: Message edited by: Rick Reumann ]
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Rick,
yeah that's somewhat weird...
You know that Java has no goto statement unlike C/C++. Such a break statement can be used to achieve the same behavior as a goto in C/C++.
The fact that the compiler objects to the unreachable statement (here2) is pretty normal since that statement is in fact unreachable.
HIH


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
NOT RECOMMENDED


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
NOT RECOMMENDED

Right, I should have added that... Bad practice...
Rick Reumann
Ranch Hand

Joined: Apr 03, 2001
Posts: 281
Thanks! That makes sense for breaking out of nested if statements, although I agree in practice I'd never do it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: a labeled break outside of an if statement
 
Similar Threads
about "for" and "if-else" ??
K&B SCJP5 Reconcile of ExamWatches
break and label...what the heck?
Help!! Runnable Interface Problem!!!
for Loop with y++ etc...