continue Statement in Java
The continue statement is used to skip the current iteration of a loop and move to the next cycle.
Unlike break, it does not exit the loop — it just jumps to the next iteration.
Use Cases:
- To skip certain values or inputs during looping.
- Useful when you want to ignore specific conditions temporarily.
Example 1: Using continue in a for loop
class ShikshaSanchar {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
if (i == 3) continue;
System.out.println(i);
}
}
}
Output:
1
2
3
4
5
Explanation:
- The loop runs from 1 to 5.
- When i == 3, continue skips the rest of that iteration.
- So, 3 is not printed.
Example 2: Using continue in a while loop
class ShikshaSanchar {
public static void main(String[] args) {
int i = 0;
while (i < 5) {
i++;
if (i == 3) continue;
System.out.println(i);
}
}
}
Output:
1
2
3
4
5
Explanation:
- Loop runs while i < 5.
- When i == 3, continue skips the print and jumps to next iteration.
- Only 3 is skipped.
Example 3: Using continue in a do-while loop
class ShikshaSanchar {
public static void main(String[] args) {
int i = 0;
do {
i++;
if (i == 3) continue;
System.out.println(i);
} while (i < 5);
}
}
Output:
1
2
3
4
5
Explanation:
- Loop runs at least once due to do-while.
- When i == 3, continue skips printing.
- Be careful: if i++ is placed after continue, it may create an infinite loop.
Example 4: Using continue in a for-each loop
class ShikshaSanchar {
public static void main(String[] args) {
String[] names = {"Devanshi", "Yash", "Vansh", "Angel"};
for (String name : names) {
if (name.equals("Vansh")) continue;
System.out.println(name);
}
}
}
Output:
Devanshi
Yash
Angel
Explanation:
- Loop iterates through the names.
- When name is "Vansh", continue skips the print.
- Rest of the names are printed normally.
Important Notes:
- continue skips the current loop iteration only.
- It works in all loop types: for, while, do-while, and for-each.
- Always make sure to not skip loop variable updates, especially in while or do-while.
Summary:
- The continue statement is used to skip the current iteration of a loop.
- It does not terminate the loop — just jumps to the next iteration.
- Useful when a specific condition should be ignored temporarily.
- Works in all loop types: for, while, do-while, and for-each.
- Be careful in while and do-while to ensure loop counter (i++, etc.) isn't skipped.