VOOZH about

URL: https://www.geeksforgeeks.org/java/break-any-outer-nested-loop-by-referencing-its-name-in-java/

⇱ Break Any Outer Nested Loop by Referencing its Name in Java - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Break Any Outer Nested Loop by Referencing its Name in Java

Last Updated : 23 Jul, 2025

A nested loop is a loop within a loop, an inner loop within the body of an outer one.

Working:

  1. The first pass of the outer loop triggers the inner loop, which executes to completion. 
  2. Then the second pass of the outer loop triggers the inner loop again. 
  3. This repeats until the outer loop finishes. 
  4. A break within either the inner or outer loop would interrupt this process.

The Function of Nested loop :


Output
GFG! GFG! GFG! 
GFG! GFG! GFG! 
GFG! GFG! GFG! 

Label the loops:

This is how we can label the name to the loop:

labelname :for(initialization;condition;incr/decr){ 
 //code to be executed 
} 

Output
GFG!
GFG!
GFG!
GFG!
GFG!


Rules to Label the loops:

  1. Label of the loop must be unique to avoid confusion.
  2. In the break statements use labels that are in scope. (Below is an implementation)

Output
1 1
2 1
Comment
Article Tags: