C:C Break and Continue Statements in Loops
C•Loop Control

C Break and Continue Statements in Loops

break exits the innermost loop immediately; continue skips to the next iteration.

Syntax

if (condition) break; if (condition) continue;

Parameters & Values

Key components and acceptable values for this structure:

ParameterTypeDescription
breakKeywordExits loop completely.
continueKeywordSkips remainder of current iteration.

Ready-to-Run Code Example

Executable in Compilra

Run this complete, working example directly without installing C locally:

#include <stdio.h>

int main(void) {
    int sensorReadings[] = {22, 24, -999, 25, 100, 26};
    int count = sizeof(sensorReadings) / sizeof(sensorReadings[0]);

    printf("Processing sensor stream:\n");
    for (int i = 0; i < count; i++) {
        int val = sensorReadings[i];
        if (val < 0) {
            printf("  [SKIP] Glitch reading: %d\n", val);
            continue;
        }
        if (val >= 100) {
            printf("  [HALT] Critical temperature %dC detected! Shutting down.\n", val);
            break;
        }
        printf("  [OK] Temperature nominal: %dC\n", val);
    }

    return 0;
}
Expected Output:
Processing sensor stream:
  [OK] Temperature nominal: 22C
  [OK] Temperature nominal: 24C
  [SKIP] Glitch reading: -999
  [OK] Temperature nominal: 25C
  [HALT] Critical temperature 100C detected! Shutting down.

How It Works: Step-by-Step

  • Lines 11-14: continue skips negative reading -999.
  • Lines 15-18: break halts loop upon temperature reaching 100.

Key Concepts

  • continue skips straight to the loop increment expression.
  • break terminates only the innermost enclosing loop.

Common Mistake: Using continue in while loop without incrementing counter

Skipping counter update in a while loop leads to an immediate infinite loop.

❌ Incorrect
while (i < 5) {
    if (i == 2) continue; // i is never incremented!
    i++;
}
✅ Correct
while (i < 5) {
    if (i == 2) { i++; continue; }
    i++;
}

Related C Tutorials & Compiler

Run and test C on Compilra

Execute this code snippet with pre-supplied standard input (stdin) in Compilra’s cloud compiler.

C Online Compiler