What is C Continue Statement Explained- Embedded C Programming Course Online

Need Help or More Information?

Questions about this free C programming course, embedded C training or career guidance? Get in touch.

Free C Programming Course · Control Statements

C Continue Statement Explained

Learn the continue statement in C with embedded-style examples: skip bad sensor samples, avoid infinite loops, and write cleaner loops in this free C tutorial.

The continue statement in C lets a loop abandon the current iteration and jump straight to the next one. It is one of the smallest keywords in the language, yet it shows up constantly in embedded C programming: a firmware loop reads a sample, decides the sample is garbage, and moves on without touching the running totals. This lecture is part of our free C programming course, and like every lecture in this free C tutorial it focuses on what the keyword really does, where students get it wrong, and how it behaves differently in for, while and do-while loops.

If you are following this free embedded C course in order, you have already met break and the three loop forms. Here we complete the loop-control toolbox before we move on to goto and switch in the next lectures.

What You Will Learn

  • What the continue statement in C does and where it is legal
  • Exactly where control lands after continue in for, while and do-while loops
  • How to skip invalid sensor readings and still compute a correct average
  • The classic infinite-loop bug caused by continue in a while loop
  • When to prefer continue, when to prefer an if-else, and how it differs from break

Prerequisites

You should be comfortable with variables, relational operators, the if statement and at least one loop (for or while) from the earlier lectures of this chapter. A GCC or Clang compiler on Linux, Windows with MinGW, or any online C compiler is enough to run every example.

What Is the Continue Statement in C?

The continue statement is a jump statement that is only valid inside a loop body. When the program reaches it, the rest of the loop body is skipped for this pass, and control goes to the point where the loop decides whether to run again. Nothing is exited, nothing is reset: the loop variable keeps its value, and the loop simply moves on.

Think of an assembly line inspector. Each item that arrives is checked, and a defective one is pushed aside without going through the remaining stations. The line itself never stops. That is exactly the role of continue.

Where continue sends the control

for loop→skip rest of body→update expression (i++)→test condition
while loop→skip rest of body→test condition
do-while loop→skip rest of body→test condition at bottom

The one detail that matters most is in the first row. In a for loop, continue still runs the update expression. In a while loop there is no update expression built into the loop header, so whatever increments your counter must run before continue, or it will never run at all.

Syntax and Rules

continue;

That is the whole syntax: the keyword and a semicolon. The rules are short:

  • It must appear inside a for, while or do-while body. Using it outside a loop is a compile error.
  • It affects only the innermost enclosing loop. A continue in an inner loop never skips an outer iteration.
  • Inside a switch that sits inside a loop, continue belongs to the loop, not the switch. (We cover switch in a later lecture.)
  • It is almost always placed inside an if, because an unconditional continue would make the code after it unreachable.

Example 1: Skip Invalid Samples in a For Loop

A temperature sensor driver stores raw samples in an array. A value of -1 means the conversion failed. We want the sum and average of the good samples only.

#include <stdio.h>

int main(void)
{
    int readings[8] = {412, -1, 398, 405, -1, 420, 415, 401};
    int count = 8, valid = 0, sum = 0;

    for (int i = 0; i < count; i++) {
        if (readings[i] < 0) {
            printf("Sample %d invalid, skipped\n", i);
            continue;
        }
        sum += readings[i];
        valid++;
    }
    printf("Valid = %d, Sum = %d, Avg = %.2f\n", valid, sum, (float)sum / valid);
    return 0;
}

Build and run it:

$ gcc -std=c11 -Wall cont1.c -o cont1
$ ./cont1
Sample 1 invalid, skipped
Sample 4 invalid, skipped
Valid = 6, Sum = 2451, Avg = 408.50

Notice the cast (float)sum. Without it, integer division would silently throw away the fractional part of the average. The important point for this lecture is that the two invalid samples never reach sum += readings[i], yet the loop counter still marches from 0 to 7 because the for loop’s update expression runs after every continue.

Example 2: Continue in a While Loop

Now the same idea with a while loop, where the counter update is our responsibility. We add up only the odd numbers from 1 to 10.

#include <stdio.h>

int main(void)
{
    int i = 0, odd_sum = 0;

    while (i < 10) {
        i++;
        if (i % 2 == 0)
            continue;
        odd_sum += i;
        printf("Added %d, running total %d\n", i, odd_sum);
    }
    printf("Sum of odd numbers 1..10 = %d\n", odd_sum);
    return 0;
}
Added 1, running total 1
Added 3, running total 4
Added 5, running total 9
Added 7, running total 16
Added 9, running total 25
Sum of odd numbers 1..10 = 25

The increment i++ sits above the if, so it happens on every pass whether or not continue fires. That ordering is not an accident, and the next section shows what happens when it is reversed.

The Infinite Loop Trap

Warning: In a while or do-while loop, never put the counter update after a continue that can skip it.

Here is the buggy version of Example 2, shown for study only. Do not run it unless you are ready to press Ctrl+C:

int i = 0;
while (i < 10) {
    if (i % 2 == 0)
        continue;      /* i is still 0, condition is true again */
    i++;
}

When i is 0, the condition i % 2 == 0 is true, continue jumps to the loop test, i < 10 is still true, and the same iteration repeats forever. On a PC this burns CPU. On a microcontroller it can be worse: the main loop appears alive but never services the peripheral, and unless a watchdog resets the chip, the board just hangs. A for loop avoids this trap by design because its update expression always runs.

