Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

continue — advance to the next iteration

continue — advance to the next iteration: continue ends the current iteration of a while, do, or for loop and transfers control to that loop’s continuation point.

Version: Java SE 21 (JLS and Java SE API); examples verified on the repository Java runtime

Syntax

continue

continue ends the current iteration of a while, do, or for loop and transfers control to that loop’s continuation point.

Use an unlabeled continue to skip one element, and continue label to begin the next iteration of a named enclosing loop.

A continue label must denote an enclosing loop statement; excessive labeled transfers make control flow difficult to follow.

A for loop evaluates its update expressions next, while while/do tests its condition; the transfer cannot cross a method, constructor, or lambda boundary.

Unlabeled continue skips column 2, and continue outer abandons the whole third row, leaving [11, 13, 21, 23].

Examples

import java.util.ArrayList;

public class Main {
    public static void main(String[] args) {
        var visited = new ArrayList<Integer>();
        outer:
        for (int row = 1; row <= 3; row++) {
            for (int column = 1; column <= 3; column++) {
                if (column == 2) continue;
                if (row == 3) continue outer;
                visited.add(row * 10 + column);
            }
        }
        System.out.println(visited);
    }
}

Result

[11, 13, 21, 23]

Unlabeled continue skips column 2, and continue outer abandons the whole third row, leaving [11, 13, 21, 23].

Sources