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.
- Interesting facts about null in Java
- Comments in Java
- Classes and Objects in Java
- Using underscore in Numeric Literals in Java
- Type conversion in Java with Examples
- Binary Search in Java
- Arrays in Java
- Widening Primitive Conversion in Java
- Using _ (underscore) as variable name in Java
- Inner class in java
- Different ways to create objects in Java
- Do we need forward declarations in Java?
- How are Java objects stored in memory?
- Java Identifiers
- How to swap or exchange objects in Java?
Improved By : subramaniyanvenkatraman