The continue statement in Java is used to skip the remaining statements in the current loop iteration and move to the next iteration.
Suppose you want to print numbers from 1 to 5 but skip the number 3. You can use the continue statement.
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue;
}
System.out.println(i);
}
Output:
1
2
4
5
continue;
Example:
for (int i = 1; i <= 5; i++) {
if (i == 2) {
continue;
}
System.out.println(i);
}
Output:
1
3
4
5
When i == 2, Java skips the print statement
for that iteration.
Print numbers from 1 to 10, excluding 5.
public class Main {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
if (i == 5) {
continue;
}
System.out.println(i);
}
}
}
Output:
1
2
3
4
6
7
8
9
10
In a for loop, continue skips the rest of the current iteration and then executes the update expression.
When using continue in a while loop, make sure the loop variable is updated before the statement is reached, or the loop may run forever.
int i = 0;
while (i < 5) {
i++;
if (i == 3) {
continue;
}
System.out.println(i);
}
Output:
1
2
4
5
The variable is incremented before continue is reached, so the loop can still make progress.
In a do-while loop, continue transfers control to the condition check at the end of the loop.
int i = 0;
do {
i++;
if (i == 3) {
continue;
}
System.out.println(i);
} while (i < 5);
Output:
1
2
4
5
When i becomes 3, the print statement is skipped. The loop condition is then checked.
You can use continue to skip odd numbers and print only even numbers.
for (int i = 1; i <= 10; i++) {
if (i % 2 != 0) {
continue;
}
System.out.println(i);
}
Output:
2
4
6
8
10
The expression i % 2 gives the remainder
after division by 2. Odd numbers have a remainder of 1.
Skip even numbers and print only odd numbers.
for (int i = 1; i <= 10; i++) {
if (i % 2 == 0) {
continue;
}
System.out.println(i);
}
Output:
1
3
5
7
9
When a number is divisible by 2, the condition is true and continue skips that iteration.
Print student names except the name "Rahul".
public class Main {
public static void main(String[] args) {
String[] names = {"Amit", "Rahul", "Priya", "Neha"};
for (int i = 0; i < names.length; i++) {
if (names[i].equals("Rahul")) {
continue;
}
System.out.println(names[i]);
}
}
}
Output:
Amit
Priya
Neha
The continue statement skips the iteration when the current name is Rahul.
Print numbers from 1 to 20 but skip multiples of 3.
for (int i = 1; i <= 20; i++) {
if (i % 3 == 0) {
continue;
}
System.out.println(i);
}
Output:
1
2
4
5
7
8
10
11
13
14
16
17
19
20
Multiples of 3, such as 3, 6, 9, 12, 15, and 18, are skipped.
This program skips negative numbers and displays only non-negative numbers entered by the user.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
for (int i = 1; i <= 5; i++) {
System.out.print("Enter a number: ");
int number = sc.nextInt();
if (number < 0) {
System.out.println("Negative number skipped.");
continue;
}
System.out.println("Accepted: " + number);
}
sc.close();
}
}
The loop asks for five inputs. A negative number is skipped, but the next input is still requested.
A continue statement without a label affects the nearest enclosing loop.
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
if (j == 2) {
continue;
}
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
The inner loop skips the iteration where j equals 2. The outer loop continues normally.
| Break | Continue |
|---|---|
| Terminates the nearest enclosing loop. | Skips the current iteration. |
| No further iterations of that loop occur. | The loop proceeds to the next iteration if allowed. |
| Useful when a search should stop after finding an item. | Useful when certain values should be skipped. |
Break example:
for (int i = 1; i <= 5; i++) {
if (i == 3) {
break;
}
System.out.println(i);
}
Output: 1 2
Continue example:
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue;
}
System.out.println(i);
}
Output: 1 2 4 5
Write Java programs to complete these assignments.
1. What does the continue statement do?
2. What is the output of this code?
for (int i = 1; i <= 4; i++) {
if (i == 2) {
continue;
}
System.out.println(i);
}
3. Which statement completely exits the nearest enclosing loop?