`switch` — C++ Keyword
`switch` — C++ Keyword
The switch keyword in C++: selects among multiple execution paths based on an integral value.
`switch` — C++ Keyword
The switch keyword in C++: selects among multiple execution paths based on an integral value.
Use reference pages to confirm names, categories, nearby facilities, and the constraints that matter before writing or reviewing code.
switchSelects among multiple execution paths based on the value of an integral or enumeration expression.
switch (expression) {
case constant1: statements; break;
case constant2: statements; break;
default: statements;
}
switch (init-statement; expression) { ... } // C++17 init-statement form
#include <print>
int main() {
int day = 3;
switch (day) {
case 1: std::println("Monday"); break;
case 2: std::println("Tuesday"); break;
case 3: std::println("Wednesday"); break; // executed
case 4: std::println("Thursday"); break;
case 5: std::println("Friday"); break;
default: std::println("Weekend");
}
}
break, execution falls through to the next case.case labels may share one body (implicit fall-through for grouping).[[fallthrough]] to document intentional fall-through.switchint 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;
}