break Exit the nearest loop or switch
break Exit the nearest loop or switch
break exits the nearest loop or switch. It leaves one level and cannot exit ordinary if; in switch, it prevents fall-through but is optional.
| Category | Control flow |
|---|---|
| Kind | Keyword / Statement |
| C standard | C89 / C90 |
📝 Syntax
while (1) { if (cond) break; }
▶ Example
Edit the C code below and click Run; the server compiles and executes it:
Output:
stopped at 3
🏷️ Related control-flow keywords
📚 Related Tutorials
❓ FAQ
QCan one break exit two levels of nested loops?
ANo. It terminates only the nearest enclosing loop or switch. To exit multiple levels, refactor into a function and return, use a clear state flag, or, where appropriate, use goto for centralized cleanup.
QCan break appear inside an ordinary if statement?
AOnly when that if is inside a loop or switch; break then terminates that enclosing statement. An if by itself does not make break valid without an enclosing loop or switch.
QIs forgetting break in a switch branch always a compilation error?
AUsually not; the syntax itself is valid, and execution continues into subsequent labels. This is called fall-through. If code sharing was not intentional, the result may be unexpected, so enable relevant compiler warnings.