Java continue Statement

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.

  • break is wrong here. Good items after the bad one never get processed.
  • A big if around 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 check if (i == 3).
  • When i is 1, 2, 4, or 5, the check is false, so the println runs.
  • When i is 3, the check is true, so continue jumps over println to the next round.
  • Result: 3 never prints, but the loop carries on to 4 and 5.

Output

1
2
4
5

Compare 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 for loop the update always runs.
  • continue jumps to the update step first, then checks the condition, then starts the next pass.
  • So even when i is 3, the i++ still happens, which is why the loop reaches 4 and 5.
  • A for loop with continue can 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 continue jumps 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 i becomes 3, continue jumps over both the println and the i++.
  • So i stays 3, i <= 5 is still true, and we hit the same if again. 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 any continue.
  • The loop ends correctly.
  • Rule to remember: in a while loop, put your update before any continue.

πŸ” 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, continue skips the rest of the pass, so it never reaches total.
  • The loop keeps going for the rest: add 10, skip -5, add 20, skip -8, add 15.
  • In the end total holds 10 + 20 + 15.

Output

Sum of positives: 45

That 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 != 0 is true, the number is odd, so continue skips it.
  • We only add 2, 4, 6, 8, and 10.
  • The for update number++ 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()). The isEmpty method is true when the text has no characters.
  • For the two blank entries, continue skips the greeting.
  • The good names still get processed.

Output

Hello, Alex
Hello, Riya
Hello, Sam

Same 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 continue only 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 continue with 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 col 1, then reaches col 2, where continue rows fires.
  • Instead of moving to col 3, it jumps to the next pass of the outer rows loop.
  • So col 3 never prints, and each row shows only col 1.

Output

row 1, col 1
row 2, col 1
row 3, col 1

A 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:

  • break ends the whole loop. Java leaves it and never comes back.
  • continue ends 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 any continue in a while loop.

  • Mixing up continue and break. Using continue when you meant break keeps 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 if around the code would read more clearly, prefer that. Too many continue jumps 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 3
int 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 forward
int 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 continue inside an obvious if so 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 if is clearer than skipping with continue.
  • 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 for loop, the update part always runs, so continue is safe there.
  • βœ… In a while loop, update the counter before any continue, 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. 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. 2

    How is continue different from break?

    Why: break exits the whole loop, while continue only skips the current pass and keeps looping.

  3. 3

    What is continue best used for?

    Why: continue is ideal for filtering, where you skip certain items but keep processing the others.

  4. 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.

Java Nested Loops

Share & Connect