aspose file tools*
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
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: 9052
    
  12
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: a labeled break outside of an if statement