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 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 |
Output:
value of j = 0
Using continue with label in Java
We can also use continue instead of break. See following program for example.
// 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 |
Output:
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 loop.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
leave a comment
0 Comments