Skip to content

Java Continue Statement | Label | in While, For Loop, Outer Loop Examples

  • by

Java Continue Statement is used when in a loop needed a jump to the beginning of the loop for the next iteration(loop cycle). It’s skipping execution of statements(code) inside the loop’s body after “Continue Keyword” for the current iteration.

Java Continue Statement or Label in While, For Loop, Outer Loop Examples

In this tutorial, you will learn about Continue Label with an example of many type control statements like While Loop, For Loop if condition, etc.

Syntax

Here is Java Continue syntax.

loop{ 
   continue;
  // More code here
}

Flow Diagram of Continue Statement

Flow diagram Java Continue Statement withExamples

Java continue example (for-each Loop)

Using a for-each loop and then checking the condition with an if statement. If in the Loop given if the condition is true then skip the current iteration.

public class ContinueExampleForEach {
    public static void main(String args[]) {
        int[] numbers = {0, 1, 2, 3};

        for (int x : numbers) {
            if (x == 2) {
                continue;
            }
            System.out.println(x);
        }
    }
}

Output: 0
1
3

How to Continue statement works:

Here is an explanation in the diagram of how an above continue program has worked.

How to java Continue statement works

Use Java continue in while loop

The above example was in the for-each loop (for each loop is an enhanced version of for loop), now see the example of how it’s working with a while loop in java. Again if a condition statement required in this example.

public class ContinueExampleWhileLoop {
    public static void main(String args[]) {
        int number = 0;
 
        while (number <= 3) {
            if (number == 2) {
                number++;
                continue;
            }
            System.out.println(number);
            number++;
        }
    }
}

Continue Lable with For Loop

The first example is in for each loop, now look at the example in for loop.

Give Extra time to look at an example, this time we are checking condition in if condition with an Array index.

public class ContinueExample {

    public static void main(String args[]) {
        int[] numbers = {0, 1, 2, 3};

        for (int i = 0; i < numbers.length; i++) {
            if (i == 2) {
                continue;
            }
            System.out.println(numbers[i]);
        }
    }
}

Output: 0
1
3

Continue in a do-While loop

public class ContinueExampleDoWhile {
    public static void main(String args[]) {
        int number = 5;
        do {
            if (number == 8) {
                number++;
                continue;
            }
            System.out.print(number + " ");
            number++;
        } while (number < 10);
    }
}

Output: 5 6 7 9

QA: How to use Java continue in the outer loop?

A Java Continue label can be used in the nested loop program, Where it can skips the current execution of the inner loop and current iteration of the outer loop too.
Inner Loop example of Java Continue Statement, in program Will Skip print 2 2.

public class ContinueExample {

    public static void main(String args[]) {
        //outer loop
        for (int i = 1; i <= 2; i++) {
            //inner loop
            for (int j = 1; j <= 2; j++) {
                if (i == 2 && j == 2) {
                    // continue statement
                    continue;
                }
                System.out.println(i + " " + j);
            }
        }
    }
}

Output: 1 1
1 2
2 1

QA: What is the “continue” keyword and how does it work in Java?

A Continue label or Continue Keyword or Continue statement Or Continue Word in Java is the same, See the above example definition with how works.
Java Continue Statement with Labeled For Loop example – Using a continue label can continue any loop in Java.

public class ContinueLabelExample {

    public static void main(String[] args) {

        label:
        for (int i = 1; i < 3; ++i) {
            System.out.println("Parent Loop " + i);
            for (int j = 1; j < 2; ++j) {
                System.out.println("child Loop " + j);
                if (i == 2 || j == 1 )
                    continue label;
                System.out.println(i + " " + j);
            }
        }
    }
}

Output: Parent Loop 1
child Loop 1
Parent Loop 2
child Loop 1

Sidenotes: It’s useful when you required to skip particular conditions in a loop like for example you want to skip an exception divide by zero. So that time you can define a Java continue Statement and the program will skip work on this code.

Java continues in if a statement is used so many times in this tutorial, it’s required to get the particular condition.

Do You have any other example on this topic do comment below?

Note: This example (Project) is developed in IntelliJ IDEA 2018.2.6 (Community Edition)
JRE: 11.0.1
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.14.1

Java version 11

All Examples Java Continue Statement with loops and control statement are in Java 11, so it may change on different from Java 9 or 10 or upgraded versions.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading