Switch Statement in C++



In this tutorial, we will learn about switch statements in C++. We will cover the basics of conditional execution using switch statements.


What is a Switch statement

A switch statement is a conditional statement that allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each case.


Syntax

The syntax for the switch statement in C++ is:

switch (variable) {
    case value1:
        // Code block to execute if variable == value1
        break;
    case value2:
        // Code block to execute if variable == value2
        break;
    // You can have any number of case statements.
    default:
        // Code block to execute if variable doesn't match any case
}

The switch statement evaluates the specified variable. The code block corresponding to the matching case value is executed. The break statement is used to exit the switch block. If no case matches, the default block is executed.

Flowchart of Switch Statement


Example 1: Checking the Day of the Week

  1. Declare an integer variable day.
  2. Assign a value to day.
  3. Use a switch statement to check the value of day.
  4. Print a message indicating the corresponding day of the week.

C++ Program

#include <iostream>
using namespace std;
int main() {
    int day = 3;
    switch (day) {
        case 1:
            cout << "Monday";
            break;
        case 2:
            cout << "Tuesday";
            break;
        case 3:
            cout << "Wednesday";
            break;
        case 4:
            cout << "Thursday";
            break;
        case 5:
            cout << "Friday";
            break;
        case 6:
            cout << "Saturday";
            break;
        case 7:
            cout << "Sunday";
            break;
        default:
            cout << "Invalid day";
    }
    return 0;
}

Output

Wednesday


Example 2: Checking the Grade of a Student

  1. Declare a character variable grade.
  2. Assign a value to grade.
  3. Use a switch statement to check the value of grade.
  4. Print a message indicating the corresponding grade description.

C++ Program

#include <iostream>
using namespace std;
int main() {
    char grade = 'B';
    switch (grade) {
        case 'A':
            cout << "Excellent";
            break;
        case 'B':
            cout << "Good";
            break;
        case 'C':
            cout << "Average";
            break;
        case 'D':
            cout << "Below Average";
            break;
        case 'F':
            cout << "Fail";
            break;
        default:
            cout << "Invalid grade";
    }
    return 0;
}

Output

Good


Example 3: Checking the Month of the Year

  1. Declare an integer variable month.
  2. Assign a value to month.
  3. Use a switch statement to check the value of month.
  4. Print a message indicating the corresponding month of the year.

C++ Program

#include <iostream>
using namespace std;
int main() {
    int month = 5;
    switch (month) {
        case 1:
            cout << "January";
            break;
        case 2:
            cout << "February";
            break;
        case 3:
            cout << "March";
            break;
        case 4:
            cout << "April";
            break;
        case 5:
            cout << "May";
            break;
        case 6:
            cout << "June";
            break;
        case 7:
            cout << "July";
            break;
        case 8:
            cout << "August";
            break;
        case 9:
            cout << "September";
            break;
        case 10:
            cout << "October";
            break;
        case 11:
            cout << "November";
            break;
        case 12:
            cout << "December";
            break;
        default:
            cout << "Invalid month";
    }
    return 0;
}

Output

May