Does Java support goto?

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:
public class Main {
  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

value of j = 0

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Please write to us at to report any issue with the above content.

Recommended Posts:

1.5 Average Difficulty : 1.5/5.0
Based on 27 vote(s)