Real'sHowTo AddThis Feed Button
Custom Search

Use a Label breakTag(s): Language


Labelled breaks allow breaking out of several levels of nested loops inside a pair of curly braces. This way, you can almost simulate a GOTO!
class JavaGoto {
  public static void main(String args[]) {

  int max = 10;
  int limit = 5;
  int j = 0;
  out: { for( int row=0; row< max; row++ ) {
          for( int col=0; col< max; col++ )
            if( row == limit) break out;
         j += 1;
         }
       }
  System.out.println(j); // output 5
  }
}

blog comments powered by Disqus


If you find this article useful, consider making a small donation
to show your support for this Web site and its content.

Written and compiled by Réal Gagnon ©1998-2013
[ home ]