Continue Statement in C Language — BKNMU Junagadh
Jump Statements, Iteration Skipping, Loop Control Behavior, and Differences from Break
continue statement in C is a jump statement that skips the remaining statements in the current iteration of a loop (for, while, or do...while) and immediately transfers execution control to the next iteration pass.
The continue keyword alters loop behavior in four distinct ways across different contexts:
1. Skipping Iterations
Instantly stops execution of statements written below it inside the loop body for that specific run only.
2. Behavior in For Loop
Jumps directly to the counter update/increment step (i++) before evaluating the condition for the next pass.
3. Behavior in While Loops
Jumps directly to the test condition line at the top (or bottom for do...while) for re-evaluation.
4. Selective Execution
Ideal for skipping invalid values, unwanted inputs, or filtering specific numbers (like skipping odd/even values).
Here is a detailed comparative breakdown showing how continue differs fundamentally from break:
| Comparison Parameter | Break Statement (break;) | Continue Statement (continue;) |
|---|---|---|
Primary Effect | Terminates the entire loop completely. | Skips only the remaining part of the current iteration. |
Execution Flow | Jumps out of the loop body to the statement placed after the loop. | Jumps to the update step / condition check for the next iteration. |
Loop Continuity | Loop stops permanently; no further iterations take place. | Loop continues running for subsequent remaining iterations. |
Switch Statement Usage | Valid and strictly required to prevent case fallthrough. | Invalid inside switch blocks (unless enclosed inside a loop). |
break, the continue statement can ONLY be used inside loop bodies (for, while, do...while). Using continue outside a loop will trigger a compile-time error!
1. Skipping a Specific Value in a For Loop:
#include <stdio.h>
int main() {
int i;
for (i = 1; i <= 5; i++) {
if (i == 3) {
printf("Skipping iteration at i = 3\n");
continue; // Skips printf below for i = 3
}
printf("Value: %d\n", i);
}
return 0;
}
2. Printing Only Even Numbers (1 to 10):
#include <stdio.h>
int main() {
int i;
for (i = 1; i <= 10; i++) {
// Skip odd numbers
if (i % 2 != 0) {
continue;
}
printf("Even Number: %d\n", i);
}
return 0;
}
3. Continue inside a While Loop (Counter Placement Caution):
#include <stdio.h>
int main() {
int i = 0;
while (i < 5) {
i++; // Counter incremented BEFORE continue
if (i == 3) {
continue; // Jumps directly to while(i < 5)
}
printf("i = %d\n", i);
}
return 0;
}
Step 1: Trigger Condition Checked
An if statement checks whether current iteration data should be skipped.
Step 2: Execute Continue Keyword
When triggered, continue; halts processing for all remaining lines in the current block.
Step 3: Jump to Next Pass
Control jumps to the counter update step (in for loops) or condition re-test (in while / do...while loops).
while loop, if you place your counter increment (i++) after the continue statement, triggering continue will bypass the increment step, freezing your program in an infinite loop! Always update your variable prior to calling continue in while loops.
continue CANNOT be used directly inside a switch statement to skip cases. It only applies to enclosing loops!
- Statement Type: Unconditional Jump / Control Keyword.
- Primary Function: Bypasses remaining statements in the current iteration pass.
- For Loop Effect: Jumps to increment/decrement counter update expression.
- While / Do-While Effect: Jumps directly to condition re-evaluation.
- Invalid Context: Cannot be used directly inside
switchblocks without an enclosing loop.