Last Updated : 23 Jul, 2025
Try it on GfG Practice
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 a simpler alternative to the long if-else-if ladder.
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 <iostream>
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 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:
When using the switch statement in C++, there are a few rules to keep in mind:
The below program demonstrates the uses of switch statement in C++ programs:
1. Print Day Name C++
#include <iostream>
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.
2. Simple Calculator using switch C++
#include <iostream>
#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 2Nested Switch Statements
Enter the Operator (+,-,*,/)
Enter any other to exit
+
10 + 2 = 12
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 ifFollowing are the main differences between switch and if else if ladder in C++:
switch
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.RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4