00:00

continue Statement in Java – Explained with Examples

The continue statement skips the rest of the current loop iteration and jumps straight to the next one. Where break says "stop the loop", continue says "skip this item, carry on with the next". It is handy for ignoring invalid data, skipping special cases, or filtering values while looping.

This tutorial shows how continue behaves in each kind of loop, explains labeled continue for nested loops, and points out the infinite-loop trap in while loops.

1. What Does continue Do?

When continue runs, the remaining statements in the loop body are skipped, and the loop moves on:

  • In a for loop, control jumps to the update (i++), then the condition.
  • In a while or do-while loop, control jumps straight to the condition.
  • In a for-each loop, control moves to the next element.
Flow of a for loop with a continue statement

2. continue in for, for-each and while Loops

ContinueInLoops.java
public class ContinueInLoops {
    public static void main(String[] args) {
        // for: skip odd numbers
        System.out.print("Even numbers: ");
        for (int i = 1; i <= 10; i++) {
            if (i % 2 != 0) continue;        // jump to i++ and the next check
            System.out.print(i + " ");
        }
        System.out.println();

        // for-each: skip invalid entries
        String[] emails = {"asha@mail.com", "", "ravi@mail.com", "not-an-email"};
        int sent = 0;
        for (String e : emails) {
            if (e.isBlank() || !e.contains("@")) {
                System.out.println("skipping invalid: '" + e + "'");
                continue;
            }
            sent++;
            System.out.println("sending to " + e);
        }
        System.out.println(sent + " emails sent");

        // while: update the counter BEFORE continue, or the loop never ends
        int n = 0;
        int sumNotDivBy3 = 0;
        while (n < 10) {
            n++;
            if (n % 3 == 0) continue;
            sumNotDivBy3 += n;
        }
        System.out.println("sum of 1..10 not divisible by 3 = " + sumNotDivBy3);
    }
}
Output
Even numbers: 2 4 6 8 10 
sending to asha@mail.com
skipping invalid: ''
sending to ravi@mail.com
skipping invalid: 'not-an-email'
2 emails sent
sum of 1..10 not divisible by 3 = 37
Watch out in while loops: the counter is updated before continue here. If n++ came after the continue check, then once n reached 3 it would never change again, and the loop would run forever.

3. Labeled continue for Nested Loops

A plain continue affects only the innermost loop. To skip to the next iteration of an outer loop, label it and use continue label;:

LabeledContinue.java
public class LabeledContinue {
    public static void main(String[] args) {
        int[][] orders = {
                {120, 80, 45},
                {300, -1, 60},     // -1 marks a cancelled order: skip the whole customer
                {25, 75, 50}
        };

        customers:
        for (int c = 0; c < orders.length; c++) {
            int total = 0;
            for (int amount : orders[c]) {
                if (amount < 0) {
                    System.out.println("customer " + c + ": cancelled, skipped");
                    continue customers;          // go to the next customer
                }
                total += amount;
            }
            System.out.println("customer " + c + ": total " + total);
        }
    }
}
Output
customer 0: total 245
customer 1: cancelled, skipped
customer 2: total 150

When a cancelled order is found, the rest of that customer's orders and the total printout are skipped, and the outer loop moves on to the next customer.

4. continue as a Guard Clause

Placing continue at the top of a loop body to filter out unwanted items keeps the main logic un-nested and easy to read:

Nested vs guard style
// Nested
for (Order o : orders) {
    if (o.isPaid()) {
        if (!o.isShipped()) {
            ship(o);
        }
    }
}

// With continue as guard clauses
for (Order o : orders) {
    if (!o.isPaid()) continue;
    if (o.isShipped()) continue;
    ship(o);
}

5. continue vs break

Featurecontinuebreak
EffectSkips the rest of the current iterationEnds the whole loop
Loop keeps running?Yes, with the next iterationNo
Allowed in switchOnly when the switch is inside a loop (it continues the loop)Yes, ends the switch
Labeled formcontinue outer;break outer;

6. Rules and Notes

  • continue can only be used inside a loop; elsewhere it is a compile error.
  • Statements after continue in the same block are unreachable.
  • A finally block still runs when continue leaves a try block.
  • In a lambda passed to forEach(), use return to skip an element, because continue is not allowed there.
  • Many filters are clearer with streams: list.stream().filter(e -> e.contains("@")).

7. Summary

  • continue skips the rest of the current iteration and starts the next one.
  • In for loops it jumps to the update; in while/do-while loops to the condition.
  • continue label; starts the next iteration of an outer loop.
  • Update loop counters before continue in while loops to avoid infinite loops.
  • Use it as a guard clause to keep loop bodies flat.

Related Tutorials

Reference: Oracle Java Tutorials: Branching Statements