Label In Java

Java support label?

Yes 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.


public class LabelTest{
  public static void main(String[] args) {    
    outer: //label for outer loop
    for (int i = 0; i < 10; i++) { 
      for (int j = 0; j < 10; j++) {
        if (j == 1)
          break outer;
        System.out.println(" value of j = " + j);
      }
    } //end of outer loop
  } // end of main()
} //end of class Main

Output:
value of j = 0

 

Related Articles

post a comment