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
Frequently Asked Questions (FAQ)
Q1. What is the continue statement in Java?
A statement used inside loops that skips the remaining code in the current iteration and moves on to the next iteration.
Q2. What is the difference between break and continue?
break ends the loop completely; continue ends only the current iteration, and the loop keeps running.
Q3. Where does control go after continue in a for loop?
To the update expression (for example i++), and then to the condition check.
Q4. Where does control go after continue in a while loop?
Directly to the condition check. That is why the loop variable must be updated before continue, or the loop may never end.
Q5. Why does my while loop with continue run forever?
The statement that changes the loop variable is placed after continue, so it is skipped whenever continue runs. Move the update before the continue check.
Q6. What is a labeled continue?
continue label; skips to the next iteration of the loop marked with that label, typically an outer loop, instead of the innermost one.
Q7. Can continue be used in a switch?
Only if the switch is inside a loop; then continue skips to the next iteration of that loop. On its own, a switch does not accept continue.
Q8. Can I use continue in a for-each loop?
Yes. It moves on to the next element of the array or collection.
Q9. Can continue be used inside a lambda?
No. Inside a lambda passed to forEach(), use return; to end processing of the current element.
Q10. Is using continue bad practice?
No. Used as a guard at the top of a loop, it makes code flatter and clearer. Many scattered continue statements in a long loop, however, make the flow hard to follow.
Q11. Can continue be used outside a loop?
No. The compiler reports "continue outside of loop".
Q12. Does finally run when continue is executed in a try block?
Yes. The finally block runs before the loop moves to its next iteration.
Q13. Can I write statements after continue?
Not in the same block: they would be unreachable, and the compiler rejects them. Statements in other branches (such as the rest of the loop after an if) are fine.
Q14. How do I skip even numbers in a loop?
for (int i = 1; i <= 10; i++) { if (i % 2 == 0) continue; System.out.println(i); } prints only the odd numbers.
Q15. Does continue work in do-while loops?
Yes. It jumps to the while (condition) check at the end of the loop.
Q16. What can I use instead of continue?
An if that wraps the rest of the body, a helper method that returns early, or a stream filter() that removes unwanted items before processing.
Q17. Can a label be used with continue on a non-loop block?
No. A labeled continue must refer to a loop. Labeled blocks can only be used with break.
Q18. Does continue affect performance?
It can save work by skipping unnecessary computations for items you do not need, but it does not make the loop itself faster or slower in any meaningful way.
Q19. Is there a goto-like behaviour in continue?
Labeled continue is a restricted, structured jump: it can only go to the start of an enclosing loop's next iteration, never to an arbitrary place.
Q20. How is continue different from return inside a loop?
continue moves on to the next iteration; return leaves the entire method, ending the loop and everything after it.