C++ Switch case statement

This tutorial covers the switch case statement in C++.

Along with the if else statement, the switch case statement is a way of decision making in C++. It’s a more precise way of decision making than if statements which are better for covering a wide range of possibilities.

The C++ switch statement comprises of many code blocks with one “case” for each code block. An expression is evaluated with the resulting value is matched against each possible case in the switch statement. If a match is found, the code block for that case is executed.


C++ Switch case example

Below is a simple example of the switch case statement in C++. The code is pretty self-explanatory so much explanation is not required.

Unlike if statements, you can just use a simple variable here as the “expression”. There aren’t many restrictions on the type either, you can use strings, characters and numbers.

#include <iostream>
using namespace std;

int main () {
    int x, y;
    char op;

    cout << "Enter number x: ";
    cin >> x;
    cout << "Enter number y: ";
    cin >> y;

    cout << "Enter arithmetic operator (symbol): ";
    cin >> op;

    switch (op) {
        case '+':
            cout << "output: " << x + y;
            break;
        case '-':
            cout << "output: " << x - y;
            break;
        case '*':
            cout << "output: " << x * y;
            break;
        case '/':
            cout << "output: " << x / y;
            break;
    }
}

Output 1

Enter number x: 6
Enter number y: 8
Enter arithmetic operator (symbol): +
output: 14

Output 2

Enter number x: 8
Enter number y: 2
Enter arithmetic operator (symbol): /
output: 4

You could create an if statement equivalent to this, but it wouldn’t be as clean and simple. The switch statement was designed just for this kind of purpose where as the if statement is more general purpose.


Default keyword

It’s not possible to account for every scenario that may occur, that’s why we are given the use of the default statement.

The code contained within the code block of this statement will execute if none of the conditions in the switch statement have been met.

    switch (op) {
        case '+':
            cout << "output: " << x + y;
            break;
        case '-':
            cout << "output: " << x - y;
            break;
        case '*':
            cout << "output: " << x * y;
            break;
        case '/':
            cout << "output: " << x / y;
            break;
        default:
            cout << "Please pick from +, -, /, *";
    }
Enter number x: 6
Enter number y: 2
Enter arithmetic operator (symbol): =
Please pick from +, -, /, *

The default statement here is playing a very important role. It’s easy for someone to use an incorrect operator here. Without the default statement there would be no output and the user would be confused.


This marks the end of the C++ switch case statement article. Any suggestions or contributions for CodersLegacy are more than welcome. Questions regarding the article content can be asked in the comments section below.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments