The continue statement is employed within loop control structures to skip to the next iteration of the loop instantly. It can be utilized in conjunction with either a for loop or a while loop.
How to continue Statement Works?
The Java continue statement is utilized to proceed with the loop, maintaining the current flow of the program and bypassing the remaining code based on a specified condition. When encountered within an inner loop, it exclusively continues the inner loop. Unlike the break statement, the continue statement solely halts the current iteration and then resumes control at the commencement of the subsequent iteration.
Loops Supported by the continue Statement
The Java continue statement is applicable in various loop types including for loops, while loops, and do-while loops.
Syntax of continue Statement
It has the following syntax:
jump-statement;
continue;
Upon execution, the continue statement directs the loop to bypass the remaining code within the current iteration and directly move on to the subsequent iteration. When nested loops are involved, the continue statement influences solely the innermost loop.
Example of continue Statement
In the upcoming illustration, you will see how the continue statement is applied within a for loop. When the variable i reaches 5, the continue statement bypasses the ongoing loop iteration, causing all numbers between 1 and 10 to be displayed except for 5.
Example
public class Main {
public static void main(String[] args) {
// for loop
for (int i = 1; i <= 10; i++) {
if (i == 5) {
// using continue statement
continue;// it will skip the rest statement
}
System.out.println(i);
}
}
}
Output:
1
2
3
4
6
7
8
9
10
Java continue Statement with Inner Loop
The inner loop will persist only if the continue statement is employed within the inner loop.
In the demonstration below, we showcase the implementation of the continue statement within a nested loop. In the scenario where the condition i == 2 and j == 2 is satisfied, the continue statement bypasses that specific iteration of the inner loop. Subsequently, the other iterations proceed as usual, resulting in the printing of all pairs except for 2 2.
Example
public class Main {
public static void main(String[] args) {
// outer loop
for (int i = 1; i <= 3; i++) {
// inner loop
for (int j = 1; j <= 3; j++) {
if (i == 2 && j == 2) {
// using continue statement inside inner loop
continue;
}
System.out.println(i + " " + j);
}
}
}
}
Output:
1 1
1 2
1 3
2 1
2 3
3 1
3 2
3 3
Java continue Statement with Labelled for Loop
The capability to employ a continue statement with a label was integrated into JDK 1.5. As a result, it is now possible to resume execution of any loop in Java, regardless of whether it is an external loop or an internal loop.
In this illustration, we showcase the application of a labeled continue statement. If the condition i equals 2 and j equals 2 evaluates to true, the continue aa statement will bypass the rest of the ongoing loops within the outer loop and proceed to the subsequent iteration of the outer loop. All other combinations will be displayed in the usual manner.
Example
//Java Program to illustrate the use of continue statement with a label inside an inner loop to continue the outer loop
public class Main {
public static void main(String[] args) {
aa: for (int i = 1; i <= 3; i++) {
bb: for (int j = 1; j <= 3; j++) {
if (i == 2 && j == 2) {
// using continue statement with label
continue aa;
}
System.out.println(i + " " + j);
}
}
}
}
Output:
1 1
1 2
1 3
2 1
3 1
3 2
3 3
Java continue Statement with While Loop
The continue statement in conjunction with a while loop allows for the skipping of the remaining code within the current iteration. It facilitates an instant transition to the subsequent iteration of the loop upon the fulfillment of a specified condition.
In this illustration, the utilization of the continue statement within a while loop is showcased. As soon as the variable i reaches the value of 5, the continue statement bypasses that particular iteration, resulting in the exclusion of the number 5 from being printed. Subsequently, all the remaining numbers between 1 and 10 are exhibited.
Example
//Java Program to demonstrate the use of continue statement inside the while loop.
public class Main {
public static void main(String[] args) {
// while loop
int i = 1;
while (i <= 10) {
if (i == 5) {
// using continue statement
i++;
continue;// it will skip the rest statement
}
System.out.println(i);
i++;
}
}
}
Output:
1
2
3
4
6
7
8
9
10
Java continue Statement with do-while Loop
In a do-while loop, the continue statement is employed to bypass the remaining code within the current iteration and advance to the subsequent iteration, despite the loop running at least once prior to evaluating the condition.
In this illustration, we showcase the implementation of the continue statement within a do-while loop. As soon as the value of 'i' reaches 5, the continue statement bypasses that particular iteration, resulting in the exclusion of the number 5 from being printed. Subsequently, all remaining numbers from 1 to 10 are presented in the output.
Example
//Java Program to demonstrate the use of continue statement inside the Java do-while loop.
public class Main {
public static void main(String[] args) {
//declaring variable
int i=1;
//do-while loop
do {
if(i==5) {
//using continue statement
i++;
continue;//it will skip the rest statement
}
System.out.println(i);
i++;
} while(i<=10);
}
}
Output:
1
2
3
4
6
7
8
9
10
Practical Applications of Continue Statement
Numerous real-world uses exist for the continue statement. Here are a few examples:
Filtering Data
An ordinary scenario where the continue statement comes in handy is when filtering data. Let's consider a scenario where there is a collection of numbers, and we wish to display only the odd numbers. By employing the continue statement, we can conveniently bypass the even numbers.
Example
import java.util.Arrays;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
for (int number : numbers) {
if (number % 2 == 0) {
continue; // Skip even numbers
}
System.out.println(number);
}
}
}
Output:
1
3
5
7
9
Advantages of Continue Statement
The continue statement in Java offers numerous benefits. Here are a few of them:
Enhances Clarity: Utilizing this approach can prevent the need for complex nested if-else statements, resulting in a more straightforward and easier-to-understand logic flow.
Adaptable Control Flow: It allows for the effective management of exceptional cases within loops without changing the core logic.
Example:
String[] names = { "Alice", null, "Bob", "Charlie", null, "Dave" };
for (String name : names) {
if (name == null) {
continue; // Skip null values
}
System.out.println(name);
}
// Output: Alice, Bob, Charlie, Dave
Explanation:
In the provided illustration, the utilization of the continue statement within a for-each loop is exemplified. Within the array, certain elements are null. Upon encountering a null element, the continue statement bypasses that specific iteration, ensuring that solely non-null names (Alice, Bob, Charlie, Dave) are displayed.
Beneficial for Filtering: Filtering out undesired data elements can be achieved during iteration without the need for additional flags or lists.
Disadvantages of Continue Statement
There exist various drawbacks associated with the continue statement in Java. A few of these are outlined below:
Diminish Clarity by Excessive Usage: Excessive use of the continue keyword or its application with intricate conditions can lead to a lack of clarity in loop logic, especially in scenarios where multiple continue statements are present.
Example:
for (int i = 1; i <= 10; i++) {
if (i % 2 == 0) continue;
if (i % 3 == 0) continue;
if (i % 5 == 0) continue;
System.out.println(i);
}
// Output: 1, 7
Explanation:
In the code snippet below, we illustrate the usage of multiple continue statements within a for loop. This loop will exclude numbers that are divisible by 2, 3, or 5. It will only print numbers that do not meet these criteria, such as 1 and 7.