C++ switch Keyword

The switch keyword in C++ is used to create a multi-way branch statement. It allows a variable or expression to be tested for equality against multiple values, called case labels. Each case represents a possible value for the variable and executes the associated block of code when matched.

The switch statement is often used as an alternative to a sequence of ifelse if statements, making the code more readable and easier to maintain.


Syntax

</>
Copy
switch (expression) {
    case value1:
        // Code to execute when expression == value1
        break;
    case value2:
        // Code to execute when expression == value2
        break;
    ...
    default:
        // Code to execute when none of the cases match
}
expression
An integral or enumerated value that determines which case to execute.
case value
A possible value for expression. The associated block of code executes if expression == value.
default
Optional. Specifies the code to execute if no case matches.

Examples

Example 1: Basic switch Statement

This example demonstrates how to use the switch statement with integer values.

</>
Copy
#include <iostream>
using namespace std;

int main() {
    int day = 3;

    switch (day) {
        case 1:
            cout << "Monday" << endl;
            break;
        case 2:
            cout << "Tuesday" << endl;
            break;
        case 3:
            cout << "Wednesday" << endl;
            break;
        case 4:
            cout << "Thursday" << endl;
            break;
        case 5:
            cout << "Friday" << endl;
            break;
        default:
            cout << "Invalid day" << endl;
    }

    return 0;
}

Output:

Wednesday

Explanation:

  1. The variable day is evaluated in the switch statement.
  2. The case labeled 3 matches, and the corresponding block of code executes, printing “Wednesday.”
  3. The break statement prevents the execution from falling through to subsequent cases.

Example 2: Using default in a switch Statement

The default case executes when no other case matches the expression.

</>
Copy
#include <iostream>
using namespace std;

int main() {
    int num = 10;

    switch (num) {
        case 1:
            cout << "One" << endl;
            break;
        case 2:
            cout << "Two" << endl;
            break;
        default:
            cout << "Number not in range" << endl;
    }

    return 0;
}

Output:

Number not in range

Explanation:

  1. The num variable does not match any of the specified cases.
  2. The default block executes and prints “Number not in range.”
  3. The default case is optional, but it’s good practice to include it for handling unexpected inputs.

Example 3: Fall-Through Behavior

By omitting break, multiple cases can execute sequentially (fall-through behavior).

</>
Copy
#include <iostream>
using namespace std;

int main() {
    int num = 2;

    switch (num) {
        case 1:
            cout << "Case 1" << endl;
        case 2:
            cout << "Case 2" << endl;
        case 3:
            cout << "Case 3" << endl;
        default:
            cout << "Default case" << endl;
    }

    return 0;
}

Output:

Case 2
Case 3
Default case

Explanation:

  1. The num variable matches case 2, and the code for case 2 executes.
  2. Since there is no break, the execution falls through to case 3 and the default case.
  3. Fall-through behavior is often undesirable and can lead to bugs, so break statements should be included unless explicitly intended.

Key Points to Remember about switch Keyword

  1. The switch statement is used for multi-way branching based on an expression.
  2. Cases must have constant values, such as literals or constexpr variables.
  3. The break statement prevents fall-through behavior; if omitted, subsequent cases execute.
  4. The default case is optional but recommended for handling unexpected values.
  5. The switch statement is often more efficient and readable than multiple ifelse if statements.