In Java, the continue
statement is used to skip the current iteration of a loop and continue with the next iteration. This can be helpful when certain conditions within a loop make it unnecessary to execute the remaining statements for that particular iteration, but you want the loop to continue running for the next iterations.
The continue
statement works differently than the break
statement, which terminates the loop altogether. Instead, the continue
allows the loop to skip the current iteration and proceed to the next iteration based on the loop's condition.
In this guide, we'll explore the continue
statement in detail, its syntax, usage in loops, and provide examples to illustrate its behavior.
The continue
statement is used inside loops to skip the current iteration and move to the next iteration of the loop. When the continue
statement is executed, the remaining code inside the loop for that iteration is skipped, and the loop proceeds to the next iteration based on the condition.
Unlike the break
statement, which terminates the loop entirely, the continue
statement only affects the current iteration.
The syntax of the continue
statement is simple:
continue;
continue
statement can be used without any condition, and it will simply skip the remaining code inside the loop and move to the next iteration.In loops with multiple conditions, such as for
or while
loops, the continue
statement can be particularly useful to skip the rest of the current iteration when certain conditions are met.
When the continue
statement is encountered inside a loop, it immediately skips the remaining part of the current iteration and proceeds to the next iteration of the loop.
for
loop:continue
statement is encountered, the remaining part of the current iteration is skipped, and the control moves to the next iteration. The loop’s increment/decrement statement is executed before checking the condition again.while
or do...while
loop:continue
statement is executed, it skips the remaining part of the loop’s body and goes directly to the next iteration.Let’s explore some examples to understand how the continue
statement works in various loop types.
In this example, we use the continue
statement inside a for
loop to skip printing even numbers and print only odd numbers between 1 and 10.
public class ContinueInForLoop {
public static void main(String[] args) {
// Loop from 1 to 10
for (int i = 1; i <= 10; i++) {
// Skip even numbers
if (i % 2 == 0) {
continue; // Skip the current iteration if i is even
}
System.out.println(i); // Print the odd numbers
}
}
}
Explanation:
i
is even, the continue
statement is executed, skipping the System.out.println(i)
statement for that iteration.
1
3
5
7
9
In this example, we use the continue
statement inside a while
loop to skip printing numbers divisible by 3.
public class ContinueInWhileLoop {
public static void main(String[] args) {
int i = 1;
while (i <= 10) {
if (i % 3 == 0) {
i++; // Increment before continue to avoid infinite loop
continue; // Skip numbers divisible by 3
}
System.out.println(i);
i++;
}
}
}
Explanation:
while
loop runs as long as i
is less than or equal to 10.i
is divisible by 3, the continue
statement is triggered, skipping the System.out.println(i)
statement.
1
2
4
5
7
8
10
In this example, we use the continue
statement inside a nested loop to skip printing even numbers in the inner loop.
public class ContinueInNestedLoop {
public static void main(String[] args) {
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 5; j++) {
// Skip even numbers in the inner loop
if (j % 2 == 0) {
continue;
}
System.out.println("i = " + i + ", j = " + j);
}
}
}
}
Explanation:
continue
statement skips even numbers in the inner loop, printing only the odd values of j
.
i = 1, j = 1
i = 1, j = 3
i = 1, j = 5
i = 2, j = 1
i = 2, j = 3
i = 2, j = 5
i = 3, j = 1
i = 3, j = 3
i = 3, j = 5
Use continue
for specific conditions: The continue
statement is useful when you want to skip certain iterations based on specific conditions. For example, skipping even numbers or invalid inputs in a loop.
Keep your loops readable: Overusing the continue
statement can make your loops harder to read and understand. Use it when necessary but avoid excessive complexity.
Control the flow clearly: Make sure that the condition for skipping the iteration is clearly defined and understood by others reading your code.
Avoid infinite loops: When using continue
, always ensure that the loop's condition will eventually be satisfied to avoid creating an infinite loop.