break immediately exits the nearest enclosing for, while, do-while loop, or switch. Control jumps to the first statement after that block.
Remember
break; // one keyword — leave now
in a loop → stop all remaining iterations
in a switch → stop fall-through to next case
nested → exits innermost loop/switch only
not valid outside a loop or switch
Place break where further work in the current loop or switch should stop.
Write a loop or switch as usual.
Add a clear condition: if (done) break; (or put break at the end of a case).
Remember: only the nearest enclosing loop or switch ends.
Continue with code after the closing } of that block.
Piece
Required?
Role
Enclosing loop / switch
Yes
break is illegal outside these
Trigger condition
Usually
When to leave (found, error, menu exit)
Code after the block
Optional
Runs next after break
Pseudocode
Pseudocode
for each item in list:
if item matches target:
save item
break // leave loop now
// more work skipped after break
// continue here after the loop
// or in switch:
switch choice:
case A:
do A
break // do not fall into case B
case B:
do B
break
Reference
Cheat Sheet
Keep these facts handy while writing loop and switch code.
Topic
Detail
Syntax
break;
Valid in
for, while, do-while, switch
Scope
Nearest enclosing loop or switch only
vs continue
break leaves; continue skips to next iteration
vs return
return leaves the whole function
switch tip
Almost always end each case with break
Compile
gcc file.c -std=c11 -o out
Try it
Live Preview
Simulate how break stops a loop. Same idea as the C examples below.
Browser simulation of break control flow (same idea as the C examples below).
Result
i = 0 … 4 printed
break at i == 5
loop ends — 5–9 never run
Trace
Worked Walkthrough — Search and Stop
Find the first value greater than 50 in {12, 34, 67, 89, 23}, then leave the loop.
Trace
values = [12, 34, 67, 89, 23]
target: first value > 50
i=0 → 12 (keep going)
i=1 → 34 (keep going)
i=2 → 67 → found! break
89 and 23 are never checked
found = 67
After the loop: print "First value > 50: 67"
Code
C Examples
Compile every example with gcc file.c -std=c11 -o out. Short programs you can copy and run.
Example 1 — break in a for Loop
Stop counting when i reaches 5. Same idea works in while and do-while.
C
#include <stdio.h>
int main(void) {
for (int i = 0; i < 10; i++) {
if (i == 5) {
break;
}
printf("%d\n", i);
}
return 0;
}
Output
0
1
2
3
4
How It Works
Loop starts.i goes from 0 toward 9.
Print first. For i 0–4 the if is false, so each value prints.
Hit the stop. When i == 5, break runs before printf.
Leave the loop. Values 5–9 never print; control continues after the for.
Example 2 — break in a switch
End the matched case so later cases do not run. See the full switch tutorial for more.
C
#include <stdio.h>
int main(void) {
int day = 3;
switch (day) {
case 1:
printf("Monday\n");
break;
case 2:
printf("Tuesday\n");
break;
case 3:
printf("Wednesday\n");
break;
default:
printf("Invalid day\n");
}
return 0;
}
Output
Wednesday
How It Works
Match a case.day is 3, so control jumps to case 3.
Run the body. It prints Wednesday.
Break out.break leaves the whole switch.
Avoid fall-through. Without break, C would keep running the next cases—often a bug.
Example 3 — Search and Stop
Find the first value greater than 50, then leave so later elements are not scanned.
C
#include <stdio.h>
int main(void) {
int values[] = {12, 34, 67, 89, 23};
int n = 5;
int found = -1;
for (int i = 0; i < n; i++) {
if (values[i] > 50) {
found = values[i];
break;
}
}
if (found != -1) {
printf("First value > 50: %d\n", found);
} else {
printf("None found\n");
}
return 0;
}
Output
First value > 50: 67
How It Works
Scan in order. Check 12, then 34—both are ≤ 50.
Match. At index 2, 67 > 50 is true; store it in found.
Break. Skip 89 and 23—no need to keep looking.
Report. After the loop, print the first match (or “None found”).
Edge Cases & Pitfalls
Check these before relying on break in real projects.
nested
Only one level exits
In nested loops, break leaves the inner loop; the outer loop keeps going. Use a flag, return, or rare goto to leave both.
switch
Forgotten break
Missing break after a case causes fall-through. That is legal C—but usually unintentional.
scope
Outside loop / switch
break at file or function scope (not inside a loop or switch) is a compile error.
compare
Not the same as continue
continue skips one iteration; break ends the loop. Mixing them up changes program logic completely.
Analysis
Performance Notes
Approach
Cost
Best for
break on find
Stops early (often fewer iterations)
Search, validation, menus
Always scan full range
Full loop cost every time
When every element must be processed
Flag + dual conditions
Tiny check overhead; clearer multi-exit
Leaving nested loops without goto
break itself has no meaningful overhead. The win is skipping work you no longer need—especially on large arrays.
One line:break leaves the nearest loop or switch now—use it when the rest should not run.
Frequently Asked Questions
break immediately exits the nearest enclosing loop (for, while, do-while) or switch block. Execution continues at the first statement after that loop or switch.
No. break only exits the innermost loop or switch that contains it. To leave multiple nested loops, use a flag, refactor into a function with return, or goto (rare).
No. break is only valid inside a loop or switch. Using it elsewhere is a compile error.
break exits the entire loop. continue skips the rest of the current iteration and jumps to the next loop cycle. See the continue tutorial next in the sidebar.
Without break after a case, C falls through and runs the next case too. break stops that fall-through so only the matched case runs.
break skips any remaining statements in the current iteration and leaves the loop entirely. Code after the loop (below the closing brace) runs next.