Java continue Statement
Table of Contents + β
In the last lesson you learned about the Java break statement, which stops a loop completely.
But sometimes you only want to skip one item and keep going with the rest, like skipping the negative numbers in a list. For that, Java has the continue statement.
π€ Why skip just one pass?
The problem: you have a list where some items are no good. Negative numbers, blank names, broken records. You still want to look at every item, but do nothing for the bad ones.
breakis wrong here. Good items after the bad one never get processed.- A big
ifaround the good code works, but it pushes your real logic deeper. - The clean way: say βthis one does not qualify, skip itβ and the loop carries on.
This fits jobs like these:
- Print all numbers except the ones that are zero.
- Add up only the passing marks, skip the failing ones.
- Process every order except the cancelled ones.
- Read a file and skip the blank lines.
That is what continue does. It skips the rest of this pass and goes straight to the next one.
π§© How continue works
continue is one word, like break, but it behaves very differently:
- It does not end the loop.
- It skips whatever is left in the current pass.
- It jumps straight to the next round.
The smallest example. This loop counts from 1 to 5 but skips the number 3.
for (int i = 1; i <= 5; i++) { if (i == 3) { continue; // β
skip the rest of this pass when i is 3 } System.out.println(i);}Reading it top to bottom:
- For each
i, we checkif (i == 3). - When
iis 1, 2, 4, or 5, the check is false, so theprintlnruns. - When
iis 3, the check is true, socontinuejumps overprintlnto the next round. - Result: 3 never prints, but the loop carries on to 4 and 5.
Output
1245Compare with break: break would stop at 3 and show only 1 and 2. continue keeps going and just misses one value.
π The update part still runs (for loops)
A for loop has three parts: the start (int i = 1), the condition (i <= 5), and the update (i++). Does continue skip the update too? No.
- In a
forloop the update always runs. continuejumps to the update step first, then checks the condition, then starts the next pass.- So even when
iis 3, thei++still happens, which is why the loop reaches 4 and 5. - A
forloop withcontinuecan never get stuck. The counter keeps moving on its own.
for (int i = 1; i <= 5; i++) { if (i == 3) { continue; // β
i++ still runs, so the loop moves to 4 } System.out.println("Pass: " + i);}Think of continue in a for loop like a referee waving a runner past one checkpoint. The runner skips the task but keeps moving down the track.
β οΈ while loops need extra care
A while loop is different. It has no built-in update step, so you write the counter update yourself inside the body. That creates a trap:
- If
continuejumps over the update line, the counter never changes. - If the counter never changes, the condition never becomes false.
- The loop runs forever. That is an infinite loop, which means the program never finishes.
The broken version first, so the danger is clear.
int i = 1;while (i <= 5) { if (i == 3) { continue; // β skips i++ below, so i stays 3 forever } System.out.println(i); i++; // never reached when i is 3}- When
ibecomes 3,continuejumps over both theprintlnand thei++. - So
istays 3,i <= 5is still true, and we hit the sameifagain. Forever. - The program prints 1 and 2, then freezes.
The fix: update the counter before the continue, so the update always runs.
int i = 0;while (i < 5) { i++; // β
update first, before any continue if (i == 3) { continue; // safe now, because i was already increased } System.out.println(i);}i++now runs at the top of every pass, so the counter moves before anycontinue.- The loop ends correctly.
- Rule to remember: in a
whileloop, put your update before anycontinue.
π A real filtering example: sum only the positives
The best use of continue is filtering. Letβs add up only the positive numbers in an array and skip the negatives.
public class SumPositives { public static void main(String[] args) { int[] numbers = {10, -5, 20, -8, 15}; int total = 0;
for (int number : numbers) { if (number < 0) { continue; // β
skip negative numbers } total = total + number; }
System.out.println("Sum of positives: " + total); }}Walking through it:
- For each number we check
if (number < 0). - If it is negative,
continueskips the rest of the pass, so it never reachestotal. - The loop keeps going for the rest: add 10, skip -5, add 20, skip -8, add 15.
- In the end
totalholds 10 + 20 + 15.
Output
Sum of positives: 45That is the whole pattern: check the bad case early, skip it with continue, and let the body run on good values only.
π’ Another example: sum only the even numbers
One more, because the pattern is so common. We count from 1 to 10 and add up only the even numbers.
A number is even when the remainder after dividing by 2 is zero. In Java the % operator gives that remainder, so number % 2 is 0 for even numbers and 1 for odd ones.
public class SumEvens { public static void main(String[] args) { int total = 0;
for (int number = 1; number <= 10; number++) { if (number % 2 != 0) { continue; // β
skip odd numbers } total = total + number; }
System.out.println("Sum of evens from 1 to 10: " + total); }}- If
number % 2 != 0is true, the number is odd, socontinueskips it. - We only add 2, 4, 6, 8, and 10.
- The
forupdatenumber++still runs each pass, so the counter keeps climbing safely.
Output
Sum of evens from 1 to 10: 30π§Ή Skipping invalid input
Filtering is not only about numbers. A common job is reading records and skipping the broken or blank ones. Here we greet every real name and skip the blanks.
public class GreetNames { public static void main(String[] args) { String[] names = {"Alex", "", "Riya", "", "Sam"};
for (String name : names) { if (name.isEmpty()) { continue; // β
skip blank entries } System.out.println("Hello, " + name); } }}- We check
if (name.isEmpty()). TheisEmptymethod is true when the text has no characters. - For the two blank entries,
continueskips the greeting. - The good names still get processed.
Output
Hello, AlexHello, RiyaHello, SamSame idea as skipping negatives: test the bad case first, skip it, keep the loop running for the rest.
π·οΈ Labeled continue with nested loops
When you have a loop inside another loop:
- A plain
continueonly skips the current pass of the inner loop. It does not touch the outer loop. - To skip to the next pass of the outer loop, put a label on it and write
continuewith that label. - A label is just a name before a loop, followed by a colon. Here we name the outer loop
rows.
public class LabeledContinue { public static void main(String[] args) { rows: for (int row = 1; row <= 3; row++) { for (int col = 1; col <= 3; col++) { if (col == 2) { continue rows; // β
jump to the next row, skip the rest of this row } System.out.println("row " + row + ", col " + col); } } }}- The inner loop prints
col1, then reachescol2, wherecontinue rowsfires. - Instead of moving to
col3, it jumps to the next pass of the outerrowsloop. - So
col3 never prints, and each row shows onlycol1.
Output
row 1, col 1row 2, col 1row 3, col 1A plain continue would only skip to the next col. The labeled continue rows skips the rest of the inner loop and moves the outer loop forward.
Use it only when you truly need to control the outer loop, since too many labels make code hard to read.
π continue vs break: the key difference
These two keywords look similar but do opposite things:
breakends the whole loop. Java leaves it and never comes back.continueends only the current pass. Java skips this round and starts the next one.
Think of a line of people each handing you an item. break means βI am done, send everyone homeβ. continue means βskip this one, send me the next personβ.
| Question | break | continue |
|---|---|---|
| Does the loop keep running? | No, the loop stops | Yes, the loop keeps going |
| What gets skipped? | Every remaining pass | Only the rest of this pass |
| Where does Java go next? | The line after the loop | The next pass of the loop |
| Typical use | Stop a search once you found it | Skip items that do not qualify |
A simple way to remember
break = leave the loop. continue = skip to the next pass. If you ever mix them up, ask yourself: do I want to stop entirely, or just skip this one item?
β οΈ Common Mistakes
Slip-ups to watch for:
-
Causing an infinite loop in a while loop. If your counter update sits after the
continue, it gets skipped. The condition never changes and the loop runs forever. Always update the counter before anycontinuein awhileloop. -
Mixing up continue and break. Using
continuewhen you meantbreakkeeps the loop running when you wanted it to stop, and the other way round. Always pick the one that matches your intent. -
Overusing continue. If a simple
ifaround the code would read more clearly, prefer that. Too manycontinuejumps can make a loop harder to follow.
The infinite-loop trap, side by side.
// β Wrong: i++ comes after continue, so i never changes when it is 3int i = 1;while (i <= 5) { if (i == 3) { continue; // jumps over i++, loop freezes here } System.out.println(i); i++;}
// β
Right: i++ runs first, so the loop always moves forwardint j = 0;while (j < 5) { j++; if (j == 3) { continue; // safe, j was already increased } System.out.println(j);}β Best Practices
Habits for using continue well:
- Use it to skip items that do not qualify. Filtering out values you do not care about is its main job.
- Keep the skip condition clear. Put
continueinside an obviousifso readers see exactly what gets skipped. - Watch the counter in while loops. Make sure the update runs before any
continue, or the loop may never end. - Prefer a plain if when it reads better. Sometimes wrapping the body in an
ifis clearer than skipping withcontinue. - Use labeled continue sparingly. Reach for it only when you truly need to move the outer loop forward, and keep the label name meaningful.
π§© What Youβve Learned
- β The continue statement skips the rest of the current loop pass and jumps to the next one.
- β
Unlike
break, it does not end the loop. The loop keeps running. - β
In a
forloop, the update part always runs, socontinueis safe there. - β
In a
whileloop, update the counter before anycontinue, or you get an infinite loop. - β It is perfect for filtering, like skipping negatives, odds, or blank entries.
- β Labeled continue lets you skip to the next pass of an outer loop in nested loops.
- β Remember the difference: break leaves the loop, continue skips one pass.
Check Your Knowledge
Test what you learned. Pick an answer for each question, then click Check.
- 1
What does the continue statement do?
Why: continue skips the remaining code in the current pass and moves on to the next iteration.
- 2
How is continue different from break?
Why: break exits the whole loop, while continue only skips the current pass and keeps looping.
- 3
What is continue best used for?
Why: continue is ideal for filtering, where you skip certain items but keep processing the others.
- 4
Why can continue cause an infinite loop in a while loop?
Why: If continue skips over the counter update in a while loop, the condition never changes and the loop runs forever.
π Whatβs Next?
You now know how to run loops, stop them, and skip passes. Up next we learn nested loops, where one loop runs inside another. They are the key to working with grids, tables, and patterns.