C++ case Keyword

The case keyword in C++ is used within a switch statement to define specific actions based on the value of a variable or expression. Each case represents a distinct scenario, and the program executes the statements associated with the matching case. The case keyword is typically followed by a constant value, and its block of code ends with a break statement to prevent fall-through to the next case.

The switch statement and case blocks are often used as an alternative to multiple if-else conditions for better readability and performance.


Syntax

</>
Copy
switch (expression) {
    case constant1:
        // Statements for constant1
        break;
    case constant2:
        // Statements for constant2
        break;
    ...
    default:
        // Default statements
}
switch
The keyword that initiates the switch statement, evaluating the expression.
case
Specifies a constant value to match against the expression. If matched, the associated block of code executes.
break
Exits the switch statement to prevent fall-through to subsequent cases.
default
Specifies the block of code to execute if no case matches. This is optional.

Examples

Example 1: Basic switch with case

This example demonstrates how to use the case keyword to match specific values.

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

int main() {
    int choice = 2;

    switch (choice) {
        case 1:
            cout << "You selected option 1." << endl;
            break;
        case 2:
            cout << "You selected option 2." << endl;
            break;
        case 3:
            cout << "You selected option 3." << endl;
            break;
        default:
            cout << "Invalid option." << endl;
    }

    return 0;
}

Output:

You selected option 2.

Explanation:

  1. The variable choice is evaluated by the switch statement.
  2. Case 2 matches the value of choice, and its associated block of code executes.
  3. The break statement prevents execution from continuing into other cases.

Example 2: Fall-Through Behavior Without break

This example demonstrates what happens when the break statement is omitted in a case.

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

int main() {
    int number = 1;

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

    return 0;
}

Output:

Case 1 executed.
Case 2 executed.
Case 3 executed.
Default executed.

Explanation:

  1. The variable number is evaluated by the switch statement.
  2. Case 1 matches, and its block executes.
  3. Since there is no break statement, the execution continues into subsequent cases (fall-through behavior).
  4. The program executes all remaining cases and the default block.

Example 3: Using case with Characters

This example shows how the case keyword can be used with character constants.

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

int main() {
    char grade = 'B';

    switch (grade) {
        case 'A':
            cout << "Excellent!" << endl;
            break;
        case 'B':
            cout << "Good!" << endl;
            break;
        case 'C':
            cout << "Fair." << endl;
            break;
        default:
            cout << "Invalid grade." << endl;
    }

    return 0;
}

Output:

Good!

Explanation:

  1. The variable grade is evaluated by the switch statement.
  2. Case 'B' matches the value of grade, and its associated block of code executes.
  3. The break statement prevents further execution of subsequent cases.

Key Points about case Keyword

  1. The case keyword defines a condition in a switch statement.
  2. Each case must be followed by a constant expression, such as an integer or character.
  3. The break statement is typically used to terminate a case block and prevent fall-through.
  4. If no case matches, the optional default block executes.
  5. The case keyword simplifies complex decision-making logic compared to multiple if-else statements.