Java continue Statement

In this article, you will learn how to use continue construct and continue labels in Java.

Sometimes it is useful to force an early iteration of a loop. That is, you might want to continue running the loop but stop processing the remaining code in its body for this particular iteration. In such case we can use continue statement.

Java continue example

The continue statement is used to skip the iteration of the nearest enclosing loop (for, while, and do…while loop).

Basically, continue is used to repeat the loop for a specific condition. It skips the remaining statements and continues with the next iteration of the loop.

Syntax

Syntax of continue in for loop:

for (...) {
    //codes
    if (test-expression) {
        continue;
    }
    //codes
}

If test-expression is evaluated to true, continue statement is executed which skips the remaining statements inside the loop and continues with the next iteration of the loop.

Example

The below program will print even numbers between 1 to 10.

public class ContinueDemo {

    public static void main(String[] args) {

        for (int n = 1; n <= 10; n++) {

            if (n % 2 != 0)
                continue;

            System.out.print(" " + n + " ");

        }
    }
}

Output

 2  4  6  8  10

Example explained:

In the above example, when n = { 1, 3, 5, 7, 9 } :

  • If condition ( n % 2 ! = 0 ) is checked, yields true.Then, continue statement is executed, which skips the execution of System.out.print(” ” + n + ” “) statement.

Java continue example in nested loop

When continue is used in the nested loop (loop inside another loop), it skips the iteration of the nearest enclosing loop (inner loop) when it is encountered and continues with the next iteration of the loop.

Example

public class ContinueDemo {

    public static void main(String[] args) {

        for (int i = 1; i <= 3; i++) {
            for (int j = 1; j <= 2; j++) {

                System.out.println("Before continue,i = " + i + "; j = " +j);
                if (i == 2) {
                    continue;
                }
                System.out.println("i = " + i + "; j = " +j);
            }
        }
      
    }
}

Output

Before continue,i = 1; j = 1
i = 1; j = 1
Before continue,i = 1; j = 2
i = 1; j = 2
Before continue,i = 2; j = 1
Before continue,i = 2; j = 2
Before continue,i = 3; j = 1
i = 3; j = 1
Before continue,i = 3; j = 2
i = 3; j = 2

Example explained:

In the above example, when i = 2 :

  • If condition ( i == 2 ) is checked, yields true.Then, continue statement is executed, which skips the execution of System.out.println(“i = ” + i + “; j = ” +j) statement and passes the control to inner for-loop for further iteration.

Java labeled continue example

In Kotlin, labeled continue is used to skip the iteration of the desired loop when certain condition is satisfied. It can be done with the help of labels.

Label starts with an identifier which is followed by ‘ : ‘. e.g.- inner: , outer: etc.

Syntax

Syntax of labeled continue in for loop:

outer:
for (...){
    // code
    inner:
    for (...){
        // code
        if (condition for continue){
            continue outer;
        }
    }
}

Example

public class ContinueDemo {

    public static void main(String[] args) {

        label:
        for (int i = 1; i <= 5; i++) {
            for (int j = 1; j <= 2; j++) {
                if (i > 2) {
                    continue label;
                }
                System.out.println("i = " + i + "; j = " +j);
            }
        }

    }
}

Output

i = 1; j = 1
i = 1; j = 2
i = 2; j = 1
i = 2; j = 2

Example explained:

In the above example, when i = { 3, 4, 5 } :

  • If condition ( i > 2 ) is checked, yields true.Then, continue label is executed, which skips the execution of System.out.println(“i = ” + i + “; j = ” +j) statement and passes the control to the desired loop marked with label label: for further iteration.

 

 

Leave a Reply