⏱️ 6 min read • Beginner Level • Lesson 31
Completed on . You can revise this lesson or continue to the next topic.
The continue statement in Java is used to skip the current iteration of a loop and move directly to the next iteration.
Before learning continue, you should understand
Java for loop,
Java while loop,
Java do-while loop, and
Java break statement.
In Java, the continue statement skips the remaining code of the current
loop iteration and moves control to the next iteration.
continue does not stop the entire loop.
It skips only the current iteration.
It is useful when you want to ignore some values and continue processing others.
continue;
continue, it skips the remaining statements of that iteration.The following flowchart shows how the continue statement skips the current iteration and moves directly to the next iteration of the loop.
This example skips printing number 3.
public class ContinueInForLoop {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue;
}
System.out.println(i);
}
}
}
Output:
1 2 4 5
1 and runs up to 5.i becomes 3, the condition i == 3 becomes true.continue statement executes.System.out.println(i) for that iteration.3 is not printed.
In a while loop, update the counter before continue.
Otherwise, the loop may become infinite.
public class ContinueInWhileLoop {
public static void main(String[] args) {
int i = 0;
while (i < 5) {
i++;
if (i == 3) {
continue;
}
System.out.println(i);
}
}
}
Output:
1 2 4 5
i starts at 0.i before checking the condition.i becomes 3, the continue statement executes.This program prints only odd numbers by skipping even numbers.
public class SkipEvenNumbers {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
if (i % 2 == 0) {
continue;
}
System.out.println(i);
}
}
}
Output:
1 3 5 7 9
1 to 10.i % 2 == 0 checks whether the number is even.continue skips that iteration.This example skips blocked users and processes only active users.
public class SkipBlockedUsers {
public static void main(String[] args) {
String[] users = {"Ayan", "Blocked", "Sarah", "John"};
for (String user : users) {
if (user.equals("Blocked")) {
continue;
}
System.out.println("Processing user: " + user);
}
}
}
Output:
Processing user: Ayan Processing user: Sarah Processing user: John
In nested loops, a normal continue skips only the current iteration
of the nearest inner loop.
public class ContinueNestedLoop {
public static void main(String[] args) {
for (int row = 1; row <= 2; row++) {
for (int col = 1; col <= 3; col++) {
if (col == 2) {
continue;
}
System.out.println("row = " + row + ", col = " + col);
}
}
}
}
Output:
row = 1, col = 1 row = 1, col = 3 row = 2, col = 1 row = 2, col = 3
| break | continue |
|---|---|
| Stops the loop completely. | Skips only the current iteration. |
| Control moves outside the loop. | Control moves to the next iteration. |
| Can be used in loops and switch. | Used only in loops. |
continue outside a loop.continue inside switch without a loop.continue in a while loop.continue when break is actually needed.What is the difference between break and continue in Java?
break terminates the loop completely, while continue skips only the current iteration and continues with the next iteration.
continue statement skips the current loop iteration.continue affects the nearest loop.continue when you want to skip and move to the next iteration.🧠 Test your understanding with a quick quiz
Topic: Continue | Language: Java
for (int i = 1; i <= 6; i++) {
if (i % 2 == 0) {
continue;
}
System.out.println(i);
}for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue;
}
System.out.println(i);
}🎉 Great job! Continue learning Java step by step.
Mark this lesson as completed to track your learning progress. To keep track across devices, please login and save your learning progress.
Discussion
Ask questions, share suggestions, or discuss this lesson.