Lesson Completion
Back to course

continue Statement: The Iteration Skipper

Beginner
15 minutes4.8Java

1. The Hook (The "Byte-Sized" Intro)

In a Nutshell: The continue statement skips the rest of the current iteration and jumps to the next one. Unlike break which exits the loop, continue just skips ahead.

When Twitter filters your timeline: for (Tweet t : timeline) { if (t.isBlocked()) continue; display(t); }. Blocked user? Skip, show next tweet!


2. Conceptual Clarity (The "Simple" Tier)

💡 The Analogy: Skip Button

Think of continue as the song skip button:

  • Loop = Playlist
  • continue = Skip this song
  • Action = Jump to next song immediately

You don't leave the playlist—just skip to the next track!

Visual Map

graph TB Start["Loop iteration i=0"] --> Check1{Should skip?} Check1 -- Yes --> Continue1["continue;<br/>Skip to i=1"] Check1 -- No --> Execute1["Execute remaining code"] Execute1 --> Next1["Move to i=1"] Continue1 --> Check2{Should skip?} Next1 --> Check2 Check2 -- No --> Execute2["Execute code"] Execute2 --> End style Continue1 fill:#F57C00 style Execute2 fill:#2E7D32

3. Technical Mastery (The "Deep Dive")

📘 Formal Definition

Syntax:

java
continue; // Skip to next iteration of innermost loop continue label; // Skip to labeled loop's next iteration
  • Skips: Remaining code in current iteration
  • Continues: Next iteration of loop
  • Works with: for, while, do-while loops

The "Why" Paragraph

continue is perfect for filtering—when you want to skip certain items without breaking the entire loop. It's cleaner than wrapping remaining code in an else block. Common use cases: skip invalid data, filter by criteria, handle special cases early. It's the "skip this one, move on" command.


4. Interactive & Applied Code

Complete Example

java
public class ContinueDemo { public static void main(String[] args) { // Basic continue (skip even numbers) for (int i = 1; i <= 10; i++) { if (i % 2 == 0) { continue; // Skip even numbers } System.out.println(i); // 1 3 5 7 9 (odd only) } // Real-world: Filter invalid data int[] scores = {85, -1, 92, 0, 78, -5, 88}; int sum = 0; int count = 0; for (int score : scores) { if (score < 0) { continue; // Skip invalid scores } sum += score; count++; } double average = (double) sum / count; System.out.println("Average: " + average); // 85.75 // Real-world: Skip blocked users String[] users = {"Alice", "BLOCKED", "Bob", "Charlie", "BLOCKED"}; for (String user : users) { if (user.equals("BLOCKED")) { continue; // Skip blocked } System.out.println("Hello, " + user); // Alice, Bob, Charlie } // continue in while loop int i = 0; while (i < 10) { i++; if (i % 3 == 0) { continue; // Skip multiples of 3 } System.out.print(i + " "); // 1 2 4 5 7 8 10 } System.out.println(); // Labeled continue (nested loops) outerLoop: for (int row = 1; row <= 3; row++) { for (int col = 1; col <= 3; col++) { if (col == 2) { continue outerLoop; // Skip to next row } System.out.print(row + "," + col + " "); } } // Output: 1,1 2,1 3,1 System.out.println(); // Real-world: Process only.java files String[] files = {"Main.java", "README.md", "Utils.java", "config.xml"}; for (String file : files) { if (!file.endsWith(".java")) { continue; // Skip non-Java files } System.out.println("Compiling: " + file); } } }

⚠️ Common Mistakes

Mistake #1: continue outside loop

java
if (condition) { continue; // ❌ Compile error: continue not in loop }

Mistake #2: Using continue when break is needed

java
for (int i = 0; i < 100; i++) { if (found) { continue; // ❌ Keeps looping! } search(); } // Use break to exit completely

Mistake #3: Forgetting loop update before continue

java
int i = 0; while (i < 10) { if (i % 2 == 0) { continue; // ❌ Infinite loop! i never increments } i++; // Never reached for even numbers } // Fix: Update before continue while (i < 10) { i++; if (i % 2 == 0) continue; }

5. The Comparison & Decision Layer

continue vs break

Aspectcontinuebreak
ActionSkip to next iterationExit loop entirely
Remaining codeSkippedAll loop code skipped
Loop continues?✅ Yes❌ No
Use caseFilter itemsStop when found

Decision Tree

graph TD Start{What to do?} Start --> Skip["Skip this item,<br/>process others"] --> Continue["Use continue"] Start --> Exit["Done with loop,<br/>exit completely"] --> Break["Use break"] style Continue fill:#2E7D32

6. The "Interview Corner" (The Edge)

🏆 Interview Question #1: "continue vs break?"

Answer:

  • continue: Skip current iteration only, loop continues
  • break: Exit loop completely, no more iterations
java
for (int i = 0; i < 5; i++) { if (i == 2) continue; System.out.print(i + " "); // 0 1 3 4 } for (int i = 0; i < 5; i++) { if (i == 2) break; System.out.print(i + " "); // 0 1 }

🏆 Interview Question #2: "Can continue be used with labeled loops?"

Answer: Yes, skips to next iteration of labeled loop:

java
outer: for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (j == 1) continue outer; // Skip to next i } }

🏆 Interview Question #3: "Avoid continue with better structure?"

Answer: Sometimes yes—use positive conditions:

java
// ❌ Using continue for (Item item : items) { if (item.isInvalid()) continue; process(item); } // ✅ Positive condition (arguably clearer) for (Item item : items) { if (item.isValid()) { process(item); } }

Both work—choose based on readability.


💡 Pro Tips

Tip #1: Guard clause pattern

java
for (Data d : dataset) { if (d == null) continue; // ✅ Early skip if (!d.isValid()) continue; process(d); // Only valid data reaches here }

Tip #2: Update before continue in while

java
int i = 0; while (i < 10) { i++; // ✅ Update first! if (i % 2 == 0) continue; System.out.println(i); }

Tip #3: Sometimes if-else is clearer

java
// For simple cases, if-else may be clearer: for (int i = 0; i < 10; i++) { if (i % 2 == 0) { System.out.println("Even"); } else { System.out.println("Odd"); } }

📚 Real-World Examples

Filtering: Skip invalid/blocked items
Validation: Skip items failing checks
Pagination: Skip already-seen items
Processing: Skip special cases


🎓 Key Takeaways

✅ Skips current iteration, continues with next
✅ Perfect for filtering and skipping items
✅ Use labeled continue for nested loops
✅ Update loop variable before continue (while loops)
✅ Consider if positive condition is clearer

Final Tip: "Skip this, not all"—that's continue!

Topics Covered

Java FundamentalsControl Flow

Tags

#java#control-flow#loops#conditionals#if-else#switch#beginner-friendly

Last Updated

2025-02-01