C Notes
Learn the continue statement in C with syntax, flowchart, examples in for/while/do-while loops, difference from break, and interview questions.
The continue statement is a loop control mechanism that skips the remaining statements in the current iteration and immediately jumps to the next iteration of the loop. Unlike break which exits the loop entirely, continue says "I'm done with this round — let's move to the next one."
Imagine you're grading exam papers. You pick up a paper, notice it belongs to an absent student (blank paper), and you skip it — moving on to the next paper. You don't stop grading entirely (that would be break); you just skip that one paper. That's exactly what continue does in a loop.
Syntax
continue;Where Can continue Be Used?
The continue statement is valid only inside loops:
forloopwhileloopdo-whileloop
It cannot be used in switch statements or outside loops.
Flowchart of Continue Statement
| Statement 2 | |
|---|---|
| Statement 3 |
How continue Works in Different Loops
The behavior of continue varies slightly depending on the loop type:
| Loop Type | After continue, execution goes to... |
|---|---|
for | Update expression, then condition check |
while | Condition check directly |
do-while | Condition check directly |
This difference in for loops is crucial — the update expression still executes!
Continue in For Loop
Numbers 1-10 (skipping multiples of 3): 1 2 4 5 7 8 10
Notice that 3, 6, and 9 are skipped. The i++ update still happens — that's why the loop doesn't get stuck.
Continue in While Loop
Even numbers from 1-20: 2 4 6 8 10 12 14 16 18 20
Important: In while loops, make sure the update (increment) happens before continue. Otherwise, you get an infinite loop!
Continue in Do-While Loop
1 3 5 7 9 11 13 15
Practical Examples
Example 1: Skip Negative Numbers in Calculation
Sum of positive numbers: 37 Count of positive numbers: 5 Average of positive numbers: 7.40
Example 2: Process Valid Input Only
Enter 5 scores (0-100). Invalid scores will be skipped: Score 1: 85 -> Accepted. Running total: 85 Score 2: 150 -> Invalid score! Skipping. Score 3: 72 -> Accepted. Running total: 157 Score 4: -10 -> Invalid score! Skipping. Score 5: 91 -> Accepted. Running total: 248 Valid scores: 3 out of 5 Average: 82.67
Example 3: Print a Pattern Skipping Diagonal
Matrix with skipped diagonal: . 2 3 4 5 6 . 8 9 10 11 12 . 14 15 16 17 18 . 20 21 22 23 24 .
Example 4: Word Processing - Skip Vowels
Original: Hello World Programming Without vowels: Hll Wrld Prgrmmng
Continue vs Break
| Feature | continue | break |
|---|---|---|
| Action | Skips to next iteration | Exits loop entirely |
| Loop continues? | Yes | No |
| After execution | Next iteration starts | Statement after loop |
| Use case | Filter/skip specific items | Stop when goal is reached |
| Works in switch? | No | Yes |
Visual Comparison
Continue in Nested Loops
Like break, continue only affects the innermost loop containing it:
Row 1: 1 2 4 5 Row 2: 1 2 4 5 Row 3: 1 2 4 5
Best Practices
- Place continue early in the loop body — acts as a guard clause
- In while/do-while, update before continue — prevents infinite loops
- Don't nest too many continues — makes flow hard to follow
- Consider restructuring — sometimes an if-else is clearer than continue
- Comment the reason for skipping to help readability
Interview Questions
Q1: What happens to the update expression in a for loop when continue executes?
The update expression (like i++) still executes. After continue, control goes to the update expression first, then to the condition check. This is different from while/do-while where continue goes directly to the condition.
Q2: Can continue cause an infinite loop?
Yes, in while and do-while loops. If the increment/update statement appears after continue and continue always executes, the loop variable never changes, creating an infinite loop. In for loops, this is not an issue because the update is part of the loop header.
Q3: Is continue valid inside a switch statement?
Only if the switch is inside a loop. The continue will skip the rest of the loop iteration (not the switch). A continue in a switch that's not inside a loop is a compilation error.
Q4: What is the output of this code?
The output is 0 1 3. When i=2, continue skips that iteration. When i=4, break exits the loop. So only 0, 1, and 3 are printed.
Q5: Can we use continue with an if statement outside a loop?
No. continue is only valid inside loop bodies (for, while, do-while). Using it anywhere else, including a standalone if statement, causes a compilation error.
Summary
The continue statement is a loop control tool that skips the remaining code in the current iteration and proceeds to the next iteration. It's perfect for filtering out unwanted values, skipping invalid input, or avoiding specific conditions without deeply nested if-else blocks. Remember the critical difference: in for loops, the update expression still runs after continue; in while/do-while loops, make sure the update happens before continue to avoid infinite loops. Use continue to keep your loop logic clean and guard-clause style.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Continue Statement in C Programming.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this C Programming topic.
Search Terms
c-programming, c programming, programming, control, statements, continue, statement, continue statement in c programming
Related C Programming Topics