What Does continue Do in C
The continue statement is a control‑flow keyword in the C programming language that lets a program skip the remaining statements in the current iteration of a loop and jump directly to the loop’s condition‑checking or update expression. By using continue, developers can avoid unnecessary processing when a particular condition is met, keeping loops clean and efficient. Understanding how continue works is essential for writing readable loops, especially when dealing with nested loops, input validation, or iterative algorithms.
Honestly, this part trips people up more than it should.
How continue Works in a Loop
When the interpreter encounters a continue; statement inside a loop body, it:
- Stops executing the rest of the current iteration – any code placed after the
continuein that iteration is ignored. - Transfers control to the loop’s update expression (for
forloops) or directly to the condition test (forwhileanddo‑whileloops). - Re‑evaluates the loop condition – if the condition remains true, the next iteration begins; otherwise, the loop terminates.
This behavior differs from break, which exits the loop entirely. continue merely skips to the next cycle.
Syntax
continue; // must be followed by a semicolon
The statement can appear anywhere inside the body of a for, while, or do‑while loop. It is illegal to use continue outside a loop context; doing so will produce a compile‑time error That's the whole idea..
Basic Examples
Example 1: Skipping Even Numbers in a for Loop
#include
int main() {
for (int i = 1; i <= 10; ++i) {
if (i % 2 == 0) { // if i is even
continue; // skip the printf below
}
printf("%d ", i); // prints only odd numbers
}
return 0;
}
Output:
1 3 5 7 9
When i is even, the continue statement causes the loop to jump to the increment part (++i) and start the next iteration, so the printf is never executed for those values.
Example 2: Using continue in a while Loop
#include
int main() {
int count = 0;
while (count < 5) {
++count;
if (count == 3) {
continue; // skip printing when count is 3
}
printf("%d ", count);
}
return 0;
}
Output:
1 2 4 5
Here, after incrementing count to 3, the continue sends execution back to the while (count < 5) test, bypassing the printf.
Example 3: continue in a do‑while Loop
#include
int main() {
int value = 0;
do {
++value;
if (value % 4 == 0) {
continue; // avoid processing multiples of 4
}
printf("%d ", value);
} while (value < 12);
return 0;
}
Output:
1 2 3 5 6 7 9 10 11
The loop stops when value reaches 12, but each time value is a multiple of 4, the continue skips the printf Easy to understand, harder to ignore. Nothing fancy..
Nested Loops and continue
In nested loops, a continue statement only affects the innermost loop in which it appears. Outer loops continue their normal execution unless they also contain their own continue.
#include
int main() {
for (int i = 1; i <= 3; ++i) {
for (int j = 1; j <= 5; ++j) {
if (j == 3) {
continue; // skips only the inner loop's j=3 iteration
}
printf("(%d,%d) ", i, j);
}
printf("\n"); // newline after each outer iteration
}
return 0;
}
Output:
(1,1) (1,2) (1,4) (1,5)
(2,1) (2,2) (2,4) (2,5)
(3,1) (3,2) (3,4) (3,5)
Notice that when j equals 3, the inner loop skips the printf but the outer loop proceeds to the next value of i That's the part that actually makes a difference..
Common Pitfalls
| Pitfall | Explanation | How to Avoid |
|---|---|---|
Using continue outside a loop |
Results in a compilation error: “‘continue’ statement not within a loop”. On top of that, | |
Placing continue after the loop’s body |
A continue after the loop’s closing brace is meaningless and will be flagged as unreachable code. g. |
|
| Infinite loops caused by missing update | If continue skips the update expression (e. |
Clearly comment the intent or use descriptive variable names to make the control flow obvious. |
Confusing continue with break |
break exits the loop entirely; continue only skips the current iteration. |
Ensure the statement is inside a for, while, or do‑while. |
continue vs. break
| Feature | continue |
break |
|---|---|---|
| Effect | Skips remaining statements in the current iteration and proceeds to the next iteration. | Loop update expressions are not executed after the break; the loop ends. On top of that, |
| Impact on loop variables | Loop update expressions (in for) are still executed; condition is re‑evaluated. In real terms, |
Terminates the loop immediately; control passes to the statement following the loop. |
| Use case | When you want to ignore certain iterations but still process others. , finding a match and exiting). g. | When a condition indicates that no further processing is needed (e.Here's the thing — |
| Nested loops | Affects only the innermost loop where it appears. | A break also only exits the innermost loop unless labeled breaks (via goto) are used. |
You'll probably want to bookmark this section.
Practical Applications
-
Input Validation – Skip invalid entries while reading a series of inputs That's the whole idea..
for (int i = 0; i < N; ++i) { scanf("%d", &value); if (value < 0) { fprintf(stderr, "Negative value ignored\n"); continue; // ignore this value, ask for next } sum += value; } -
Filtering Data – Process only items that meet a criterion inside a