LABEL IN JAVA

Unlike C/C++, Java does not have goto statement, but java supports label. The only place where a label is useful in Java is right before nested loop statements. We can specify label name with break to break out a specific outer loop. Similarly, label name can be specified with continue.

See following program for example.

// file name: NewFile.java
public class NewFile{
  public static void main(String[] args) {   
    outer: //label for outer loop
    for (int l = 1; l < 3; l++) {
      for (int m = 1; m < 3; m++) {
        if (m == 2)
          break outer;
        System.out.println(" value of m = " + m);
      }
    
  
Output:
value of m = 1

One thought on “LABEL IN JAVA”

Leave a Reply