Showing posts with label while-loop. Show all posts

Example of continue in Java

continue is used to skip the current iteration in Java.

continue in loops
Following example shows how you can use continue in for-loop, for-each loop, while-loop and do-while loop.

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

    public static void main(String[] args) {
        System.out.println("continue in for loop");
        for (int i = 0; i < 5; i++) {
            if (i == 2) {
                /* Loop will skip current iteration when i == 2 */
                continue;
            }
            System.out.println("for-loop: " + i);
        }

        System.out.println("------------------------------------------");
        System.out.println("continue in for-each loop");

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

        System.out.println("------------------------------------------");
        System.out.println("continue in while loop");

        int i = 5;
        while (i > 0) {
            if (i == 1) {
                i--;
                /* Loop will skip current iteration when i == 1 */
                continue;
            }
            System.out.println("while loop: " + i);
            i--;
        }

        System.out.println("------------------------------------------");
        System.out.println("continue in do-while loop");

        int j = 5;
        do {
            if (j == 2) {
                j--;
                /* Loop will skip current iteration when j == 2 */
                continue;
            }
            System.out.println("do-while loop: " + j);
            j--;
        } while (j > 0);
    }
}

Output
continue in for loop
for-loop: 0
for-loop: 1
for-loop: 3
for-loop: 4
------------------------------------------
continue in for-each loop
for-each loop: 0
for-each loop: 1
for-each loop: 2
for-each loop: 4
------------------------------------------
continue in while loop
while loop: 5
while loop: 4
while loop: 3
while loop: 2
------------------------------------------
continue in do-while loop
do-while loop: 5
do-while loop: 4
do-while loop: 3
do-while loop: 1

continue in loops using label
Following example shows how you can skip multiple loop using continue label-name.

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

    public static void main(String[] args) {
        System.out.println("continue in loop using label");

        /* Label used to continue loop */
        rootForLoop:
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                if (i == 1 && j == 2) {
                    System.out.println(">>>> skipped");
                    /* Loop will skip current iteration when i == 1 and j == 2 */
                    continue rootForLoop;
                }
                System.out.println("i = " + i + ", j = " + j);
            }
        }

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

        /* Label used to continue loop */
        parentForLoop:
        for (int l = 0; l < 2; l++) {

            childForLoop:
            for (int m = 0; m < 3; m++) {
                int n = 1;

                rootWhileLoop:
                while (n <= 2) {
                    if (l == 0 && m == 2) {
                        System.out.println(">>>> skipped");
                        /* Loop will skip current iteration when l == 0 && m == 2 */
                        continue childForLoop;
                    }
                    System.out.println("l = " + l + ", m = " + m + ", n = " + n);
                    n++;
                }
            }
        }
    }
}

Output
continue in loop using label
i = 0, j = 0
i = 0, j = 1
i = 0, j = 2
i = 1, j = 0
i = 1, j = 1
>>>> skipped
i = 2, j = 0
i = 2, j = 1
i = 2, j = 2
------------------------------------------
l = 0, m = 0, n = 1
l = 0, m = 0, n = 2
l = 0, m = 1, n = 1
l = 0, m = 1, n = 2
>>>> skipped
l = 1, m = 0, n = 1
l = 1, m = 0, n = 2
l = 1, m = 1, n = 1
l = 1, m = 1, n = 2
l = 1, m = 2, n = 1
l = 1, m = 2, n = 2

Other References:
How to break / stop loop in Java?

How to break / stop loop in Java?

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