Java continue Statement
The continue
statement in Java is used when you want to skip the current iteration of a loop and continue from a specific point in the loop body.
In while and do-while loops, the continue
statement transfers control directly to the conditional expression that governs the loop. In a for loop, control is passed first to the iteration expression and then to the loop condition.
The continue
statement should be used only inside a loop structure.
Java continue Statement Example
In the following example, only odd numbers are printed to the console. If the number is even, the iteration is skipped using the continue
statement:
public class ContinueExample {
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
if (i % 2 == 0) {
continue;
}
System.out.print(i + " ");
}
}
}
Using continue with Label in Java
The continue
statement in Java can also be used with a label. In the following example, the outer loop is labeled as outer
. When the statement continue outer;
is reached, the outer loop immediately proceeds to its next iteration. Without the label, only the inner loop would be affected.
public class ContinueLabelExample {
public static void main(String[] args) {
outer:
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if (j > i) {
System.out.println();
continue outer;
}
System.out.print(" " + (i * j));
}
}
System.out.println();
}
}

Please log in or register to have a possibility to add comment.