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
forloop, control jumps to the update (i++), then the condition. - In a
whileordo-whileloop, control jumps straight to the condition. - In a for-each loop, control moves to the next element.
2. continue in for, for-each and while Loops
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);
}
}
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
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;:
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);
}
}
}
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
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
| Feature | continue | break |
|---|---|---|
| Effect | Skips the rest of the current iteration | Ends the whole loop |
| Loop keeps running? | Yes, with the next iteration | No |
| Allowed in switch | Only when the switch is inside a loop (it continues the loop) | Yes, ends the switch |
| Labeled form | continue outer; | break outer; |
6. Rules and Notes
continuecan only be used inside a loop; elsewhere it is a compile error.- Statements after
continuein the same block are unreachable. - A
finallyblock still runs whencontinueleaves atryblock. - In a lambda passed to
forEach(), usereturnto skip an element, becausecontinueis not allowed there. - Many filters are clearer with streams:
list.stream().filter(e -> e.contains("@")).
7. Summary
continueskips the rest of the current iteration and starts the next one.- In
forloops it jumps to the update; inwhile/do-whileloops to the condition. continue label;starts the next iteration of an outer loop.- Update loop counters before
continuein 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