Labelled breaks breaks out of several levels of nested loops inside a pair of curly braces. : Break Continue « Language Basics « Java






Labelled breaks breaks out of several levels of nested loops inside a pair of curly braces.

 

public class Main {
  public static void main(String args[]) {

    int len = 100;
    int key = 50;
    int k = 0;
    out: {
      for (int i = 0; i < len; i++) {
        for (int j = 0; j < len; j++) {
          if (i == key) {
            break out;
          }
          k += 1;
        }
      }
    }
    System.out.println(k);
  }
}

   
  








Related examples in the same category

1.Break DemoBreak Demo
2.Break With Label DemoBreak With Label Demo
3.Continue Demo Continue Demo
4.Continue With Label Demo
5.Demonstrates break and continue keywordsDemonstrates break and continue keywords