Introduction
The Java Continue Statement is a control flow statement used to skip the current iteration of a loop and immediately move to the next iteration. Unlike the break statement, which completely terminates a loop, the continue statement allows the loop to continue executing while ignoring the remaining code in the current iteration.
The continue statement is particularly useful when you want to ignore specific values or conditions without stopping the entire loop. It helps simplify loop logic, improves code readability, and avoids unnecessary nested if statements.
For example:
- Skip negative numbers while processing a list of integers.
- Skip weekends while processing business days.
The continue statement is commonly used in Java applications such as data processing systems, games, banking software, inventory management, reporting tools, and input validation programs.
What is Java Continue Statement?
The Java continue statement is a jump statement that skips the remaining statements in the current iteration of a loop and immediately proceeds with the next iteration.
It can be used with:
- for loop
- while loop
- do-while loop
Unlike the break statement, the continue statement does not terminate the loop. Instead, it simply skips the current iteration and allows the loop to continue normally.
- Skips only the current iteration.
- Does not stop the loop.
- Improves code readability.
- Eliminates unnecessary nested conditions.
- Supports labeled continue in nested loops.
Syntax
Continue Statement
continue;
Labeled Continue
continue labelName;
A labeled continue skips the current iteration of an outer loop from within an inner loop.
How Java Continue Statement Works
The execution process is:
- The loop begins.
- Java evaluates the loop condition.
- If a continue statement is encountered:
- The remaining statements in the current iteration are skipped.
- The loop proceeds directly to the next iteration.
- The loop continues until the condition becomes false.
Flow of Java Continue Statement
Start Loop
|
Execute Statements
|
Continue Encountered?
/ \
Yes No
| |
Skip Remaining |
Statements |
| |
Next Iteration |
________|
|
Check Condition
Example 1: Continue in a for Loop
public class Main {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue;
}
System.out.println(i);
}
}
}
Output
2
4
5
Explanation
When i becomes 3, the continue statement skips the System.out.println(i) statement and moves directly to the next iteration.
Example 2: Print Only Odd Numbers
for (int i = 1; i <= 10; i++) {
if (i % 2 == 0) {
continue;
}
System.out.println(i);
}
Output
3
5
7
9
Even numbers are skipped.
Example 3: Continue in a while Loop
int number = 0;
while (number < 5) {
number++;
if (number == 3) {
continue;
}
System.out.println(number);
}
Output
2
4
5
Example 4: Continue in a do-while Loop
int i = 0;
do {
i++;
if (i == 2) {
continue;
}
Sysem.out.println(i);
} while (i < 5);
Output
2
4
5
Example 5: Skip Negative Numbers
int[] numbers = {5, -2, 10, -7, 15};
for (int number : numbers) {
if (number < 0) {
continue;
}
System.out.println(number);
}
Output
10
15
Only positive numbers are processed.
Labeled Continue Statement
A labeled continue skips the current iteration of an outer loop.
Syntax
labelName:
for (...) {
for (...) {
continue labelName;
}
}
Example
outerLoop:
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
if (j == 2) {
continue outerLoop;
}
System.out.println(i + " " + j);
}
}
Output
2 1
3 1
When j becomes 2, Java skips the remaining inner loop and starts the next iteration of the outer loop.
Real-life Examples
1. Skip Inactive Users
boolean[] activeUsers = {true, false, true};
for (boolean active : activeUsers) {
if (!active) {
continue;
}
System.out.println("Processing User");
}
2. Ignore Out-of-Stock Products
int[] stock = {10, 0, 15};
for (int item : stock) {
if (item == 0) {
continue;
}
System.out.println("Available Stock: " + item);
}
3. Skip Weekend Days
String[] days = {
"Monday", "Saturday", "Sunday", "Tuesday"
};
for (String day : days) {
if (day.equals("Saturday") || day.equals("Sunday")) {
continue;
}
System.out.println(day);
}
4. Ignore Invalid Scores
int[] marks = {80, -1, 90, -5};
for (int mark : marks) {
if (mark < 0) {
continue;
}
System.out.println(mark);
}
5. Skip Blank Records
String[] names = {"John", "", "David"};
for (String name : names) {
if (name.isEmpty()) {
continue;
}
System.out.println(name);
}
Advantages of Java Continue Statement
- Makes loop logic cleaner.
- Eliminates unnecessary nested if statements.
- Skips unwanted data efficiently.
- Improves readability.
- Allows loops to continue normally.
- Useful in data filtering and validation.
- Supports labeled continue for nested loops.
Continue vs Break
| Feature | continue | break |
|---|---|---|
| Skips Current Iteration | Yes | No |
| Terminates Loop | No | Yes |
| Continues Remaining Iterations | Yes | No |
| Works in Loops | Yes | Yes |
| Works in switch | No | Yes |
Common Mistakes
1. Using Continue Outside a Loop
Incorrect:
continue;
This causes a compilation error because continue can only be used inside loops.
2. Creating Infinite Loops
Incorrect:
int i = 1;
while (i <= 5) {
if (i == 3) {
continue;
}
i++;
}
The loop becomes infinite because i is never incremented when it equals 3.
Correct:
int i = 1;
while (i <= 5) {
i++;
if (i == 3) {
continue;
}
System.out.println(i);
}
3. Confusing Continue with Break
continue skips the current iteration.
break terminates the loop completely.
4. Overusing Continue
Using too many continue statements can make code difficult to follow. Prefer simple conditions whenever possible.
5. Using Labeled Continue Unnecessarily
Labeled continue should be used only when working with nested loops that require skipping an outer loop iteration.
Best Practices
- Use continue only when it improves readability.
- Keep loop logic simple.
- Update loop variables before using continue in while and do-while loops.
- Avoid excessive labeled continue statements.
- Use meaningful variable names.
- Test loops carefully to prevent infinite execution.
When Should You Use Continue?
Use the continue statement when:
- Skipping invalid records.
- Ignoring negative values.
- Filtering user input.
- Processing only active users.
- Skipping weekends in scheduling applications.
- Ignoring out-of-stock products.
- Filtering arrays and collections.
Conclusion
The Java continue statement is a useful control flow statement that allows you to skip the current iteration of a loop without terminating it. It helps create cleaner, more efficient, and more readable code by eliminating unnecessary nested conditions and allowing loops to process only the required data.
Mastering the continue statement will improve your ability to write efficient loop logic in Java applications. As you continue learning Java, explore related topics such as the break statement, for loop, while loop, do-while loop, and switch statement to gain a deeper understanding of Java control flow and program execution.