Labeled statements

Labeled statements are useful when dealing with nested loops. For example, if you have one loop inside another and call ‘break’, which ends a loop, it is the inner loop that is exited.
However, if you also want to exit the outer loop you can label it and call ‘break’ with that label.
For example, consider these loops (see section about loops for more info):

This dummy example just wants to exit when j == 5, but when calling break above it’ll just exit the inner loop. To exit the outer loop we label the outer loop and use that label:

Search for more Java info on this site here:
Custom Search

Any thoughts or questions?