🚀 Learn BLE on Linux (BlueZ) – Programming Part 1
Hands-on Bluetooth Low Energy course. Special price: ₹399
Enroll Now on Udemy →C Continue Statement Explained
See how the continue statement in C skips a loop pass, where control lands in each loop type, and how it is used to filter data in embedded C programming.
The continue statement in C skips the rest of one loop pass and moves on to the next. This lecture of our free C programming course shows where control goes in for, while and do...while, how it differs from break, and the one bug that turns it into an endless loop. All programs were compiled with GCC and the output shown is real.
What You Will Learn
- What the continue statement in C does and how it differs from
break - Where control goes after
continueinfor,whileanddo...whileloops - How to skip invalid data without stopping a loop
- The classic bug where
continueskips the update and creates an endless loop - When to prefer an
ifblock overcontinue - How embedded code uses
continueto skip unused channels or bad samples
Prerequisites
- The three loop statements and the
breakstatement from the previous lecture - The
ifstatement and comparison operators gccwith-std=c17 -Wall -Wextrato try the code
What Is the Continue Statement in C?
The continue statement in C is written continue;. When it runs, the rest of the current loop body is skipped and the loop moves on to its next pass. The loop is not finished. It simply abandons the current iteration. Like break, it is almost always placed under an if, so that only the unwanted case is skipped.
What ‘the next pass’ means depends on the loop type. This detail causes most confusion, so keep the table below close.
| Loop | After continue control goes to | Consequence |
|---|---|---|
for | The update expression (i++), then the condition | The counter still advances |
while | The condition test | Any update written below continue is skipped |
do...while | The condition test at the bottom | Body is not repeated unless the test is true |
Continue Statement in C Inside a for Loop
In a for loop the update expression lives in the loop header, so continue does not interfere with it. The trace below prints a line at the start and end of every pass. When i is 3 the continue jumps over the end-of-pass message, but the counter still moves to 4.
#include <stdio.h>
int main(void)
{
int i;
for (i = 1; i <= 5; i++) {
printf("start of pass, i=%d\n", i);
if (i == 3) {
printf(" continue: jump to i++\n");
continue;
}
printf(" end of pass, i=%d\n", i);
}
return 0;
}
$ gcc -std=c17 -Wall -Wextra contfor.c -o contfor && ./contfor
start of pass, i=1
end of pass, i=1
start of pass, i=2
end of pass, i=2
start of pass, i=3
continue: jump to i++
start of pass, i=4
end of pass, i=4
start of pass, i=5
end of pass, i=5
Pass 3 prints a start line and the continue message but never prints its end line. Pass 4 starts normally, proving that i++ ran. This safe behaviour is one reason for loops are a good partner for continue.
Skipping Bad Data with the Continue Statement in C
The most useful application is filtering. Sensors, serial links and ADCs sometimes deliver an error marker instead of a reading. The loop below treats a negative value as invalid, reports it and moves on to the next sample. Only valid samples are added to the sum and counted.
#include <stdio.h>
int main(void)
{
const int raw[] = {120, -1, 135, 128, -1, 140};
int i, valid = 0, sum = 0;
for (i = 0; i < 6; i++) {
if (raw[i] < 0) {
printf("sample %d invalid, skipped\n", i);
continue;
}
sum += raw[i];
valid++;
}
printf("valid=%d sum=%d average=%d\n", valid, sum, sum / valid);
return 0;
}
$ gcc -std=c17 -Wall -Wextra skipbad.c -o skipbad && ./skipbad
sample 1 invalid, skipped
sample 4 invalid, skipped
valid=4 sum=523 average=130
Because the invalid samples are skipped by continue, the average is calculated over four valid readings: 523 divided by 4 gives 130 in integer arithmetic. If all samples were invalid, valid would be zero and the division would fail, so a real program must check valid before dividing.
Guard the division
Skipping data can leave you with nothing to average. Test valid > 0 before the division, or the program may crash on a divide by zero.
Break Versus Continue Statement in C
Both statements change the normal flow, but they do very different things. break abandons the whole loop. continue abandons only the current pass. The run below uses the same loop twice and prints the counter afterwards to show the difference.
#include <stdio.h>
int main(void)
{
int i;
printf("break : ");
for (i = 1; i <= 6; i++) {
if (i == 4) break;
printf("%d ", i);
}
printf("| i after loop = %d\n", i);
printf("continue: ");
for (i = 1; i <= 6; i++) {
if (i == 4) continue;
printf("%d ", i);
}
printf("| i after loop = %d\n", i);
return 0;
}
$ gcc -std=c17 -Wall -Wextra cmp.c -o cmp && ./cmp
break : 1 2 3 | i after loop = 4
continue: 1 2 3 5 6 | i after loop = 7
| Question | break | continue |
|---|---|---|
| Does the loop end? | Yes, immediately | No, only the current pass ends |
| Where does control go? | First statement after the loop | Next iteration (update or test) |
Usable in switch? | Yes, ends the switch | No on its own, it needs an enclosing loop |
| Typical use | Search found, error, timeout | Skip invalid or unwanted item |
| Counter after loop (demo) | 4 (stopped there) | 7 (ran to the end) |
Continue Statement in C Inside while and do…while
In a while loop the update usually sits inside the body. If you place continue before that update, the update is skipped and the same value is tested again on the next pass. The condition stays the same, so the loop never ends. This is the classic bug of the continue statement in C.
#include <stdio.h>
int main(void)
{
int i = 1, guard = 0;
while (i <= 5) {
if (i == 3) {
if (++guard > 3) { /* safety stop for the demo */
printf("guard hit: i is stuck at %d\n", i);
break;
}
continue; /* skips i++ below */
}
printf("i=%d\n", i);
i++;
}
return 0;
}
$ gcc -std=c17 -Wall -Wextra contwhile_bug.c -o contwhile_bug && ./contwhile_bug
i=1
i=2
guard hit: i is stuck at 3
The guard stopped the demo. Without it the program would loop forever with i stuck at 3. The fix is to move the update to the top of the loop, before the continue decision, as shown below.
#include <stdio.h>
int main(void)
{
int i = 0;
while (i < 5) {
i++; /* update first, then decide */
if (i == 3)
continue;
printf("i=%d\n", i);
}
return 0;
}
$ gcc -std=c17 -Wall -Wextra contwhile_fix.c -o contwhile_fix && ./contwhile_fix
i=1
i=2
i=4
i=5
continue in do…while
In do...while the test is at the bottom, so continue jumps straight to that test. In the program below the odd values skip the print statement, but the test still runs and the loop ends when n reaches 6.
#include <stdio.h>
int main(void)
{
int n = 0;
do {
n++;
if (n % 2 == 1)
continue; /* jumps to the while test */
printf("even n=%d\n", n);
} while (n < 6);
return 0;
}
$ gcc -std=c17 -Wall -Wextra contdo.c -o contdo && ./contdo
even n=2
even n=4
even n=6
continue inside a switch
continue is not for switch. If you write it inside a switch that sits in a loop, it applies to the loop, not to the switch. That makes it a handy way to skip the rest of the loop body from inside a case.
Embedded Use Cases
| Situation | How continue helps |
|---|---|
| Scanning ADC channels | Skip channels that are disabled in a configuration mask |
| Parsing a serial stream | Skip padding or noise bytes and go to the next byte |
| Processing a buffer | Ignore entries marked as free or invalid |
| Key scanning | Skip rows or columns that are not connected on this board |
Common Mistakes and Troubleshooting
| Mistake | Symptom | Fix |
|---|---|---|
continue before the update in a while loop | Loop never ends | Update first, or restructure with if |
Using continue when break was meant | Loop runs on after a fault | Decide whether to skip one pass or stop |
| No check after skipping everything | Divide by zero or empty result | Test the count before using it |
Too many continue statements | Hard-to-follow loop | Combine conditions or use a helper function |
Best Practices
- Use
continueat the top of the loop body for early rejection of bad input, so the main logic stays unindented. - In
whileanddo...whileloops, put the update before anycontinue. - If a loop has more than two
continuestatements, consider anifblock or a helper function. - Comment why the pass is skipped, for example ‘invalid sample’.
Readability and safety
continue can always be replaced by wrapping the rest of the body in an if. Use whichever reads better. Some coding standards discourage it because jumps make control flow harder to review, so check your project rules before using it in safety-related code.
Summary and Key Takeaways
continue;skips the rest of the current pass and starts the next one.- In
forit jumps to the update expression, inwhileanddo...whileto the test. - It is ideal for skipping invalid or unwanted data.
continuebefore the update in awhileloop creates an infinite loop.breakends the loop,continueends only one pass.
Conclusion
The continue statement in C keeps loops short and focused by rejecting unwanted cases early. Remember where control lands in each loop type, and you will avoid its one famous trap. In the next lecture of this free C programming course you will practise break, continue and infinite loops together with a quiz and interview questions.
Frequently Asked Questions
What does the continue statement in C do?
It skips the remaining statements of the current loop iteration and moves on to the next iteration.
What is the difference between break and continue in C?
break ends the whole loop. continue ends only the current pass and the loop keeps running.
Where does continue jump in a for loop?
To the update expression, such as i++, and then to the condition test.
Why can continue cause an infinite loop in a while loop?
If the variable update comes after the continue, it is skipped, so the condition never changes.
Can I use continue in a switch statement?
Only when the switch is inside a loop. Then continue applies to the loop, not to the switch.
Is continue good practice in embedded C?
It is fine for skipping invalid data, but keep it simple and follow your project coding standard.
Is this C course really free?
Yes. The free C programming course on EmbeddedPathashala is aimed at students learning C for embedded systems.
Keep learning with the free C course
EmbeddedPathashala teaches C programming for embedded systems for free, one small lecture at a time. Move on to the next lecture, or revisit the previous one and run the programs yourself.

1 Comment