Java Tutorial
🔍

Java continue Statement

Java continue Statement

The continue statement skips the rest of the current loop iteration and moves directly to the next one. The loop does not exit — it keeps running. Only the remaining code in the current pass is skipped.

The distinction between continue and break is one of the most tested Java control flow questions, and the confusion is understandable — both appear inside loops, both change the normal execution path, and both are triggered by an if condition. What separates them is the outcome: break ends the loop, continue preserves it while skipping one pass.

What Is the Java continue Statement?

The continue statement transfers control to the update expression in a for loop, or directly to the condition check in a while or do-while loop, bypassing all remaining statements in the current iteration body.

Where break says "stop the loop entirely," continue says "skip this particular item and move to the next." This makes continue the natural fit for filtering logic — process everything that qualifies, skip everything that does not, without nesting the success path inside an if block.

Like break, continue has two forms:

  • ›Unlabeled continue — skips the current iteration of the innermost enclosing loop
  • ›Labeled continue — skips the current iteration of a specific outer loop identified by its label

Syntax

Unlabeled continue

for (int index = 0; index < limit; index++) { if (shouldSkip(index)) { continue; // jump to index++ then re-check condition } // this code runs only when shouldSkip returns false }

continue in while and do-while

while (condition) { if (skipCondition) { continue; // jump directly to condition re-check } // body runs only when skipCondition is false }

Labeled continue

outerLoop: for (int outer = 0; outer < outerLimit; outer++) { for (int inner = 0; inner < innerLimit; inner++) { if (skipThisOuterIteration) { continue outerLoop; // skips rest of outer body, advances outer counter } } }

In a for loop, continue jumps to the update expression — i++ or i-- — before re-checking the condition. In a while or do-while loop, it jumps directly to the condition check. This timing difference matters when the update has side effects.

Beginner Examples

continue in a for Loop — Skipping Invalid Entries

The cleanest everyday use: process only the valid items, skip everything else without exiting the loop.

public class OrderFilterDemo { public static void main(String[] args) { double[] orderAmounts = {250.0, -50.0, 780.0, 0.0, 1200.0, -30.0, 450.0}; double validTotal = 0.0; int validCount = 0; for (double amount : orderAmounts) { // Skip invalid amounts — negative or zero orders are data errors if (amount <= 0) { System.out.println("Skipping invalid amount: Rs." + amount); continue; // jumps to the next element in the array } validTotal += amount; validCount++; } System.out.println("\nValid orders processed: " + validCount); System.out.printf("Total valid revenue: Rs.%.2f%n", validTotal); } }
Output:
Skipping invalid amount: Rs.-50.0
Skipping invalid amount: Rs.0.0
Skipping invalid amount: Rs.-30.0

Valid orders processed: 4
Total valid revenue: Rs.2680.00

The alternative to continue here is wrapping the processing logic in an else block. With continue, the main logic stays at the same indentation level — easier to read, especially when the processing block is long. During code reviews, teams consistently prefer continue for filtering over deep nesting.

continue in a while Loop — Skipping Blank Lines

import java.util.List; public class LogParserDemo { public static void main(String[] args) { List<String> logLines = List.of( "INFO App started", "", "ERROR Null pointer at OrderService:142", " ", "WARN Memory usage at 87%", "", "INFO Request processed in 42ms" ); int lineIndex = 0; int errorCount = 0; System.out.println("Non-empty log entries:"); while (lineIndex < logLines.size()) { String line = logLines.get(lineIndex); lineIndex++; // Skip blank or whitespace-only lines if (line.isBlank()) { continue; // jump to condition check — lineIndex already incremented } System.out.println(" " + line); if (line.startsWith("ERROR")) { errorCount++; } } System.out.println("\nErrors found: " + errorCount); } }
Output:
Non-empty log entries:
  INFO  App started
  ERROR Null pointer at OrderService:142
  WARN  Memory usage at 87%
  INFO  Request processed in 42ms

Errors found: 1

lineIndex++ runs before continue — the increment is inside the loop body, not in the header. This means continue correctly advances the index before skipping the blank-line processing. In a for loop the update expression runs automatically after continue; in while any manual update must happen before continue is reached or the loop will either skip it or produce an infinite loop.

Labeled continue — Skipping an Outer Loop Iteration

public class ScheduleFilterDemo { public static void main(String[] args) { String[] shifts = {"MORNING", "AFTERNOON", "NIGHT"}; String[] employees = {"Rahul", "Priya", "Arjun", "Deepa"}; // Simulate "NIGHT" shift having no available staff today String unavailableShift = "NIGHT"; System.out.println("Schedule assignments:"); shiftLoop: for (String shift : shifts) { if (shift.equals(unavailableShift)) { System.out.println(" " + shift + " shift: skipped — no staff available."); continue shiftLoop; // skips all employee assignments for this shift } for (String employee : employees) { System.out.println(" " + shift + " shift → " + employee); } } } }
Output:
Schedule assignments:
  MORNING shift → Rahul
  MORNING shift → Priya
  MORNING shift → Arjun
  MORNING shift → Deepa
  AFTERNOON shift → Rahul
  AFTERNOON shift → Priya
  AFTERNOON shift → Arjun
  AFTERNOON shift → Deepa
  NIGHT shift: skipped — no staff available.

continue shiftLoop skips the entire inner employee loop for the NIGHT shift and moves directly to the next shift in the outer loop. Without the label, a plain continue inside the outer loop would skip only the current outer iteration — it cannot be placed inside the inner loop and still target the outer one.

How continue Works Internally

Execution Flow — for Loop

The diagram below shows precisely where continue sends execution in a for loop versus normal flow.

          for (init; condition; update)
                     |
             Check condition
                     |
           condition is true
                     |
             Execute body
                     |
           continue reached?
                     |
          +----------+----------+
          |                     |
         Yes                    No
          |                     |
    Jump to update         Finish body
    expression (i++)            |
          |              Jump to update
    Re-check condition    expression

In a while loop, continue skips the update (there is no automatic one) and jumps straight to the condition check. This is why while loops with continue require the index increment to appear before the continue statement — if it comes after, the increment is skipped and the loop can hang.

continue vs break — Side-by-Side Behavior

public class BreakVsContinueDemo { public static void main(String[] args) { System.out.println("--- continue: skips current iteration, loop continues ---"); for (int i = 1; i <= 5; i++) { if (i == 3) continue; System.out.print(i + " "); } System.out.println("\n--- break: exits the loop entirely ---"); for (int i = 1; i <= 5; i++) { if (i == 3) break; System.out.print(i + " "); } System.out.println(); } }
Output:
--- continue: skips current iteration, loop continues ---
1 2 4 5 
--- break: exits the loop entirely ---
1 2 

With continue, element 3 is skipped but elements 4 and 5 still process. With break, the loop stops at element 3 — nothing after it runs. This side-by-side output is the clearest way to explain the behavioral difference in an interview.

Basic Example

The most common everyday use of continue is skipping unwanted values while summing or printing the rest. This program prints only the odd numbers from 1 to 10, skipping every even number with continue.

public class Main { public static void main(String[] args) { for (int i = 1; i <= 10; i++) { if (i % 2 == 0) { continue; // skip even numbers — jump straight to i++ } System.out.println(i); } } }

Output

1
3
5
7
9

How it works:

  1. ›The loop runs i from 1 to 10.
  2. ›When i % 2 == 0 is true (i.e. i is even), continue fires immediately, skipping the System.out.println(i) line for that iteration and jumping straight to the loop's update expression (i++).
  3. ›When i is odd, continue is never reached, so System.out.println(i) runs normally.
  4. ›The loop itself never exits early — it still checks all ten values, it just skips printing the even ones.

Best Practices

Use continue to replace else blocks in filtering loops

When a loop processes elements that pass a filter and skips those that do not, continue on the failure condition keeps the success path flat. The equivalent else block nests the success logic unnecessarily.

// Nested else — success logic at second indent level for (String item : items) { if (isInvalid(item)) { logInvalid(item); } else { process(item); // unnecessary nesting record(item); } } // continue — success logic stays flat for (String item : items) { if (isInvalid(item)) { logInvalid(item); continue; } process(item); record(item); }

In while loops, always update the index before continue

In a for loop, the update expression runs automatically after continue. In a while loop, no such automatic update exists — if you use continue and the index update comes after it in the body, the index never increments, the condition never changes, and the loop hangs.

int index = 0; while (index < items.size()) { index++; // must come BEFORE continue to avoid infinite loop if (shouldSkip(items.get(index - 1))) { continue; } process(items.get(index - 1)); }

Use labeled continue sparingly — prefer method extraction

When logic requires continue outerLoop deep inside a nested structure, consider whether extracting the inner loop into a method and using return is clearer. return exits the method entirely, which has the same effect as continue outerLoop from the outer loop's perspective.

Common Mistakes

Mistake 1 — Placing the while Loop Index Update After continue

public class InfiniteLoopMistakeDemo { public static void main(String[] args) { java.util.List<String> items = java.util.List.of("A", "", "B", "", "C"); int index = 0; // This causes an infinite loop when an empty string is encountered // because index++ never runs — continue skips it while (index < items.size()) { if (items.get(index).isEmpty()) { continue; // index++ below is never reached — infinite loop } System.out.println(items.get(index)); index++; // this line is skipped when continue fires on an empty string } } }

The index++ after continue is unreachable when continue fires. The while loop re-checks index < items.size() — but index is still 0 if the first item was empty. The fix is always to put the index update before the continue.

Mistake 2 — Confusing continue with break

public class ConfusionDemo { public static void main(String[] args) { int[] values = {10, 20, 30, 40, 50}; System.out.println("With continue — skips 30, processes all others:"); for (int value : values) { if (value == 30) continue; System.out.print(value + " "); } System.out.println("\n\nWith break — stops at 30, never reaches 40 or 50:"); for (int value : values) { if (value == 30) break; System.out.print(value + " "); } System.out.println(); } }
Output:
With continue — skips 30, processes all others:
10 20 40 50 

With break — stops at 30, never reaches 40 or 50:
10 20 

continue at 30 skips that element and continues with 40 and 50. break at 30 stops the loop — 40 and 50 are never seen. The confusion often happens because both appear inside an if inside a loop. The keyword itself is the entire difference.

Mistake 3 — Using continue in switch (It Does Not Apply)

// continue is NOT valid inside a switch statement that is not inside a loop switch (status) { case "PENDING": // continue; // compile error if not inside a loop break; // this is correct for switch case "COMPLETED": process(); break; }

continue applies to loops, not to switch. If a switch is inside a loop, continue inside the switch applies to the enclosing loop — not to the switch itself. This is a nuance that appears in interview questions about control flow.

Mistake 4 — Unreachable Code After continue

for (int i = 0; i < 5; i++) { if (i % 2 == 0) { continue; System.out.println("Even: " + i); // never reached — compile warning } System.out.println("Odd: " + i); }

Any statement placed after continue in the same block is unreachable and produces a compiler warning or error. Move the println before continue or remove it.

Interview Questions

Q1. What is the difference between break and continue in Java?

break exits the enclosing loop entirely — no further iterations occur after it fires. continue skips the rest of the current iteration's body and immediately moves to the next iteration — the loop continues running. Use break when the loop's purpose is complete. Use continue when the current element fails a filter but remaining elements should still be processed.

Q2. Where does continue transfer control in a for loop versus a while loop?

In a for loop, continue jumps to the update expression — i++ or equivalent — then re-checks the condition. In a while or do-while loop, continue jumps directly to the condition check, bypassing any index update that follows it in the body. This is why while loops using continue must place their index update before the continue statement, or the update is skipped and the loop can run indefinitely.

Q3. What is a labeled continue in Java?

A labeled continue skips the current iteration of the loop named by the label, not just the innermost enclosing loop. You place the label before an outer loop — outerLoop: — and write continue outerLoop; inside any inner loop to skip the rest of the outer loop's current body and advance to the outer loop's next iteration. This is useful when a condition encountered in an inner loop means the entire outer iteration should be abandoned.

Q4. Can continue be used inside a switch statement?

continue is not valid for switch itself — switch does not have iterations. However, if a switch statement is placed inside a loop, continue inside the switch applies to the enclosing loop, not to the switch. This causes the current loop iteration to skip and the next to begin. break inside a switch exits the switch; continue inside a switch (within a loop) exits the loop's current iteration. Knowing this distinction is tested in both service-based and product-based interviews.

Q5. How does continue help avoid deep nesting in loop bodies?

When a loop needs to filter elements before processing them, the alternative to continue is wrapping the processing logic in an else block or a deeply nested if. Each additional filter adds another nesting level. With continue, each filter becomes a guard at the top of the loop body — fail the guard and continue, otherwise process. The success path stays at the same indentation level regardless of how many guards are added, which is cleaner and easier to extend.

Q6. What happens if continue is placed inside a do-while loop?

In a do-while loop, continue jumps to the condition check at the bottom of the loop. The condition is evaluated, and if true, the body runs again from the top. The key difference from while is that the condition check in do-while is at the end — continue in a do-while always results in at least the condition being checked once more, whereas in a while loop it jumps to the same condition check at the top.

FAQs

What does the continue statement do in Java?

continue skips the rest of the current loop iteration and immediately moves to the next one. The loop does not exit — it keeps running with the next element or iteration. Only the remaining code in the current pass is skipped.

What is the difference between continue and break in Java?

break exits the enclosing loop entirely. continue skips only the current iteration and continues the loop. After break, no more iterations run. After continue, the loop proceeds normally with the next iteration.

Can continue be used in all loop types in Java?

Yes — continue works in for, while, and do-while loops. Its behavior is slightly different in each: in for it jumps to the update expression, in while and do-while it jumps to the condition check.

What is a labeled continue in Java?

A labeled continue skips the current iteration of a specific outer loop identified by its label. It is useful in nested loops when a condition inside the inner loop means the outer loop's current iteration should be abandoned entirely.

Does continue work inside a switch in Java?

continue is not valid for switch itself — switch has no iterations. If the switch is inside a loop, continue inside the switch applies to the enclosing loop, causing the current loop iteration to skip. break inside switch exits only the switch, not the loop.

When should I use continue instead of if-else?

Use continue when a loop processes elements that pass a filter and skips those that do not. Instead of wrapping the success path in an else block, place the skip condition with continue at the top and let the success path run flat. This reduces nesting and makes it easier to add new filters later.

Summary

The continue statement skips the current loop iteration and moves to the next — the loop itself keeps running. This single behavioral characteristic makes it the correct tool for filtering patterns: validate at the top, skip on failure with continue, process on success in the remaining body.

The most important behavioral detail to remember: in a for loop, continue jumps to the update expression before re-checking the condition. In a while loop, it jumps straight to the condition check — meaning any manual index update placed after continue in a while body is silently skipped, which causes an infinite loop. Always put while loop index updates before continue.

For interviews, know the difference from break by name and with a concrete output example, explain where continue transfers control in for versus while, and be ready to discuss whether continue inside a switch that is inside a loop applies to the switch or the loop. These three points cover the depth tested across both service-based recall questions and product-based design discussions.

What to Read Next

Practice Programs

Put this topic into practice — each program has full code, output and a step-by-step explanation you can run online.

Browse all Java programs →