avgvstvs avgvstvs - 1 year ago 77
Java Question

Is using a labeled break a good practice in Java?

I'm staring at some old code from 2001 and came across this statement:

else {
do {
int c = XMLDocumentFragmentScannerImpl.this.scanContent();
if (c == 60) {
break label913:

I had never seeen this before, and discovered labeled breaks here:


Doesn't this essentially function like
? Is it even good practice to use it? It makes me uneasy.

Answer Source

No, it's not like a goto, since you can't "go" to another part of the control flow.

From the page you linked:

The break statement terminates the labeled statement; it does not transfer the flow of control to the label. Control flow is transferred to the statement immediately following the labeled (terminated) statement.

This means you can only break loops that are currently being executed.

Consider this example:

for( int i = 0; i < 10; i++) {
  for(int j = 0; j < 5; j ++ )
    break xxx;

for( int a = 0; a < 10; a++) {


You could replace xxx with first or second (to break the outer or inner loop), since both loops are being executed, when you hit the break statement, but replacing xxx with third won't compile.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download