ProgramGuru

Continue

Continue

A school program needs reliable data and clear rules. Continue skips the rest of the current iteration. A report can ignore an absent student while still processing later roll numbers.

  1. Understand

    Continue skips the rest of the current iteration.

  2. Apply

    Trace the Java example, predict its output and explain the result before running it.

  3. Avoid mistakes

    In a for loop the update still happens. In a while loop an update placed after continue is skipped.

Continue

In Java, the continue statement is used to skip the current iteration of a loop and move to the next iteration. It does not terminate the loop entirely (unlike break) — instead, it simply jumps over the remaining statements inside the loop body for that iteration.

When Should You Use continue ?

You use continue when you want to ignore specific cases inside loops but still allow the loop to run. It's especially helpful when filtering data, skipping invalid inputs, or jumping over certain conditions without cluttering your loop logic.

Syntax of continue in Java

continue;

It’s that simple — just the word continue followed by a semicolon. Let’s bring it to life with examples.

Example 1: Skipping Even Numbers

public class ContinueExample1 {
    public static void main(String[] args) {
        for (int i = 1; i <= 10; i++) {
            if (i % 2 == 0) {
                continue;
            }
            System.out.println("Odd number: " + i);
        }
    }
}
Odd number: 1
Odd number: 3
Odd number: 5
Odd number: 7
Odd number: 9

Explanation

Here, we loop from 1 to 10. When the number is even, the continue statement is triggered — the print statement is skipped, and the loop moves to the next number.

Example 2: Using continue in a While Loop

public class ContinueExample2 {
    public static void main(String[] args) {
        int i = 0;
        while (i < 6) {
            i++;
            if (i == 3) {
                continue;
            }
            System.out.println("i = " + i);
        }
    }
}
i = 1
i = 2
i = 4
i = 5
i = 6

Explanation

When i == 3, the continue skips the System.out.println() and moves directly to the next loop cycle. That's why 3 is missing in the output.

Example 3: continue with Do-While Loop

public class ContinueExample3 {
    public static void main(String[] args) {
        int i = 0;
        do {
            i++;
            if (i == 2 || i == 4) {
                continue;
            }
            System.out.println("Value: " + i);
        } while (i < 5);
    }
}
Value: 1
Value: 3
Value: 5

Explanation

This example shows that continue also works inside do-while loops. Notice how 2 and 4 are skipped.

Common Use Cases

  • Skip invalid or unwanted values in a list or array
  • Ignore specific characters in a string processing loop
  • Prevent certain cases from being logged or printed

Points to Remember

  • continue skips only the current iteration, not the entire loop.
  • It's useful in all loop types — for, while, and do-while.
  • Use it to simplify logic by avoiding nested if-else structures.

Quick Comparison: continue vs break

Keyword Effect Use Case
continue Skips current loop iteration Filter or ignore certain values
break Exits the loop completely Stop processing when a condition is met

Important boundaries and mistakes

In a for loop the update still happens. In a while loop an update placed after continue is skipped.

Try the school example

Before running this Java program, write down each line you expect to see. Identify the value, branch, object or exception responsible for each line. Then explain whether the following assumption is valid: In a for loop the update still happens.

Java 17 · Main.java · complete practice program

public class Main  {
  public static void main(String[] args)  {
    for (int roll = 1; roll <= 3; roll++)  {
      if (roll == 2) continue;
      System.out.println(roll);
    }
  }
}

Now change one thing: Skip roll number 1 instead. Predict the new behavior before checking the worked variation.

Worked answer and reasoning

Java 17 · Main.java · source repeated for this answer

public class Main  {
  public static void main(String[] args)  {
    for (int roll = 1; roll <= 3; roll++)  {
      if (roll == 2) continue;
      System.out.println(roll);
    }
  }
}

Expected output

1
3

Continue skips the rest of the current iteration. A report can ignore an absent student while still processing later roll numbers.

In a for loop the update still happens. In a while loop an update placed after continue is skipped.

Run with javac Main.java, then java Main. The declaration of Main names the class; main starts the example, and println writes a complete output line.

Worked variation: Skip roll number 1 instead. Roll numbers 2 and 3 print.

Check your understanding

For the complete practice program in this lesson, which output is expected on Java 17?
Choose one answer. You can try again.

Answer and explanation

1 3

Continue skips the rest of the current iteration. A report can ignore an absent student while still processing later roll numbers. In a for loop the update still happens. In a while loop an update placed after continue is skipped.

What to remember

Continue skips the rest of the current iteration. A report can ignore an absent student while still processing later roll numbers. In a for loop the update still happens. In a while loop an update placed after continue is skipped.

Related explanations: Java Variables, Java If Statement - Syntax and Examples, Java Class - Blueprint of Objects, Exceptions in Java Introduction.

Supporting documentation: Official Java learning guide and Java 17 language specification.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Oop Introduction →