What is Continue Statement in C- C Programming Course in Telugu

🚀 Learn BLE on Linux (BlueZ) – Programming Part 1

Hands-on Bluetooth Low Energy course. Special price: ₹399

Enroll Now on Udemy →
💬 Need help or career guidance? Ask us!Contact Us →

Control Statements | Free C Programming Course

Continue Statement in C

Learn where continue really jumps in for and while loops, with embedded-style examples you can compile and run.

The continue statement in C looks tiny, yet it hides a surprising number of firmware bugs and a favourite interview trick question. In this lecture of our free C programming course you will learn exactly where continue jumps inside a for loop and inside a while loop, why the two behave differently, and how embedded C programming teams use it to ignore bad sensor readings. If you are following this free C tutorial to prepare for embedded systems interviews, this is a topic you should never guess on. Every example below is written from scratch, compiled with gcc, and the output you see is the real output.

What You Will Learn

  • What the continue statement in C does, and where control actually lands next
  • Why continue in a for loop is safe but in a while loop can freeze your program
  • How to trace loop variables by hand, the skill that exam questions and interviews test
  • A practical embedded use: filtering failed ADC samples without nested if blocks
  • How continue differs from break and return
  • Common mistakes, debugging tips and coding-standard concerns

Prerequisites

You should be comfortable with the for and while loops from the earlier lectures of this chapter, with the increment operator ++, and with writing, compiling and running a small C file using gcc on Linux or any embedded toolchain host. If you need a refresher, finish the loop lectures first; they are part of this free embedded C course for a reason.

What the Continue Statement in C Actually Does

A loop body is a list of statements that the program runs again and again. Sometimes halfway through the body you discover that the current round is not worth finishing: the sample is invalid, the packet header is wrong, the character is a space. You do not want to leave the loop completely, that would be break. You only want to abandon this iteration and start the next one. That is the whole job of continue.

The important detail is the phrase “start the next one”. C does not simply jump to the top of the body. Where it lands depends on the kind of loop, and that is exactly what trips people up.

Loop typeWhere continue jumpsIs the update expression executed?
for (init; test; update)To the update expression, then the testYes, always
while (test)Straight to the testThere is no built-in update, so only if you wrote it before continue
do { } while (test)To the test at the bottomSame as while: your own update must come first

Where continue lands in a for loop

init (once)→test→body→continue hit→update→test again

Read the diagram left to right: when continue fires, the rest of the body is skipped, but the update part of the for header still runs. That single fact explains why a for loop with continue is almost always safe: the counter keeps moving.

Continue in For and While Loops Side by Side

The best way to understand the continue statement in C is to run the same task in both loop styles. The program below prints the numbers 1 to 8 but skips 4. Pay attention to where the increment lives in each loop.

File: continue.c

#include <stdio.h>

int main(void)
{
    int n;

    printf("for   : ");
    for (n = 1; n <= 8; n++) {
        if (n == 4)
            continue;              /* jumps to n++ */
        printf("%d ", n);
    }

    printf("\nwhile : ");
    n = 0;
    while (n < 8) {
        n++;                       /* update FIRST, then continue is safe */
        if (n == 4)
            continue;
        printf("%d ", n);
    }
    printf("\n");
    return 0;
}

Build and run it:

$ gcc -std=c11 -Wall -Wextra -o continue continue.c
$ ./continue
for   : 1 2 3 5 6 7 8 
while : 1 2 3 5 6 7 8 

Both loops print the same sequence, but they get there differently. Here is a hand trace of the while version, the form you should practise on paper:

Trace of the while loop (n starts at 0)

Roundn after n++n == 4 ?Action
11noprint 1
22noprint 2
33noprint 3
44yescontinue, nothing printed
55noprint 5
6 to 86, 7, 8noprint 6, 7, 8

Rule to remember

In a while or do-while loop, put the statement that moves the loop forward above any continue. In a for loop the header does that job for you.

The Infinite Loop Trap with Continue in While

Now the dangerous version. Suppose the increment sits at the bottom of the body, after the if that contains continue:

int n = 0;

while (n < 8) {
    if (n == 4)
        continue;          /* jumps back to the test, n is still 4 */
    n++;
}

Counting goes fine until n reaches 4. Then continue sends control back to the test, the test passes because 4 is less than 8, the same if fires again, and n++ is never reached. The loop spins forever at full CPU. On a desktop that means a frozen terminal; on a microcontroller it means the hardware watchdog resets your board, which is a painful bug to chase because the symptom looks like a random reboot.

The stuck while loop never leaves this cycle

n = 4→test n < 8 passes→if (n == 4) taken→continue→back to test, n still 4

Warning: if you test this trap yourself, run it under timeout 2 ./a.out so that Ctrl+C is not your only exit.

Embedded C Programming Use Case: Skipping Bad Samples

Real firmware reads sensors that fail now and then. An ADC driver might return -1 when a conversion times out. Rather than wrap the whole processing code inside an if (sample is valid) block, many engineers use continue as a guard clause: reject the bad case early and keep the good path flat and readable.

File: sensor.c

#include <stdio.h>

#define SAMPLES 6

int main(void)
{
    int adc[SAMPLES] = { 12, -1, 15, -1, 18, 20 };   /* -1 = failed read */
    int i, good = 0, sum = 0;

    for (i = 0; i < SAMPLES; i++) {
        if (adc[i] < 0)
            continue;              /* skip bad sample, go straight to i++ */
        sum += adc[i];
        good++;
    }
    printf("valid = %d, sum = %d, average = %.2f\n",
           good, sum, (double)sum / good);
    return 0;
}
$ gcc -std=c11 -Wall -Wextra -o sensor sensor.c
$ ./sensor
valid = 4, sum = 65, average = 16.25

