Does Java support goto?
Java does not support goto, it is reserved as a keyword just in case they wanted to add it to a later version.
- 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.
Using break with label in Java
Java
// Java code to illustrate // using label and break // instead of goto // file name: Main.java public class Main { public static void main(String[] args) { // label for outer loop outer: 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
Using continue with label in Java
We can also use continue instead of break. See following program for example.
Java
// Java code to illustrate // using label and continue // instead of goto // file name: Main.java public class Main { public static void main(String[] args) { // label for outer loop outer: for ( int i = 0 ; i < 10 ; i++) { for ( int j = 0 ; j < 10 ; j++) { if (j == 1 ) continue outer; System.out.println( " value of j = " + j); } } // end of outer loop } // end of main() } // end of class Main |
value of j = 0 value of j = 0 value of j = 0 value of j = 0 value of j = 0 value of j = 0 value of j = 0 value of j = 0 value of j = 0 value of j = 0
Explanation: Since continue statement skips to the next iteration in the loop, it iterates for 10 times as i iterates from 0 to 9. So the outer loop executes for 10 times and the inner for loop executes 1 time in each of the outer loops.
Java does not have a goto statement because it provides a way to branch in an arbitrary and unstructured manner. This usually makes goto-ridden code hard to understand and hard to maintain. It also prohibits certain compiler optimization. There are, however, a few places where the goto is a valuable and legitimate construct for flow control. For example, the goto can be useful when you are exiting from a deeply nested set of loops. To handle such situations, Java defines an expanded form of the break statement.
The general form of the labelled break statement is:
break label;
Example 1:
Java
// Java code public class Label_Break1 { public static void main(String[] args) { boolean t = true ; first : { second : { third : { System.out.println( "Before the break" ); if (t) // break out of second block break second; } System.out.println( "This won't execute" ); } System.out.println( "This is after the second block" ); } } } // This code is contributed by Sagar Gupta |
Before the break This is after the second block
Example 2:
Java
// Java code public class Label_Break2 { public static void main(String[] args) { outer: for ( int i = 0 ; i < 3 ; i++) // label { System.out.print( "Pass " + i + ": " ); for ( int j = 0 ; j < 100 ; j++) { if (j == 10 ) { break outer; // Exit both loops } System.out.print(j + " " ); } System.out.println( "This will not be printed" ); } System.out.println( "Loops Complete." ); } } // This code is contributed by Sagar Gupta |
Pass 0: 0 1 2 3 4 5 6 7 8 9 Loops Complete.