C++ Switch Statement

C++ switch statement is a control flow statement that allows a programmer to choose among multiple options in a program. It is useful when you want to test a single variable against multiple values, and perform different actions based on the result. The switch statement is often used as an alternative to the if-else statement when there are more than two options to choose from. In this article, we will explore the syntax, usage, and some examples of the C++ switch statement.

Syntax

The basic syntax of a C++ switch statement is as follows:

switch(expression) {
    case value1:
        // code to be executed if expression == value1;
        break;
    case value2:
        // code to be executed if expression == value2;
        break;
    ...
    default:
        // code to be executed if expression does not match any of the case values;
}

The expression is evaluated, and the corresponding code block for the matching case value is executed. If no case value matches the expression, the code in the default block is executed. It is important to note that the break statement is used to exit the switch statement after a case has been executed. If the break statement is not used, the code will continue to execute into the next case, regardless of whether or not the expression matches.

Usage

The switch statement is used to simplify a program when there are multiple conditions to check for. It allows for cleaner and more readable code. The switch statement is especially useful when working with enumerated data types, where the possible values are known in advance.

Examples

Here are some examples of how to use the C++ switch statement:

#include <iostream>

int main() {
    int day = 4;

    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;
    case 4:
        std::cout << "Thursday" << std::endl;
        break;
    case 5:
        std::cout << "Friday" << std::endl;
        break;
    case 6:
        std::cout << "Saturday" << std::endl;
        break;
    case 7:
        std::cout << "Sunday" << std::endl;
        break;
    default:
        std::cout << "Invalid day" << std::endl;
    }

    return 0;
}

In this example, the variable “day” is tested against the values of 1 to 7, corresponding to the days of the week. If the value of “day” matches a case value, the corresponding day of the week is printed. If the value of “day” is not between 1 and 7, the default case is executed, and “Invalid day” is printed.

Another example:

#include <iostream>

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

    switch (grade) {
    case 'A':
        std::cout << "Excellent" << std::endl;
        break;
    case 'B':
        std::cout << "Good" << std::endl;
        break;
    case 'C':
        std::cout << "Average" << std::endl;
        break;
    case 'D':
        std::cout << "Below average" << std::endl;
        break;
    case 'F':
        std::cout << "Fail" << std::endl;
        break;
    default:
        std::cout << "Invalid grade" << std::endl;
    }

    return 0;
}

In this example, the variable “grade” is tested against the values of ‘A’, ‘B’, ‘C’, ‘D’ and ‘F’, corresponding to different grades. If the value of “grade” matches a case value, the corresponding grade description is printed. If the value of “grade” does not match any of the case values, the default case is executed, and “Invalid grade” is printed.

Conclusion

The C++ switch statement is a powerful control flow statement that allows a programmer to choose among multiple options in a program. It is useful when you want to test a single variable against multiple values and perform different actions based on the result. With the help of examples provided in this article, you can see how to use the switch statement effectively in your own programs. It’s a great alternative to the if-else statement and makes the code more readable and maintainable.

Leave a Reply

Your email address will not be published. Required fields are marked *