Break Any Outer Nested Loop by Referencing its Name in Java Last Updated : 23 Jul, 2025 Suggest changes Share 1 Likes Like Report A nested loop is a loop within a loop, an inner loop within the body of an outer one. Working: The first pass of the outer loop triggers the inner loop, which executes to completion. Then the second pass of the outer loop triggers the inner loop again. This repeats until the outer loop finishes. A break within either the inner or outer loop would interrupt this process. The Function of Nested loop : Java // Nested for loop import java.io.*; class GFG { public static void main(String[] args) { for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { System.out.print("GFG! "); } System.out.println(); } } } OutputGFG! 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 } Java // Naming the loop import java.io.*; class GFG { public static void main(String[] args) { // Type the name outside the loop outer: for (int i = 0; i < 5; i++) { System.out.println("GFG!"); } } } OutputGFG! GFG! GFG! GFG! GFG! Rules to Label the loops: Label of the loop must be unique to avoid confusion.In the break statements use labels that are in scope. (Below is an implementation) Java // Break statements and naming the loops import java.lang.*; public class GFG { public static void main(String[] args) { // Using Label for outer and for loop outer: for (int i = 1; i <= 3; i++) { // Using Label for inner and for loop inner: for (int j = 1; j <= 3; j++) { if (j == 2) { break inner; } System.out.println(i + " " + j); } if (i == 2) { break outer; } } } } Output1 1 2 1 R rohit2sahu Follow 1 Article Tags : Java Java-Control-Flow Explore Java BasicsIntroduction to Java3 min readJava Programming Basics9 min readJava Methods6 min readAccess Modifiers in Java4 min readArrays in Java7 min readJava Strings8 min readRegular Expressions in Java3 min readOOP & InterfacesClasses and Objects in Java9 min readAccess Modifiers in Java4 min readJava Constructors4 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages7 min readJava Interface7 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java4 min readIterator in Java5 min readJava Comparator Interface6 min readException HandlingJava Exception Handling6 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java10 min readFile Handling in Java4 min readJava Method References9 min readJava 8 Stream Tutorial7 min readJava Networking6 min readJDBC Tutorial5 min readJava Memory Management4 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers15+ min readJava Programs - Java Programming Examples7 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz1 min readJava Project Ideas For Beginners and Advanced15+ min read My Profile ${profileImgHtml} My Profile Edit Profile My Courses Join Community Transactions Logout Like