Two of the six samples are -1, so four are valid: 12 + 15 + 18 + 20 = 65, and 65 divided by 4 is 16.25. Notice the cast (double)sum in the printf argument. Without it, the division would be integer division and the average would silently become 16, a classic embedded rounding bug.

Because this is a for loop, i++ still runs when continue fires, so every sample is visited exactly once. That is the behaviour you want, and it is why this guard-clause style is popular in C for embedded systems.

Continue vs Break vs Return

StatementWhat it abandonsWhere control goesTypical use
continueOnly the current iterationNext iteration of the same loopSkip invalid items
breakThe whole innermost loop (or switch)First statement after that loopStop when the answer is found
returnThe whole functionThe callerFatal error or final result
goto labelAnything up to the labelThe labelCleanup paths in drivers

A good habit when reading unfamiliar code: ask which of these four is leaving, and where it lands. Most loop bugs come from assuming the wrong answer.

Continue Inside Nested Loops

continue always belongs to the innermost enclosing loop. If you write a loop inside a loop and put continue in the inner one, the outer loop is not affected at all; only the inner iteration is skipped. C has no labelled continue like some other languages. When you really want to skip to the next outer iteration, the usual options are a flag variable that the inner loop sets and the outer loop checks, moving the inner loop into its own function and using return, or, as a last resort, a goto to a label at the end of the outer body.

Also remember that continue is only legal inside a loop. Using it inside a plain if or inside a switch that is not itself inside a loop is a compile-time error. We look at that exact rule in the lecture on switch statements.

Common Mistakes and Troubleshooting

SymptomLikely causeFix
Program hangs after a certain valueIncrement placed after continue in a while loopMove the update above the continue, or convert to a for loop
Skipped item is still countedCounter incremented before the continue checkDo the validity check first, count afterwards
Averages come out as whole numbersInteger division of sum by countCast one operand to double before dividing
Division by zero crashEvery sample was skipped, so the count is zeroCheck the count before dividing
continue seems to affect the wrong loopIt is inside an inner loopUse a flag or refactor into a function
Compiler error: continue statement not within a loopcontinue used outside any loopUse break or return instead, or restructure

Notice the fourth row of that table in our sensor demo. If all six samples had been -1, good would be zero and the average calculation would divide by zero. Guard that case before printing in production code.

Best Practices, Performance and Safety Notes

  • Prefer for over while when you need continue. The update expression is part of the header, so you cannot forget it.
  • Use continue for early rejection, not for tangled logic. One or two guard clauses at the top of a loop are clear; five scattered continues are not.
  • Performance: continue costs nothing extra. Compilers turn it into the same jump instruction they would generate for an equivalent if-else, so choose by readability.
  • Safety: make sure a skipped iteration does not also skip cleanup such as releasing a buffer or re-enabling an interrupt. If cleanup is needed, do it before the continue.
  • Coding standards: older MISRA C:2004 guidance forbade continue, and some teams still avoid it. Check what your project allows before using it in safety-critical code.
  • Compile with warnings on. Use -Wall -Wextra so that suspicious loop conditions are flagged early.

Summary and Key Takeaways

  • The continue statement in C skips the rest of the current iteration and starts the next one.
  • In a for loop it lands on the update expression, so the counter still advances.
  • In while and do-while loops it lands on the test, so your update must come before it.
  • Continue only affects the innermost loop and is illegal outside loops.
  • Used as a guard clause it keeps embedded C code flat, but always protect against a zero count when averaging.

Conclusion

Mastering the continue statement in C is less about memorising syntax and more about tracing control flow carefully. When you can say, without running the program, which statement executes after a continue, you can read almost any loop in real firmware. In the next lecture we move on to variable scope inside loops, where a single extra declaration can quietly change the meaning of your program. Keep practising on paper first, then compile to check yourself, exactly the way this free embedded C course teaches it.

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. In a for loop the update expression runs first; in a while loop control goes straight to the condition.

Is continue in a for loop different from continue in a while loop?

Yes. A for loop executes its update expression after continue, so the counter advances. A while loop has no built-in update, so if your increment sits after the continue, the loop can run forever.

What is the difference between break and continue in C?

break ends the whole innermost loop or switch. continue ends only the current iteration and the loop keeps running.

Can I use continue inside a switch statement?

Only if the switch is itself inside a loop, in which case continue applies to that loop. A switch outside any loop with a continue inside it fails to compile.

Does continue work with nested loops?

It always applies to the innermost enclosing loop. To skip to the next outer iteration, use a flag variable, a helper function with return, or a goto label.

Is using continue bad practice in embedded C?

Not inherently. It is fine for early rejection of invalid data. Some coding standards such as MISRA C:2004 discouraged it, so follow your project rules and keep its use simple.

Why did my loop with continue hang the microcontroller?

Most likely the loop variable is updated after the continue in a while loop, so it never changes. The watchdog then resets the chip. Move the update before the continue or use a for loop.

Does continue make the program slower or faster?

Neither. The compiler generates an ordinary jump, equivalent to the if-else version, so choose the form that reads best.

Keep Going With the Free C Course

This free C tutorial is built for students who want solid embedded C programming fundamentals. Continue to the next lecture to master variable scope in loops.

💬 Need help or career guidance? Ask us!Contact Us →

2 Comments

Leave a Reply

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

×

🚀 BLE on Linux (BlueZ)

Hands-on course. Only ₹399

Enroll Now →