C++ Switch Statement

Welcome to The Coding College! In this tutorial, we’ll explore the switch statement in C++. It’s a powerful tool for decision-making, especially when handling multiple discrete cases.

What is a Switch Statement?

The switch statement is used to evaluate an expression and execute code blocks based on matching cases. It’s an alternative to multiple if...else if statements, making code cleaner and more readable.

Syntax

switch (expression) {
    case constant1:
        // Code to execute if expression matches constant1
        break;
    case constant2:
        // Code to execute if expression matches constant2
        break;
    ...
    default:
        // Code to execute if no cases match
}
  • expression: The value being compared; must be of integer, character, or enumerated type.
  • case: Specifies a value to match against the expression.
  • break: Exits the switch block. Without it, execution will “fall through” to the next case.
  • default: An optional block executed if no cases match.

Example: Basic Switch Statement

#include <iostream>
using namespace std;

int main() {
    int day;

    cout << "Enter a number (1-7): ";
    cin >> day;

    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;
        case 6:
            cout << "Saturday" << endl;
            break;
        case 7:
            cout << "Sunday" << endl;
            break;
        default:
            cout << "Invalid input! Enter a number between 1 and 7." << endl;
    }

    return 0;
}

Output Example:

Enter a number (1-7): 5  
Friday

Example: Character Input with Switch

#include <iostream>
using namespace std;

int main() {
    char grade;

    cout << "Enter your grade (A-F): ";
    cin >> grade;

    switch (grade) {
        case 'A':
            cout << "Excellent!" << endl;
            break;
        case 'B':
            cout << "Good job!" << endl;
            break;
        case 'C':
            cout << "Well done." << endl;
            break;
        case 'D':
            cout << "You passed." << endl;
            break;
        case 'F':
            cout << "Better luck next time." << endl;
            break;
        default:
            cout << "Invalid grade entered." << endl;
    }

    return 0;
}

Output Example:

Enter your grade (A-F): B  
Good job!

Example: Fallthrough Behavior

If you omit the break statement, execution continues to the next case, even if it doesn’t match.

Example:

#include <iostream>
using namespace std;

int main() {
    int number;

    cout << "Enter a number (1-3): ";
    cin >> number;

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

    return 0;
}

Output for Input 1:

You selected 1.  
You selected 2.  
You selected 3.

To avoid this, always include break where appropriate.

Example: Using Default

The default case is executed if none of the specified cases match.

#include <iostream>
using namespace std;

int main() {
    int option;

    cout << "Enter a menu option (1-3): ";
    cin >> option;

    switch (option) {
        case 1:
            cout << "Option 1: Start Game" << endl;
            break;
        case 2:
            cout << "Option 2: Load Game" << endl;
            break;
        case 3:
            cout << "Option 3: Quit" << endl;
            break;
        default:
            cout << "Invalid option. Please choose between 1 and 3." << endl;
    }

    return 0;
}

Output Example:

Enter a menu option (1-3): 4  
Invalid option. Please choose between 1 and 3.

Key Points to Remember

  1. Expression Type: The switch expression must evaluate to an integer, character, or enumerated value.
  2. Break Statement: Without break, execution falls through to subsequent cases.
  3. Default Case: It’s optional but recommended to handle unexpected inputs.
  4. No Overlapping Cases: Each case must have a unique constant value.

When to Use Switch Statements

Use a switch statement when:

  • You need to evaluate a single expression against multiple discrete values.
  • The conditions are mutually exclusive.
  • You want cleaner and more readable code compared to multiple if...else if statements.

Summary

The switch statement is a cleaner alternative to multiple if...else conditions for handling discrete cases. It simplifies your code and enhances readability.

Explore More at The Coding College

Discover more C++ tutorials and practical examples at The Coding College. Master programming concepts with user-focused, high-value content tailored to your learning needs.

What’s Next?

  • Dive into loops (for, while, and do...while) for repetitive tasks.
  • Learn about functions for modular and reusable code.
  • Explore arrays and pointers to handle data efficiently.

Leave a Comment