Continue Statement in C
The continue statement in C is used to skip the rest of the code inside a loop for the current iteration and proceed with the next iteration of the loop.
Usage in Loops
You can use continue inside for, while, or do-while loops to skip certain iterations based on a condition.
- Example: for (int i = 1; i <= 5; i++) { if (i == 3) { continue; } printf("%d\n", i); } // Output: 1 2 4 5
How It Works
- When continue is executed, the remaining statements in the loop body for that iteration are skipped.
- The loop then evaluates its condition and proceeds to the next iteration.
- In for loops, the increment/decrement step is executed before the next iteration.
- In while or do-while loops, the condition is checked before continuing.
Tips
- Use continue to skip unnecessary iterations cleanly instead of using complex if-else statements.
- Avoid excessive use in nested loops, which can make logic harder to follow.
- Combine with if statements for precise control over which iterations to skip.
- Ensure that the loop condition will eventually become false to prevent infinite loops.
Common Mistakes
- Using continue outside a loop, which causes a compile-time error.
- Confusing continue with break (continue skips current iteration; break exits the loop).
- Placing code after continue that you expect to execute (it will be skipped).
- Using continue without proper conditions, causing unintended skipped iterations.
Codecrown