continue Keyword in Java
The continue keyword is used to skip the current iteration of a loop (for, while, or do-while) and proceed to the next iteration. It is particularly useful for controlling the flow of loops when certain conditions are met, allowing for more readable and efficient code.
Usage
The continue statement can be used within any loop structure to skip the remainder of the code inside the loop for the current iteration and move to the next iteration.
Syntax
continue;
Examples
Example 1: Using continue in a for Loop
public class ContinueExample {
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
if (i % 2 == 0) {
continue; // Skip even numbers
}
System.out.println(i);
}
}
}
In this example, the continue statement is used to skip the even numbers in a for loop. When i is even, the continue statement is executed, and the loop proceeds to the next iteration without printing the value of i.
Example 2: Using continue in a while Loop
public class ContinueWhileExample {
public static void main(String[] args) {
int i = 0;
while (i < 10) {
i++;
if (i % 2 == 0) {
continue; // Skip even numbers
}
System.out.println(i);
}
}
}
Here, the continue statement is used within a while loop to skip the even numbers. When i is even, the continue statement is executed, and the loop proceeds to the next iteration without printing the value of i.
Example 3: Using continue in a Nested Loop
public class ContinueNestedLoopExample {
public static void main(String[] args) {
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
if (j == 2) {
continue; // Skip when j is 2
}
System.out.println("i = " + i + ", j = " + j);
}
}
}
}
In this nested loop example, the continue statement is used to skip the iteration when j equals 2. This results in the inner loop skipping the print statement whenever j is 2, but continuing with the next iteration.
Additional Example: Using continue with Labelled For Loop
public class ContinueLabelExample {
public static void main(String[] args) {
outerLoop:
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
if (j == 2) {
continue outerLoop; // Continue outer loop when j is 2
}
System.out.println("i = " + i + ", j = " + j);
}
}
}
}
In this example, the continue statement is used with a label to skip the current iteration of the outer loop when j equals 2. This control flow allows more complex loop structures to be managed effectively.
Tips and Best Practices
- Use Sparingly: Overusing
continuecan make your code harder to read and understand. Use it sparingly and only when it makes the logic clearer. - Avoid Complex Conditions: Try to avoid using
continuewith complex conditions as it can make the loop logic difficult to follow. - Nested Loops: Be cautious when using
continuein nested loops. It only affects the innermost loop in which it is used. - Alternative Logic: Consider if there is a more straightforward way to achieve the same result without using
continue. Sometimes restructuring the loop logic can eliminate the need forcontinue.