Java - break with Label

What is a Label?

A label in Java is any valid Java identifier followed by a colon.

The following are some valid labels in Java:

label1:
alabel:
Outer:
Hello:
Control:
myControl:

labeled break statement

The following code shows how to use a labeled break statement.

Demo

public class Main {
  public static void main(String[] args) {
    outer: // Defines a label named outer
    for (int i = 1; i <= 3; i++) {
      for (int j = 1; j <= 3; j++) {
        System.out.print(i + "" + j);
        if (i == j) {
          break outer; // Exit the outer for loop
        }//  w  w w.j  a v a 2 s.  c om
        System.out.print("\t");
      }
      System.out.println();
    }

  }
}

Result

Related Topic