Java - What is the output: break with label

Question

What is the output of the following code.


public class Main{

  public static void main(String[] args) {
    outer:
      for(int i=0; i< 3; i++) {
        for(int j=0; j<3; j++) {
          System.out.println("j="+j);
          break outer;
        }
      }
   
  }
}


Click to view the answer

j=0

Demo

public class Main{

  public static void main(String[] args) {
    outer://from w w  w. j av  a 2  s.c  om
      for(int i=0; i< 3; i++) {
        for(int j=0; j<3; j++) {
          System.out.println("j="+j);
          break outer;
        }
      }
   
  }
}

Note

break with label will jump to the label.

Related Topic