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
value of j = 0
Using continue with label in Java
We can also use continue instead of break. See following program for example.
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.
This article is attributed to GeeksforGeeks.org