简体   繁体   中英

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
 }

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;
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM