Java Continue Statement

A continue statement skips the remaining statements of current iteration of a loop and the program control moves to the end of the loop. Next iteration of the loop starts by evaluating the test condition (update statement is executed in case of a for loop). Unlike break statement, continue won't terminate loop but skips remaining statements of loop's code block and start new iteration from beginning.

We use continue statement, when we want to skip execution of current iteration if some condition becomes true. The continue statement is generally used with decision making statements.

In case of nested loops, if continue statement is encountered inside inner loop then it will only skip current iteration of inner loop. In general, it skips the current iteration of loop enclosing it.

Syntax of continue Statement

continue;

Continue statement inside for loop

When a continue statement is executed inside a for loop, it skips the remaining statements of the loop code block immediately and program control starts execution of update statement and evaluation of control statement of for loop for next iteration.

for (initialization; condition; update) {
    statements;
    if(condition to continue) {
        continue;
    }
    statements;
}

Continue statement in for loop example program

public class ForLoopContinueStatement {
    public static void main(String[] args) {
        int count;
        for (count = 0; count < 10; count++) {
            // Skip if count is even number
            if (count%2 == 0) {
                continue;
            }
            System.out.println(count);
        }
    }
}
Output
1
3
5
7
9

Continue statement inside while loop

When a continue statement is executed inside a while loop, it skips the remaining statements of the while code block immediately and program control starts evaluates condition statement for next iteration of while loop.

while (condition) {
    statements;
    if(condition to continue) {
        continue;
    }
    statements;
}

Continue statement in while loop example program

public class WhileLoopContinueStatement {
    public static void main(String[] args) {
        int count = 0;
        while (count < 10) {
            count++;
            // Skip if count is even number
            if (count % 2 == 0) {
                continue;
            }
            System.out.println(count);
        }
    }
}
Output
1
3
5
7
9

Continue statement inside do-while loop

When a continue statement is executed inside a do-while loop, it skips the remaining statements of the do-while code block immediately and program control starts evaluation of condition statement of do-while loop for next iteration.

do {
    statements;
    if(condition to continue) {
        continue;
    }
    statements;
} while (condition);

Continue statement in do-while loop example program

public class DoWhileLoopContinueStatement {
    public static void main(String[] args) {
        int count = 0;
        do {
            count++;
            // Skip if count is even number
            if (count % 2 == 0) {
                continue;
            }
            System.out.println(count);
        } while (count < 10);
    }
}
Output
1
3
5
7
9

Continue statement in nested loop

In case of nested loop, if continue statement is encountered it will only skip the execution of loop enclosing it.

  • If continue statement is encountered inside inner loop then it will only skip the execution of inner loop.
  • If continue statement is encountered inside outer loop then it will skip the execution of outer loop.

public class ContinueNestedLoop {
    public static void main(String[] args) {
        int i, j;
        for (i = 0;i < 5;i++) {
            for (j = 0;j < 5;j++) {
                if (i != j) {
                    continue;
                }
                System.out.println("i="+ i +",j="+ j);
            }
        }
    }
}
Output
i=0,j=0
i=1,j=1
i=2,j=2
i=3,j=3
i=4,j=4