
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Break Any Outer Nested Loop by Referencing Its Name in Java
Programming is all about coming up with the best and most efficient ways to solve the real?world problems. There are situations when you want to exit multiple loops simultaneously. This can be accomplished in Java by simply referencing the name of the loop you want to exit. In this tutorial, we'll look at how to break any outer nested loop in Java by referencing its name.
Referencing Loop Names in Java
You can break out of the Java nested loop by labelling the outer loop. This can be accomplished by using a label before the outer loop followed by a colon. The break statement and label combination can then be used inside the inner loop to exit the outer loop.
Syntax
Following is the syntax to reference the loop ?
labelname :for(initialization;condition;incr/decr){ //code to be executed }
Example
The following is an illustration of how to use a labelled break statement in Java to exit a nested loop:
public class Test{ public static void main(String args[]){ outerloop: for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { if (i == 2 && j == 2) { break outerloop; } System.out.println("i = " + i + ", j = " + j); } } } }
Output
i = 0, j = 0 i = 0, j = 1 i = 0, j = 2 i = 0, j = 3 i = 0, j = 4 i = 1, j = 0 i = 1, j = 1 i = 1, j = 2 i = 1, j = 3 i = 1, j = 4 i = 2, j = 0 i = 2, j = 1
In this example, the outer loop is labeled as "outerloop" using the label syntax. Within the inner loop, the if statement checks if the current values of i and j are equal to 2. If they are equal, you can use the break statement with the label "outerloop" to come out of the outer loop, otherwise, the loop continues to execute and prints the current values of i and j.
When this program is run, it will print the values of i and j for each iteration of the loop until i and j are both equal to 2 and at that point, the control will break out of the outer loop and terminate the program.
Practical Applications of Breaking Multiple Loops
A variety of circumstances call for the ability to break numerous loops. Take, for instance, a program that searches a multi?dimensional array for a specific element. If the element is located, we can exit both loops, which will reduce processing time. A program that checks for collisions in a game is another illustration. We can exit both loops if a collision is found to prevent pointless calculations.
Common Mistakes to Avoid
There are a few frequent errors to avoid when breaking multiple loops in Java. Forgetting to name the outer loop is one of the most frequent mistakes. We won't be able to use the outer loop's label in the break statement if we don't label it. Using the incorrect loop name in the break statement is another frequent error. When exiting a loop, be sure to use the right name.
Conclusion
In this article, you learned about how to break any outer nested loop by referencing its name in Java. You also came across how to create nested loops, how to break out of a single loop, and how to break out of multiple loops using a labelled loop along with the practical applications of breaking multiple loops and common mistakes to avoid.