How to break out of nested loops in Java?

Today we are going to discuss about How to break out of nested loops in Java?. We also discuss about break and continue, how and in which situation we can use break and continue keyword.

 

 

Where and why we are using nested loop (multiple loop).

There are many situations where we need to be nested loops in Java, Nested loop mean’s one loop containing another loop like to implement many O(n^2) or quadratic algorithms e.g. insertion sort, selection sort. Suppose we need to printing the pascal triangle and printing those star structures exercises from school days.

 

Lest understand with real problem where we are using nested loop, Suppose we need to fetch some data from list of collection, like we have on list of some array List and in that array List. There are some other list are also available then we need to fetch data from that inner list then we need to use nested loop for fetching that particular data. So ones we get that data then we need to exit from that loop, Now we don’t want to iterate unwanted because our requirement is completed.

How to break nested loop or outer loop.

There are two steps to break from a nested loop in java, the first part is labeling loop and the second part is using labeled break.

 

How to use Label to Break Nested Loop in Java.

If you are using Label then you must put your label before the loop and you need a colon after the label as well. When you use that label after the break, control will jump outside of the labeled loop.

 

Suppose if you have 10 level of nested loop, then you can break from all of them by just calling break and label of first loop. Same if you use labeled continue, it starts continuing from the labeled loop.

 

Example:

public class BreakingNestedLoop {
    public static void main(String[] args) {
        outerloop:
        for (int i=0; i < 5; i++) {
            for (int j=0; j < 5; j++) {
                if (i * j > 6) {
                    System.out.println("BreakingNestedLoop");
                    break outerloop;
                }
                System.out.println(i + " " + j);
            }
        }
        System.out.println("Done");
    }
}

 

Output:

0 0
0 1
0 2
0 3
0 4
1 0
1 1
1 2
1 3
1 4
2 0
2 1
2 2
2 3
Breaking
Done

 

There are way to use label in java Example:

public class Test {
    public static void main(String[] args) {
        loop();
        System.out.println("Done");
    }

    public static void loop() {
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 5; j++) {
                if (i * j > 6) {
                    System.out.println("Breaking");
                    return;
                }
                System.out.println(i + " " + j);
            }
        }
    }
}

Read more about label .

 

Read more topics related to java.

← 

Hope this was helpful for you. If you have any questions please feel free to leave a comment. Thank you for reading.

Leave a Reply

Your email address will not be published.