0
0
CHow-ToBeginner · 3 min read

How to Use Continue Statement in C: Syntax and Examples

In C, the continue statement skips the rest of the current loop iteration and moves control to the next iteration. It works inside loops like for, while, and do-while. Use continue to avoid executing certain code when a condition is met.
📐

Syntax

The continue statement is used inside loops to skip the remaining code in the current iteration and jump to the next iteration.

  • Inside a for loop: It moves control to the update expression.
  • Inside while or do-while loops: It jumps to the loop condition check.
c
for (initialization; condition; update) {
    if (condition_to_skip) {
        continue;
    }
    // code to execute if not skipped
}
💻

Example

This example prints numbers from 1 to 5 but skips printing the number 3 using continue.

c
#include <stdio.h>

int main() {
    for (int i = 1; i <= 5; i++) {
        if (i == 3) {
            continue; // skip when i is 3
        }
        printf("%d\n", i);
    }
    return 0;
}
Output
1 2 4 5
⚠️

Common Pitfalls

Some common mistakes when using continue include:

  • Using continue outside loops causes a compile error.
  • Placing continue in loops without braces can cause confusion about which statements are skipped.
  • Overusing continue can make code harder to read.
c
#include <stdio.h>

int main() {
    int i = 0;
    // Wrong: continue outside loop causes error
    // continue;

    for (i = 0; i < 5; i++)
        if (i == 2)
            continue; // skips only the next statement
        else
            printf("%d\n", i); // This runs only when i is not 2

    // Correct way with braces:
    for (i = 0; i < 5; i++) {
        if (i == 2) {
            continue;
        }
        printf("%d\n", i);
    }
    return 0;
}
Output
0 1 3 4 0 1 3 4
📊

Quick Reference

Summary tips for using continue in C:

  • Use continue only inside loops.
  • It skips the rest of the current iteration.
  • In for loops, control moves to the update step.
  • In while and do-while, control moves to the condition check.
  • Use braces { } to avoid confusion with multiple statements.

Key Takeaways

The continue statement skips the rest of the current loop iteration and proceeds to the next one.
Use continue only inside loops like for, while, or do-while.
Always use braces to clearly define the scope when using continue with multiple statements.
Avoid overusing continue to keep code readable and simple.
Using continue outside loops causes a compile-time error.