Example of continue in Java

UPDATED: 11 December 2015
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?

0 comments :