Switch Statement in C++
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.
Let’s take a look at an example:
#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;
}
Output
A
Explanation: In the above program, the switch statement evaluates the value of x and executes the matching case. If x is ‘A’, it prints “A”; if ‘B’, it prints “B”. The default case handles other values.
Syntax of switch Statement in C++
switch (expression) {
case value_1:
// statements_break.
break;
case value_2:
// statements_2;
break;
…..
…..
default:
// default_statements;
break;
}
Here,
- The default case is executed if none of the cases are matched.
- The break keyword in each case is used to break out of the switch statement after the matching case is found. Otherwise, all the statements after the matching case will be executed.
Rules of the switch Statement in C++
There are some rules that we need to follow when using switch statements in C++. They are as follows:
- The case value must be either int or char type.
- There can be any number of cases.
- No duplicate case values are allowed.
- Each statement of the case can have a break statement. It is optional.
- The default Statement is also optional.
Flowchart of Switch Statement in C++

Flowchart of the switch statement in C++
Working of switch Statement in C++
The working of the switch statement in C is as follows:
- Step 1: The switch expression is evaluated.
- Step 2: The evaluated value is then matched against the present case values.
- Step 3A: If the matching case value is found, that case block is executed.
- Step 3B: If the matching code is not found, then the default case block is executed if present.
- Step 4A: If the break keyword is present in the block, then program control comes out of the switch statement.
- Step 4B: If the break keyword is not present, then all the cases after the matching case are executed.
- Step 5: Statements after the switch statement is executed.
Examples of Switch Statement
The below program demonstrates the uses of switch statement in C++ programs:
Using switch Cases Without break
#include <iostream>
using namespace std;
int main() {
// Switch variable
int var = 2;
// Switch statement
switch (var) {
case 1:
cout << "Case 1 is Matched." << endl;
case 2:
cout << "Case 2 is Matched." << endl;
case 3:
cout << "Case 3 is Matched." << endl;
default:
cout << "Default case is Matched." << endl;;
}
return 0;
}
Output
Case 2 is Matched. Case 3 is Matched. Default case is Matched.
As we can see, if break is not used, every case after the matched case is executed.
Print the Day Name Using Day Number
#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;
}
Output
Thursday
Simple Calculator using the switch
#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 2
Enter the Operator (+,-,*,/)
Enter any other to exit
+
10 + 2 = 12
Important Points About switch Statement
1. Switch expression should result in a constant value
If the expression provided in the switch statement does not result in a constant value, it would not be valid. Some valid expressions for switch case will be,
// Constant expressions allowed
switch(1 + 2 + 23);
switch(1 * 2 + 3 % 4);
// Variable expression are allowed provided
// they are assigned with fixed values
switch(a * b + c * d);
switch(a + b + c);
2. Expression must evaluate only int or char type values.
The switch statement can only evaluate the integer or character value. So, the switch expression should return the values of type int or char only.
3. Break in switch case
The break keyword is used in the switch case to break out of the switch when encountered. It is used at the end of every case block so that when the matching case is executed, the program control comes out of the loop.
The break statement is optional. If omitted, all the cases after the matching case will also be executed.
4. Default in switch case
The default keyword is used to define a default case which will be executed when no case value is matched. It is also an optional statement, and the switch case statement runs without problem if it is omitted.
5. No duplicate Case Values
In the C switch statement, duplicate case values are not allowed. All the case values must be unique.
6. Nested Switch Statements
In C++, we can nest one switch inside another switch statement without any problem. Though it is avoided most of the time as it makes the program more complex and unreadable.
7. Position of Default Case Does Not Matters
Regardless of its placement, the default case only gets executed if none of the other case conditions are met. So, putting it at the beginning, middle, or end doesn’t change the core logic (unless you’re using a less common technique called fall-through).
8. Differences between switch and if else if ladder in C?
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. |


