Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

a labeled break outside of an if statement

 
Rick Reumann
Ranch Hand
Posts: 281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Marilyn de Queiroz
Sheriff
Posts: 9066
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
NOT RECOMMENDED
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
NOT RECOMMENDED

Right, I should have added that... Bad practice...
 
Rick Reumann
Ranch Hand
Posts: 281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks! That makes sense for breaking out of nested if statements, although I agree in practice I'd never do it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic