user3586231
user3586231

Reputation: 365

Label break statement in Java: compile time error

use the label OUTER_LOOP but when i use it below the break statement i works fine but,

when i use it above the break statement it gives error "label is missing".

 public void twoNum( int num, int val )
 {

 for ( int i = 0 ; i < num ; i++ )
 {

 for ( int j = 0 ; j < num ; j++ )
             {
     if ( i + j >= 2 * val )
        break OUTER_LOOP ;
     val = val / 2 ;
  }
  OUTER_LOOP: 
 }
 // break comes here if it runs
 }

Upvotes: 0

Views: 1006

Answers (1)

Bhesh Gurung
Bhesh Gurung

Reputation: 51030

If you want to break out of the outer loop from within the inner loop then you should use it as follows -

OUTER_LOOP: //put it right before the outer loop
for (int i = 0; i < num; i++) {
    for (int j = 0; j < num; j++) {
        if(condition) {
            break OUTER_LOOP;
        }

Upvotes: 2

Related Questions