The continue statement in C is used inside loops when you want to skip the rest of the current iteration and move directly to the next iteration. It does not terminate the loop completely. Instead, it tells the loop to ignore the remaining statements for the current cycle and continue with the next loop check or update step.
This makes continue useful when some values should be ignored but the loop itself should keep running. For example, you may want to skip negative numbers, ignore invalid input, or bypass one special case in a repeated process. In this article, we will understand the continue statement in C, its syntax, working in different loops, difference from break, use in nested loops, common mistakes, and best practices.
What is Continue Statement in C?
The continue statement in C is a loop control statement that skips the remaining statements inside the current loop iteration and transfers control to the next iteration of that loop.
It can be used inside:
forloopswhileloopsdo whileloops
The continue statement skips the current iteration, but it does not stop the loop itself.
Syntax of Continue Statement in C
The syntax is very simple:
continue;When the program reaches this statement inside a loop, it immediately skips the remaining code in the current iteration and moves ahead according to the type of loop.
How Continue Statement Works in C
- The loop starts an iteration.
- If the program reaches
continue;, the remaining statements of that iteration are skipped. - Control moves to the loop’s next iteration step.
- The loop continues normally until its condition becomes false or it is terminated by
breakor another control statement.
The exact next step depends on the loop type. In a for loop, the update expression runs before the next condition check. In a while or do while loop, control returns to the condition check point, so variable updates must be handled carefully.
Simple Example of Continue Statement in C
The following program skips the number 3 while printing the values from 1 to 5.
#include <stdio.h>
int main(void)
{
int i;
for (i = 1; i <= 5; i++)
{
if (i == 3)
{
continue;
}
printf("%d ", i);
}
return 0;
}The output is 1 2 4 5 because when i == 3, the continue statement skips the printf() call for that iteration.
Continue Statement in for Loop
In a for loop, continue skips the remaining body statements and then moves to the update expression before the next condition check. That is why continue is often safest and easiest to understand inside for loops.
#include <stdio.h>
int main(void)
{
int i;
for (i = 1; i <= 10; i++)
{
if (i % 2 == 0)
{
continue;
}
printf("%d ", i);
}
return 0;
}This prints only odd numbers because even values are skipped.
Continue Statement in while Loop
In a while loop, continue jumps directly to the condition check for the next iteration. This creates an important risk: if the loop variable update appears after continue, that update may never run and the loop can become infinite.
Correct example:
#include <stdio.h>
int main(void)
{
int i = 0;
while (i < 5)
{
i++;
if (i == 3)
{
continue;
}
printf("%d ", i);
}
return 0;
}Here the update happens before continue, so the loop progresses correctly.
Continue Statement in do while Loop
In a do while loop, continue skips the remaining body statements and moves to the condition check at the bottom of the loop. Just like in while, the update logic must be placed carefully.
#include <stdio.h>
int main(void)
{
int i = 0;
do
{
i++;
if (i == 2)
{
continue;
}
printf("%d ", i);
} while (i < 5);
return 0;
}This prints 1 3 4 5 because the value 2 is skipped.
Difference Between Break and Continue in C
Many beginners confuse break and continue, but their effects are different.
| Statement | Effect |
|---|---|
continue | Skips the current iteration and moves to the next one |
break | Terminates the loop completely |
If you want to ignore one iteration, use continue. If you want to stop the loop entirely, use break.
Continue Statement in Nested Loops
When continue is used inside nested loops, it affects only the nearest enclosing loop. It does not skip the outer loop automatically.
#include <stdio.h>
int main(void)
{
int i, j;
for (i = 1; i <= 3; i++)
{
for (j = 1; j <= 3; j++)
{
if (i == 2 && j == 2)
{
continue;
}
printf("(%d,%d) ", i, j);
}
}
return 0;
}In this example, only the inner loop iteration for (2,2) is skipped. The outer loop continues normally.
Can Continue Be Used in Switch Statement?
By itself, continue cannot be used in a plain switch statement that is not inside a loop. The continue statement is meant for loops only.
However, if a switch statement is written inside a loop, then continue can appear inside the switch and it will apply to the nearest enclosing loop, not to the switch itself.
This is an important rule because many old explanations get it wrong and make it sound as if continue directly controls switch-case flow. It does not. It always belongs to a loop.
Common Uses of Continue Statement in C
- skipping unwanted values in a loop
- ignoring invalid input while continuing processing
- skipping even or odd numbers during traversal
- filtering data without ending the loop
- avoiding deep nesting for one special case
Common Mistakes in Continue Statement in C
- placing
continuebefore the loop variable update inwhileordo while - confusing
continuewithbreak - assuming continue exits the loop completely
- trying to use continue outside a loop
- using continue so often that loop logic becomes hard to follow
| Mistake | Problem | Better Practice |
|---|---|---|
| Update after continue in while loop | Can create infinite loop | Move the update before the continue path when needed |
| Using continue instead of break | Loop keeps running when it should stop | Choose the control statement based on the exact goal |
| Overusing continue | Makes control flow harder to read | Use it only when it genuinely simplifies the loop |
Best Practices for Continue Statement in C
- Use continue only when it makes loop logic simpler.
- Be careful with loop variable updates in
whileanddo while. - Do not confuse continue with break.
- Keep nested loop behavior clear when continue is inside inner loops.
- Avoid writing overly complex loops that depend on many continue paths.
FAQs
What is continue statement in C?
The continue statement in C skips the rest of the current loop iteration and moves to the next iteration.
What is the difference between break and continue in C?
continue skips only the current iteration, while break terminates the loop completely.
Can continue be used in while loop?
Yes. It can be used in while loops, but the update logic must be handled carefully to avoid infinite loops.
Can continue be used in switch statement?
Only if the switch is inside a loop. In that case, continue applies to the loop, not to the switch itself.
Does continue stop the loop?
No. It skips only the current iteration and lets the loop continue normally.
Can continue cause an infinite loop?
Yes, especially in while or do while loops if the update step is skipped and the loop condition never changes.