Language: EN

cpp-condicional-switch

What is and how to use the SWITCH conditional in C++

The switch conditional in C++ is a control structure that allows executing different blocks of code based on the value of an expression.

Sometimes it is a cleaner and more readable alternative to a series of nested if-else statements. However, many people (myself included) believe that instead of improving readability, it often worsens it.

Basic Syntax

The basic syntax of a switch conditional in C++ is:

switch (expression) {
    case value1:
        // Code to execute if expression equals value1
        break;
    case value2:
        // Code to execute if expression equals value2
        break;
    // You can add more cases here
    default:
        // Code to execute if expression does not match any case
        break;
}
  • expression: This is the expression whose value is evaluated and compared with the case values.
  • case: Each case represents a specific value that is compared with the expression.
  • default: This is an optional block that executes if no case matches the value of the expression.

Let’s look at a basic example of using switch:

#include <iostream>

int main() {
    int number = 2;

    switch (number) {
        case 1:
            std::cout << "One" << std::endl;
            break;
        case 2:
            std::cout << "Two" << std::endl;
            break;
        case 3:
            std::cout << "Three" << std::endl;
            break;
        default:
            std::cout << "Invalid number" << std::endl;
            break;
    }

    return 0;
}

In this example,

  • Depending on the value of number, the program will print the corresponding number to the console.
  • If the value does not match any of the defined cases, it will print Invalid number.

Fall-Through Between Cases

In C++, it is not mandatory to include a break at the end of each case (unlike other languages like C#).

If the break is omitted, the program flow will continue executing the code of the subsequent case (this is known as “fall-through”).

#include <iostream>

int main() {
    int number = 2;

    switch (number) {
        case 1:
        case 2:
        case 3:
            std::cout << "Number between 1 and 3" << std::endl;
            break;
        default:
            std::cout << "Invalid number" << std::endl;
            break;
    }

    return 0;
}

In this example,

  • Both cases 1, 2, and 3 will execute the same block of code and then end with the break.

Although this can be useful in some cases, it can also lead to errors if not handled carefully

In general, don’t do it :::

Practical Examples

Determine the name of a number

In this example, we use switch to determine the name of a number based on its value.

#include <iostream>

int main() {
    int number = 2;

    switch (number) {
        case 1:
            std::cout << "One" << std::endl;
            break;
        case 2:
            std::cout << "Two" << std::endl;
            break;
        case 3:
            std::cout << "Three" << std::endl;
            break;
        default:
            std::cout << "Invalid number" << std::endl;
            break;
    }

    return 0;
}

Determine the mathematical operation based on the operator

This code shows how to use switch to select the mathematical operation based on the provided operator.

#include <iostream>

int main() {
    char operator = '+';

    switch (operator) {
        case '+':
            std::cout << "Addition" << std::endl;
            break;
        case '-':
            std::cout << "Subtraction" << std::endl;
            break;
        case '*':
            std::cout << "Multiplication" << std::endl;
            break;
        case '/':
            std::cout << "Division" << std::endl;
            break;
        default:
            std::cout << "Invalid operator" << std::endl;
            break;
    }

    return 0;
}

Determine the name of a day of the week

Here, we use a switch to determine the name of the day of the week based on its number.

#include <iostream>

int main() {
    int day = 3;

    switch (day) {
        case 1:
            std::cout << "Monday" << std::endl;
            break;
        case 2:
            std::cout << "Tuesday" << std::endl;
            break;
        case 3:
            std::cout << "Wednesday" << std::endl;
            break;
        // Add more cases for other days
        default:
            std::cout << "Invalid day" << std::endl;
            break;
    }

    return 0;
}

Determine the name of a month (with enumeration)

In this example, we use a switch to determine the name of a month based on its value in an enumeration.

#include <iostream>

enum Months { January = 1, February, March, April, May, June, July, August, September, October, November, December };

int main() {
    Months month = May;

    switch (month) {
        case January:
            std::cout << "January" << std::endl;
            break;
        case February:
            std::cout << "February" << std::endl;
            break;
        // Add more cases for other months
        default:
            std::cout << "Invalid month" << std::endl;
            break;
    }

    return 0;
}

These examples are intended to show how to use the Switch conditional. It does not mean that it is the best way to solve the problem they address. Usually, there are better alternatives.