Table of Contents
1. Introduction
The continue
statement in Java is used to skip the current iteration of a loop (for, while, or do-while) and proceed to the next iteration. When a continue
statement is executed, the loop does not terminate. Instead, it immediately jumps to the next iteration, bypassing any code following the continue
statement within the loop body.
2. Syntax
The syntax of the continue
statement is very simple. We just need to use the continue keyword followed by a semicolon (;). We can also use labeled continue statements, which we will explore later in the article.
1 2 3 4 5 6 7 8 9 |
while (condition) { //statement(s) if(continue-condition) continue; //statement(s) } //statement(s) |
Here, iteration is skipped as soon as the continue statement is executed, bypassing any code following the continue
statement within the loop body.
3. Using continue Statement in For Loop
The continue
statement is commonly used in for
loops to skip the current iteration and proceed to the next iteration based on certain conditions. Let’s look at an example where we want to print only odd numbers from an array, skipping the even numbers.
Example: Print Odd Numbers
1 2 3 4 5 6 7 8 9 10 |
int[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; for(int i = 0; i < numbers.length; i++) { if(numbers[i] % 2 == 0) { // Check if the number is even continue; // Skip the rest of the loop body } System.out.println(numbers[i]); // Print the number if it is odd } |
Output:
1 2 3 4 5 6 7 |
1 3 5 7 9 |
In this example, the continue
statement is used to skip even numbers. When an even number is encountered, the continue
statement is executed, causing the loop to skip the System.out.println
statement and proceed to the next iteration.
4. Using continue Statement in While Loop
The continue
statement can also be used in while
loops to skip certain iterations based on a condition. Here’s an example that prints only positive numbers from a array, skipping any negative numbers.
Example: Skip Negative Numbers
1 2 3 4 5 6 7 8 9 10 11 12 13 |
int[] numbers = {1, -2, 3, -4, 5, -6, 7, 8, -9, 10}; int index = 0; while(index < numbers.length) { if(numbers[index] < 0) { // Check if the number is negative index++; continue; // Skip the rest of the loop body } System.out.println(numbers[index]); // Print the number if it is positive index++; } |
Output:
1 2 3 4 5 6 7 8 |
1 3 5 7 8 10 |
In this while
loop, the continue
statement skips the printing of negative numbers. When a negative number is encountered, the loop increments the index and continues to the next iteration without executing the print statement.
5. Using continue Statement in Do-While Loop
The continue
statement functions similarly in do-while
loops, allowing certain iterations to be skipped. Here is an example that filters out zero values from a array of numbers.
Example: Ignore Zero Values
1 2 3 4 5 6 7 8 9 10 11 12 13 |
int[] numbers = {0, 1, 2, 0, 3, 0, 4, 5}; int index = 0; do { if(numbers[index] == 0) { // Check if the number is zero index++; continue; // Skip the rest of the loop body } System.out.println(numbers[index]); // Print the number if it is not zero index++; } while(index < numbers.length); |
Output:
1 2 3 4 5 6 7 |
1 2 3 4 5 |
In this do-while
loop, the continue
statement is used to skip over zero values. If the current number is zero, the loop increments the index and proceeds to the next iteration without executing the print statement. The do-while
loop ensures that the loop body is executed at least once before the condition is checked.
6. Continue in Nested Loops
When using continue
in nested loops, it only impacts the immediate loop it’s in. Consider a nested loop scenario where we want to skip certain combinations:
1 2 3 4 5 6 7 8 9 10 |
for(int i = 1; i <= 3; i++) { for(int j = 1; j <= 3; j++) { if(i == 1 && j == 1) { continue; // Skip the current iteration of the inner loop } System.out.println("i = " + i + ", j = " + j); } } |
Output:
1 2 3 4 5 6 7 8 9 10 |
i = 1, j = 2 i = 1, j = 3 i = 2, j = 1 i = 2, j = 2 i = 2, j = 3 i = 3, j = 1 i = 3, j = 2 i = 3, j = 3 |
In this nested loop example, the iteration where i == 1
and j == 1
is skipped, demonstrating how continue
can be used to control the flow in complex looping structures. Please note that it impacted only inner loop, and we didn’t skip any iteration for outer loop.
What if we wanted to skip iteration for outer loop? To do that, we can use labeled continue statements.
7. Labeled continue Statement
A label is an identifier followed by a colon (:
) placed before a loop or block statement. When a continue statement specifies a label, it skips an iteration for the loop marked with that label, not just the nearest loop.
Consider a scenario where we have nested loops and want to skip to the next iteration of an outer loop based on a condition inside an inner loop.
1 2 3 4 5 6 7 8 9 10 |
label: for(int i = 1; i <= 3; i++) { for(int j = 1; j <= 3; j++) { if(j == 2) { continue label; // Skip to the next iteration of the loop labeled "label" } System.out.println("i = " + i + ", j = " + j); } } |
Output:
1 2 3 4 5 |
i = 1, j = 1 i = 2, j = 1 i = 3, j = 1 |
In this example, whenever j
equals 2, the continue label;
statement is executed. This causes the program to skip the remaining iterations of the inner loop and continue with the next iteration of the outer loop labeled label
. As a result, the output shows that the inner loop’s iterations are skipped whenever j == 2
.
8. Conclusion
The continue
statement in Java is a versatile tool for managing loop execution, allowing developers to skip certain iterations based on specific conditions. Through practical examples, we’ve seen how continue
can be applied in both simple and nested loops to control the flow of execution. We’ve also explored labeled continue
statements and their functionality