Introduction
The continue
statement in Java is a control flow statement that is used to skip the current iteration of a loop and proceed with the next iteration. It can be particularly useful when you want to skip certain iterations based on specific conditions, without exiting the entire loop. In this chapter, we will explore the syntax, usage, and examples of the continue
statement in Java.
Syntax
The basic syntax of the continue
statement is as follows:
continue;
Key Points:
- The
continue
statement can be used inside loops (for
,while
,do-while
). - When the
continue
statement is executed, the loop’s current iteration is terminated, and control moves to the next iteration of the loop.
Using continue in Loops
Example: Using continue in a for Loop
Let’s consider an example where we use the continue
statement to skip certain iterations in a for
loop.
Example Code:
public class ContinueForLoopExample {
public static void main(String[] args) {
// Loop from 1 to 10
for (int i = 1; i <= 10; i++) {
// Skip the iteration if i equals 5
if (i == 5) {
continue;
}
// Print the current value of i
System.out.println("i: " + i);
}
// This statement is executed after the loop terminates
System.out.println("Loop completed.");
}
}
Output:
i: 1
i: 2
i: 3
i: 4
i: 6
i: 7
i: 8
i: 9
i: 10
Loop completed.
Example: Using continue in a while Loop
Let’s consider an example where we use the continue
statement to skip certain iterations in a while
loop.
Example Code:
public class ContinueWhileLoopExample {
public static void main(String[] args) {
int count = 1;
// Loop while count is less than or equal to 10
while (count <= 10) {
// Skip the iteration if count equals 5
if (count == 5) {
count++;
continue;
}
// Print the current value of count
System.out.println("Count: " + count);
count++;
}
// This statement is executed after the loop terminates
System.out.println("Loop completed.");
}
}
Output:
Count: 1
Count: 2
Count: 3
Count: 4
Count: 6
Count: 7
Count: 8
Count: 9
Count: 10
Loop completed.
Example: Using continue in a do-while Loop
Let’s consider an example where we use the continue
statement to skip certain iterations in a do-while
loop.
Example Code:
public class ContinueDoWhileLoopExample {
public static void main(String[] args) {
int count = 1;
// Loop at least once, then while count is less than or equal to 10
do {
// Skip the iteration if count equals 5
if (count == 5) {
count++;
continue;
}
// Print the current value of count
System.out.println("Count: " + count);
count++;
} while (count <= 10);
// This statement is executed after the loop terminates
System.out.println("Loop completed.");
}
}
Output:
Count: 1
Count: 2
Count: 3
Count: 4
Count: 6
Count: 7
Count: 8
Count: 9
Count: 10
Loop completed.
Using continue in Nested Loops
The continue
statement can also be used in nested loops to skip iterations of the inner loop based on a condition.
Example Code:
public class ContinueNestedLoopExample {
public static void main(String[] args) {
// Outer loop iterates from 1 to 3
for (int i = 1; i <= 3; i++) {
// Inner loop iterates from 1 to 3
for (int j = 1; j <= 3; j++) {
// Skip the iteration if j equals 2
if (j == 2) {
continue;
}
// Print the current values of i and j
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
Conclusion
The continue
statement is a powerful control flow statement in Java that allows you to skip the current iteration of a loop and proceed with the next iteration. It enhances the flexibility of your code by providing a way to skip certain iterations based on specific conditions, without exiting the entire loop. Understanding how to use the continue
statement effectively can help you write more efficient and readable Java programs.