`case` — C++ Keyword

`case` — C++ Keyword

The case keyword in C++: defines a labeled branch inside a switch statement.

How to use this reference page

Use reference pages to confirm names, categories, nearby facilities, and the constraints that matter before writing or reviewing code.

  • Scan the top of the page first to identify the primary types, functions, or algorithm families involved.
  • Use the nearby-page links when your question is really about a companion header, related algorithm family, or broader subsystem.
  • Validate tricky behavior with a small compileable example before relying on memory for details like invalidation, ordering, allocation, or lifetime rules.

case

Defines a labeled branch inside a switch statement. Execution jumps to the matching case label when the switch expression equals its constant.

Syntax

switch (expression) {
    case constant-expression: statements
    case constant-expression: statements
    default: statements
}

Example

#include <print>

int main() {
    char c = 'e';

    switch (c) {
        case 'a': case 'e': case 'i':
        case 'o': case 'u':
            std::println("vowel");   // executed
            break;
        default:
            std::println("consonant");
    }
}

Notes

Example in practice

int 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;
}