How to break / stop loop in Java?

UPDATED: 11 December 2015
Following examples demonstrates how you can break loop in java using simple break keyword and break with label.

Break(loop) simple loops
Following example shows how you can break single for-loop, for-each  loop, while-loop and do-while loop.

Source code (BreakLoopExample.java)
/**
 * Example: Break loop in Java
 *
 * @author javaQuery
 * @date 11th December, 2015
 * @Github: https://github.com/javaquery/Examples
 */
public class BreakLoopExample {

    public static void main(String[] args) {
        System.out.println("break(stop) for loop");
        for (int i = 0; i < 10; i++) {
            if (i == 2) {
                /* Loop will be terminated when i == 2 */
                break;
            }
            System.out.println("for-loop: " + i);
        }

        System.out.println("------------------------------------------");
        System.out.println("break(stop) for-each loop");

        int[] numbers = {0, 1, 2, 3, 4};
        for (int number : numbers) {
            if (number == 2) {
                /* Loop will be terminated when number == 2 */
                break;
            }
            System.out.println("for-each loop: " + number);
        }

        System.out.println("------------------------------------------");
        System.out.println("break(stop) while loop");

        int i = 10;
        while (i > 0) {
            if (i == 8) {
                /* Loop will be terminated when i == 8 */
                break;
            }
            System.out.println("while loop: " + i);
            i--;
        }

        System.out.println("------------------------------------------");
        System.out.println("break(stop) do-while loop");

        int j = 10;
        do {
            if (j == 8) {
                /* Loop will be terminated when j == 8 */
                break;
            }
            System.out.println("do-while loop: " + j);
            j--;
        } while (j > 0);
    }
}

Output
break(stop) for loop
for-loop: 0
for-loop: 1
------------------------------------------
break(stop) for-each loop
for-each loop: 0
for-each loop: 1
------------------------------------------
break(stop) while loop
while loop: 10
while loop: 9
------------------------------------------
break(stop) do-while loop
do-while loop: 10
do-while loop: 9

Break(stop) Multiple loops using label
When you want to break multiple loop then create label above the loop and now you can break that loop using break lable-name. Following example shows how you can break multiple loops.

Source code (BreakLoopWithLabel.java)
/**
 * Example: Break loop using label in Java
 *
 * @author javaQuery
 * @date 11th December, 2015
 * @Github: https://github.com/javaquery/Examples
 */
public class BreakLoopWithLabel {

    public static void main(String[] args) {

        System.out.println("break multiple loop using label");

        /* Label used to break loop anytime */
        rootForLoop:
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 3; j++) {
                if (i == 1 && j == 2) {
                    break rootForLoop;
                }
                System.out.println("i = " + i + ", j = " + j);
            }
        }

        System.out.println("------------------------------------------");

        /* Label used to break loop anytime */
        rootWhileLoop:
        while (true) {

            parentForLoop:
            for (int m = 0; m < 10; m++) {

                childForLoop:
                for (int n = 0; n < 3; n++) {
                    if (m == 0 && n == 2) {
                        break rootWhileLoop;
                    }
                    System.out.println("m = " + m + ", n = " + n);
                }
            }
        }
    }
}

Output
break multiple loop using label
i = 0, j = 0
i = 0, j = 1
i = 0, j = 2
i = 1, j = 0
i = 1, j = 1
------------------------------------------
m = 0, n = 0
m = 0, n = 1

Other References:
Example of continue in Java

0 comments :