Switch Statement in C++ (original) (raw)

Last Updated : 16 May, 2025

Try it on GfG Practice redirect icon

In C++, the **switch statement is a flow control statement that is used to execute the different blocks of statements based on the value of the given expression. It is an alternative to the long if-else-if ladder which provides an easy way to execute different parts of code based on the value of the expression.

Syntax

C++ `

switch (expression) { case value_1: // code to be executed. break; case value_2: // code to be executed; break; ..... ..... default: // code to be executed; }

`

In the above syntax:

**Example:

C++ `

#include using namespace std;

int main() {

// Variable to the used as switch expression
char x = 'A';

// Switch statement with three cases
switch (x) {
case 'A':
    cout << "A";
    break;
case 'B':
    cout << "B";
    break;
default:
    cout << "Other than A and B";
    break;
}
return 0;

}

`

Flowchart of Switch Statement

switch case in cpp

Flowchart of the switch statement in C++

Working of switch Statement

The working of the switch statement in C++ is quite similar to C. Here's how it works:

Rules of the switch Statement

When using the switch statement in C++, there are a few rules to keep in mind:

Examples

The below program demonstrates the uses of switch statement in C++ programs:

C++ `

#include using namespace std;

int main() { int day = 4;

// Determine the day name 
// using switch
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 number";
        break;
}
return 0;

}

`

In the above example, we printed the day name using a switch statement. Similarly, we can write a program to create a **simple calculator using a switch statement to perform basic arithmetic operations.

Simple Calculator using switch

C++ `

#include #include <stdlib.h> using namespace std;

int main() {

// Switch variable for 
// operation choice
char c;

// Operands
int x, y;

cout << "Enter the two numbers: ";
cin >> x >> y;

cout << "Enter the Operator (+,-,*,/)\nEnter any other to "
        "exit\n";
cin >> c;

// switch case with operation for each operator
switch (c) {
case '+':
    cout << x << " + " << y << " = " << x + y << endl;
    break;

case '-':
    cout << x << " - " << y << " = " << x - y << endl;
    break;

case '*':
    cout << x << " * " << y << " = " << x * y << endl;
    break;
case '/':
    cout << x << " / " << y << " = " << x / y << endl;
    break;
default:
    printf("Exiting\n");
}

return 0;

}

`

**Output

Enter the two numbers: 10 2
Enter the Operator (+,-,*,/)
Enter any other to exit

Nested Switch Statements

C++ allows **nested switch statements, meaning we can place one switch inside another. However, this is generally avoided as it can make the program more complex and harder to read.

Switch vs if else if

Following are the main differences between switch and if else if ladder in C++:

switch **if else if
It executes the different cases on the basis of the value of the switch variable. It executes the different blocks based on the condition specified.
It can only evaluate the int or char type expressions. It can evaluate any type of expression.
Faster and easier to read for a large number of conditions. It can get messy when there are lots of conditions.