Java Continue statement plays an important role in loop
control by skipping the current iteration and proceeding with the next
iteration. By utilizing the continue statement, you can optimize your code,
exclude certain iterations, and streamline the execution flow. In this detailed
guide, we will dive deep into the power of the Java continue statement, and learn
its syntax with the help of examples with detailed explanations.
Java Continue Statement
The continue statement allows you to skip the rest of the
current iteration in a loop and move to the next iteration. It provides a way
to control the flow of execution within loops. It enables programmers to handle specific conditions gracefully. By utilizing
the continue statement strategically, you can optimize your code and exclude
unnecessary computations or operations.
Syntax and Usage of the Continue Statement
The continue statement can be used within different loop
structures in Java. Let's examine its syntax and usage in each scenario:
1. Continue Statement in For Loop:
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue;
}
System.out.println(i);
}
Output:
1
2
4
5
Explanation:
In this example, the for loop iterates from 1 to 5. When the
value of `i` is equal to 3, the continue statement is encountered. As a result,
the remaining code within the loop for that particular iteration is skipped,
and the loop proceeds to the next iteration. This effectively excludes the
number 3 from being printed.
2. Continue Statement in While Loop:
int i = 1;
while (i <= 5) {
if (i == 3) {
i++;
continue;
}
System.out.println(i);
i++;
}
Output:
1
2
4
5
Explanation:
In this example, the while loop iterates from 1 to 5. When
`i` is equal to 3, the continue statement is encountered. The statement `i++;`
is included to ensure that the loop variable `i` is incremented before
continuing to the next iteration. The subsequent code within the loop for that
particular iteration is skipped, and the loop proceeds to the next iteration.
Practical Applications of the Continue Statement:
1.
Filtering Data in Loops:
In scenarios where you need to filter or process data
selectively, the continue statement can be used to exclude certain data points
or perform specific operations on a subset of the data. This helps in focusing
on relevant data and avoiding unnecessary processing.
Example:
int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
for (int number : numbers) {
if (number % 2 ==
0) {
continue;
}
System.out.println(number);
}
Output:
1
3
5
7
9
Explanation:
In this example, we have an array numbers containing
integers from 1 to 10. We want to filter and print only the odd numbers from
the array. Within the loop, we check if the current number is divisible by 2
(number % 2 == 0). If it is, indicating an even number, the continue statement
is executed. This causes the loop to skip the remaining code for that
particular iteration and move on to the next iteration.
As a result, the System.out.println(number); statement is
only executed when the current number is odd. The continue statement ensures
that even numbers are excluded from the output. Hence, the output displays only
the odd numbers: 1, 3, 5, 7, and 9.
2.
Continue Statement in
Nested Loops:
The continue statement can also be used in nested loop
structures. When encountered, the continue statement skips the current
iteration of the innermost loop and proceeds with the next iteration of that
loop.
Example:
for (int i = 1; i <= 3; i++) {
for (int j = 1; j
<= 3; j++) {
if (i == 2
&& j == 2) {
continue;
}
System.out.println("i = " + i + ", j = " + j);
}
}
Output:
i = 1, j = 1
i = 1, j = 2
i = 1, j = 3
i = 2, j = 1
i = 3, j = 1
i = 3, j = 2
i = 3, j = 3
Explanation:
In this example, we have two nested for loops. The outer
loop iterates from 1 to 3, and the inner loop also iterates from 1 to 3. Within
the nested loops, we encounter the condition if (i == 2 && j == 2).
When this condition evaluates to true, the continue statement is executed.
When i and j both equal 2, the continue statement skips the
remaining code within the inner loop for that particular iteration. Instead of
printing "i = 2, j = 2", the program proceeds to the next iteration
of the inner loop.
As a result, the output skips the combination where i and j
are both 2. All other combinations of i and j within the specified ranges are
printed to the console. The output showcases how the continue statement allows
you to skip specific iterations within nested loops, providing fine-grained
control over the flow of execution
java continue vs break
Following table compares the Java continue statement
and break statement.
continue
Statement |
break
Statement |
|
Purpose |
Skips the
current iteration and proceeds with the next iteration of the loop. |
Terminates
the loop entirely and proceeds with the code after the loop. |
Usage |
Used within
loops to exclude specific iterations based on certain conditions. |
Used within
loops to exit the loop prematurely based on specific conditions. |
Scope |
Affects only
the current iteration within the loop. |
Affects the
entire loop, terminating all subsequent iterations. |
Nested
Loops |
Skips the
current iteration of the innermost loop and continues with the next iteration
of that loop. |
Terminates
the innermost loop entirely and continues with the next iteration of the
outer loop. |
Loop
Control |
Controls the
flow within a loop by skipping certain iterations. |
Controls the
overall flow of the loop by breaking out of the loop prematurely. |
Loop
Continuity |
Continues the
loop execution, skipping the remaining code for the current iteration. |
Ends the loop
execution and proceeds with the code after the loop. |
The Java continue statement provides an efficient means of
controlling loop execution and skipping specific iterations. By strategically
using the continue statement, you can optimize your code, exclude unnecessary
computations, and streamline the execution flow. Understanding the syntax and
behavior of the continue statement empowers you to write clean, efficient, and
concise code. By leveraging the examples and explanations provided in this blog
post, you can effectively utilize the continue statement to enhance your Java
programs and improve their loop control.