Does Java support goto?

1.6

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: Main.java
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

Output:
value of j = 0

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

GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.

Recommended Posts:



1.6 Average Difficulty : 1.6/5.0
Based on 23 vote(s)










Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.