Example 3: Retry Until Enough Good Inputs

Sometimes you want continue to leave the counter alone on purpose. Suppose a command parser needs exactly five valid positive numbers. Bad values are rejected and do not count toward the five. Instead of reading from a keyboard, this program feeds values from an array so the output is repeatable.

#include <stdio.h>

int main(void)
{
    int attempts[] = {7, -3, 12, -9, 5, 20, 8, 1, 14, 6, 9};
    int idx = 0, accepted = 0, total = 0;

    while (accepted < 5) {
        int v = attempts[idx++];
        if (v < 0) {
            printf("Rejected %d, asking again\n", v);
            continue;
        }
        total += v;
        accepted++;
        printf("Accepted #%d = %d\n", accepted, v);
    }
    printf("Total = %d, Avg = %.1f\n", total, total / 5.0);
    return 0;
}
Accepted #1 = 7
Rejected -3, asking again
Accepted #2 = 12
Rejected -9, asking again
Accepted #3 = 5
Accepted #4 = 20
Accepted #5 = 8
Total = 52, Avg = 10.4

Here the loop condition watches accepted, and continue skips the line that increases it. The loop does not advance toward its goal on a rejected value, but it does consume the next input because idx++ runs when the value is read. Progress is guaranteed by the input index, which is why this loop terminates. Always ask yourself: what changes on the continue path so that the condition can eventually become false?

Example 4: Continue Inside a For Loop With Printing

A tiny scheduler loop runs tasks 1 to 5 but skips task 3 because it is disabled.

#include <stdio.h>

int main(void)
{
    for (int i = 1; i <= 5; i++) {
        if (i == 3)
            continue;
        printf("Task %d running\n", i);
    }
    printf("Loop finished\n");
    return 0;
}
Task 1 running
Task 2 running
Task 4 running
Task 5 running
Loop finished

Continue vs Break

Featurecontinuebreak
Effect on the loopSkips only the current iterationEnds the loop completely
Where control goesUpdate expression or condition testFirst statement after the loop
Allowed inLoops onlyLoops and switch
Typical embedded useIgnore a bad sample or disabled channelStop on first error or found item
Main riskSkipping the counter update in while loopsLeaving state half-initialised

Continue Statement in C: Real-World Uses

  • Sensor filtering: ignore out-of-range ADC readings before averaging.
  • Buffer parsing: skip whitespace or padding bytes while scanning a UART receive buffer.
  • Channel scanning: skip disabled channels when polling a multi-channel peripheral.
  • Table processing: bypass entries marked as unused in a configuration table.

Common Mistakes

  1. Placing the increment after continue in a while loop, causing a hang.
  2. Expecting continue to leave an outer loop. It only affects the innermost loop.
  3. Using continue where an else branch is clearer. If the loop body has only two short paths, if-else may read better.
  4. Writing continue as the last statement of a loop body. It does nothing there and just confuses readers.
  5. Integer division in the average calculation, as in sum / valid, which truncates the result.

Best Practices

  • Use continue to handle rare “bad” cases early, so the main logic stays at one indentation level.
  • Keep the counter update in the loop header (a for loop) whenever you plan to use continue.
  • Add a short comment explaining why the iteration is skipped.
  • If you must follow a coding standard such as MISRA C, check its rules on continue and multiple exits from loops before using it widely.
  • Compile with -Wall so the compiler can warn about suspicious loop conditions.

Performance and Safety Notes

The continue statement costs nothing extra at run time; the compiler turns it into a plain jump. The real cost is human: a loop with several continue statements is harder to reason about. In safety-critical embedded code, prefer a single clear skip condition. In firmware that feeds a hardware watchdog, remember that a continue which skips the watchdog-refresh line can let the watchdog reset the device. Place the refresh at the top of the loop or in the loop header’s scope so every path reaches it.

Summary and Key Takeaways

  • continue ends only the current iteration, never the loop.
  • In a for loop, the update expression still runs. In while and do-while loops, control goes to the condition test.
  • It affects only the innermost loop.
  • Always make sure something changes on the continue path, or the loop may never finish.
  • It is excellent for filtering invalid data in embedded C programming.

You now know how the continue statement in C reshapes loop flow without the cost of extra nesting. In the next lecture of this free C programming course we look at the goto statement, the other jump statement, and why it has such a mixed reputation.

Frequently Asked Questions

Can I use continue outside a loop in C?

No. The continue statement is only valid inside a for, while or do-while body. The compiler reports an error otherwise.

Does continue skip the increment in a for loop?

No. In a for loop, control jumps to the update expression, so the counter still advances before the condition is tested.

What is the difference between break and continue in C?

break leaves the loop entirely. continue only abandons the current iteration and lets the loop run again.

Why does my while loop hang when I use continue?

Most likely the counter update sits after the continue, so it is skipped and the condition never changes. Move the update above the continue.

Does continue work inside a switch statement?

Yes, if the switch is inside a loop. The continue applies to the enclosing loop, not to the switch.

Is continue used in embedded C programming?

Yes. Firmware loops use it to skip invalid samples, disabled channels or padding bytes while keeping the main code flat.

Keep Learning Embedded C for Free

This free C tutorial is part of a complete free embedded C course on EmbeddedPathashala. Continue with the next lecture to master goto and switch.

Need Help or More Information?

Questions about this free C programming course, embedded C training or career guidance? Get in touch.

2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *