Java Loop Break: Labeled vs Unlabeled

🚀 Labeled vs Unlabeled break in Java – Small Concept, Big Difference! When working with nested loops in Java, understanding the difference between labeled and unlabeled break is very important. 🔹 Unlabeled break Stops only the innermost loop. for(int i=0;i<3;i++){ for(int j=0;j<5;j++){ if(j==3){ break; // breaks only inner loop } System.out.println(i+" "+j); } } 👉 The outer loop continues execution. 🔹 Labeled break Stops the loop that has the label (even the outer loop). outerloop: for(int i=0;i<3;i++){ for(int j=0;j<5;j++){ if(j==3){ break outerloop; // breaks outer loop } System.out.println(i+" "+j); } } 👉 Both loops stop immediately. 💡 Simple understanding: Unlabeled break = Exit from a room Labeled break = Exit from the building This is especially useful while working with: ✔ Nested loops ✔ 2D arrays ✔ Search logic #Java #JavaProgramming #CoreJava #JavaDeveloper #Coding #InterviewPreparation #Developers #LearnToCode #TechLearning #break

  • No alternative text description for this image

To view or add a comment, sign in

Explore content categories