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:
| Parameter | Type | Description |
|---|---|---|
| break | Keyword | Exits loop completely. |
| continue | Keyword | Skips remainder of current iteration. |
Ready-to-Run Code Example
Executable in CompilraRun 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
C While and Do-While Conditional Repetition Pipelines
Construct robust while pre-test loops and do-while post-test execution pipelines in C with safety guards and retry counters.
C While and Do-While Loops with Examples
Learn conditional while loops and post-test do-while loops in C.
C Short-Circuit Ternary Logic and Conditional Expressions
Implement short-circuit logical expressions (&&, ||) and compact ternary operators in C for defensive null checks and inline branching.
C Online Compiler
Open in Compilra IDE
Run and test C on Compilra
Execute this code snippet with pre-supplied standard input (stdin) in Compilra’s cloud compiler.