The continue
statement in Java is used to skip the current iteration of a loop and jump to the next iteration. It is commonly used when you want to bypass certain parts of the loop based on a condition but still continue looping.
The continue
statement works with all types of loops in Java, including for
, while
, and do-while
Syntax of the continue
Statement
continue;
The continue
statement is usually placed inside a conditional block within the loop.
How the continue
Statement Works
When the continue
statement is executed:
- The loop immediately skips the remaining code for the current iteration.
- Control jumps to the next iteration of the loop.
- In a
for
loop, the update statement is executed before proceeding to the next iteration. - In a
while
ordo-while
loop, the condition is re-evaluated before the next iteration begins.
Example: Using continue
in a for
Loop
public class Main {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue; // Skip the rest of the loop when i == 3
}
System.out.println("i = " + i);
}
}
}
Output:
i = 1
i = 2
i = 4
i = 5
In this example, the loop skips printing the value 3
and continues with the next iteration.
Example: Using continue
in a while
Loop
public class Main {
public static void main(String[] args) {
int i = 1;
while (i <= 5) {
if (i == 3) {
i++; // Increment before continue to avoid an infinite loop
continue; // Skip the rest of the loop for i == 3
}
System.out.println("i = " + i);
i++;
}
}
}
Output:
i = 1
i = 2
i = 4
i = 5
Example: Using continue
in a Nested Loop
The continue
statement can also be used in nested loops. When used inside an inner loop, it skips the current iteration of that loop only.
public class Main {
public static void main(String[] args) {
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
if (j == 2) {
continue; // Skip the rest of the inner loop when j == 2
}
System.out.println("i = " + i + ", j = " + j);
}
}
}
}
Output:
i = 1, j = 1
i = 1, j = 3
i = 2, j = 1
i = 2, j = 3
i = 3, j = 1
i = 3, j = 3
Example: Labeled continue
Statement
Java allows the use of labels with continue
to skip iterations of an outer loop. A label is a name followed by a colon, placed before the loop.
public class Main {
public static void main(String[] args) {
outerLoop:
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
if (j == 2) {
continue outerLoop; // Skip to the next iteration of the outer loop
}
System.out.println("i = " + i + ", j = " + j);
}
}
}
}
Output:
i = 1, j = 1
i = 2, j = 1
i = 3, j = 1
Key Points to Remember
- The
continue
statement skips the current iteration of a loop and moves to the next iteration. - In a
for
loop, the update statement is executed before the next iteration. - In a
while
ordo-while
loop, the condition is checked again before proceeding. - Labeled
continue
can be used to skip iterations of an outer loop in nested loops.
When to Use the continue
Statement
- To skip specific iterations based on a condition.
- To make the code more readable by avoiding unnecessary
if-else
nesting. - In nested loops, to skip specific iterations of inner or outer loops using labels.
The continue
statement in Java is a handy tool for controlling the flow of loops. It helps developers skip certain iterations without breaking out of the loop entirely. By understanding how and when to use continue
, you can write cleaner and more efficient loop-based code.