Switch Statement in C++ (original) (raw)
Last Updated : 16 May, 2025
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:
- **expression****:** Variable or any expression that evaluates to a constant value.
- **case and values: Each value such as **value_1, **value_2, etc. is a possible value that the expression might result. If the expression matches a case value, the code inside that case block will be executed.
- **break****:** After a case block executes, the **break statement is used to exit the switch statement. Without the break statement, the program would continue executing the next case blocks even if they match or not.
- **default: The default case is optional and is executed only if no other case matches. Position of the default case doesn't affect the behaviour of the switch statement.
**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
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:
- **Evaluate the switch expression: The expression provided in the switch statement is evaluated.
- **Match with case values: The evaluated value is compared with the values specified in each case block.
- **Execute the matching case block:
- If a matching case value is found, the corresponding block of code is executed.
- If no matching case is found, the default case, if provided, is executed.
- **Use of the break keyword:
- If the break keyword is used in a case block, the program exits the switch statement, and the flow continues after the switch.
- If there is no break, the program will continue executing subsequent case blocks.
- **Proceed after the switch: After the switch statement completes, the statements following it are executed.
Rules of the switch Statement
When using the switch statement in C++, there are a few rules to keep in mind:
- The case values must be of int or char type.
- You can have as many case blocks as needed.
- Duplicate case values are not allowed.
- A break statement is optional in each case.
Examples
The below program demonstrates the uses of switch statement in C++ programs:
Print Day Name
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
- 10 + 2 = 12
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. |