`break` — C++ Keyword
`break` — C++ Keyword
The break keyword in C++: exits the nearest enclosing loop or switch statement.
`break` — C++ Keyword
The break keyword in C++: exits the nearest enclosing loop or switch statement.
Use reference pages to confirm names, categories, nearby facilities, and the constraints that matter before writing or reviewing code.
breakExits the nearest enclosing for, while, do/while, or switch statement immediately.
break;
#include <print>
#include <vector>
int main() {
// Exit a loop early
for (int i = 0; i < 10; ++i) {
if (i == 5) break;
std::print("{} ", i); // 0 1 2 3 4
}
std::println();
// Exit a switch
int val = 2;
switch (val) {
case 1: std::println("one"); break;
case 2: std::println("two"); break; // executed, then exits switch
case 3: std::println("three"); break;
}
}
break only exits the innermost enclosing loop or switch; use a flag or goto to break out of nested loops.switch nested in a loop, break exits the switch, not the loop.breakint main() {
// Pick one facility from this reference page.
// Write the smallest program that exercises its main precondition,
// complexity rule, or lifetime constraint before scaling up.
return 0;
}