CKTIS / C++
case — a value label in switch
The case keyword marks an entry point in a switch statement for a particular constant value. A label does not end a branch: without break or another transfer of control, execution continues onward, including through subsequent labels.
Version: C++23; repository compiler flag, production runtime not attested
Syntax
switch (expression) {
case constant_expression:
statement;
break;
default:
statement;
}case creates a label inside switch; it belongs to the nearest enclosing switch. When the condition matches the label's value, execution starts at the statement following it. Optional default handles no match; without it, an unmatched condition skips the switch body.
In case constant_expression:, the expression must be a converted constant expression of the condition's type after required conversions and integral promotions. A value known only at runtime is insufficient. Two labels in the same switch cannot have equal values after conversion.
Labels do not stop execution. Consecutive case labels can share statements, as case 1: and case 2: do in the example. break exits the nearest enclosing loop or switch; here it exits switch.
A case does not create a scope. Use a { ... } block for branch-local variables: jumping to another label must not illegally bypass initialization. The example uses rules available in C++23; the supplied source is an evolving draft, not a frozen C++23 publication.
Examples
#include <iostream>
int main() {
constexpr int choice = 2;
switch (choice) {
case 1:
case 2: {
const int result = choice * 10;
std::cout << result << '\n';
break;
}
default:
std::cout << "other\n";
break;
}
}
Result
20
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.