Lesson 27 of 50 – Java Continue Statement
54%

Java Continue Statement

The continue statement in Java is used to skip the remaining statements in the current loop iteration and move to the next iteration.

1. What Is the Continue Statement?

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
The continue statement skips printing 3, but the loop continues with the next iteration.
2. Syntax of the Continue Statement
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.

3. Continue Statement in a For Loop

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.

4. Continue Statement in a While Loop

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.

5. Continue Statement in a Do-While Loop

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.

6. Print Only Even Numbers

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.

7. Print Only Odd Numbers

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.

8. Skip a Specific Word in an Array

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.

9. Skip Multiples of 3

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.

10. Continue with User Input

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.

11. Continue in Nested Loops

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.

12. Difference Between Break and Continue
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

13. Practice Assignments

Write Java programs to complete these assignments.

  1. Print numbers from 1 to 20 but skip 10.
  2. Print even numbers from 1 to 50 using continue.
  3. Print odd numbers from 1 to 50 using continue.
  4. Print numbers from 1 to 100, skipping multiples of 5.
  5. Print student names except one selected name.
  6. Skip negative numbers entered by the user.
  7. Print numbers from 1 to 30 but skip multiples of 3.
  8. Use continue in a while loop to skip a specific number.
  9. Use continue in a nested loop to skip a value of j.
  10. Compare the output of break and continue with the same loop.
Tip: Use continue when you want to skip one iteration without stopping the entire loop.

🧠 Quick Quiz

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?