Switch Statement in C++ (original) (raw)
The switch statement in C++ is a multi-way selection statement that allows a program to execute different blocks of code based on the value of an expression. It provides a cleaner and more organized alternative to long chains of if-else-if statements when multiple conditions depend on the same expression.
- The controlling expression must evaluate to an integral type, enumeration type, or a type that can be implicitly converted to an integral type.
- The switch statement improves code readability and simplifies handling multiple possible values of an expression. 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;}
`
Syntax
switch(expression)
{
case value1 :
// Statements
break; // break is optionalcase value2 :
// Statements
break; // break is optionaldefault :
// Statements executed if no case matches
}
**Where:
- expression is the value that determines which case will execute.
- case labels represent constant values that are compared against the expression.
- break terminates the switch statement and prevents fall-through.
- default executes when no case label matches the expression.
Working of switch Statement in C++
The working of a switch statement is as follows:
- The switch expression is evaluated.
- Its value is compared with each case label.
- If a matching case is found, execution begins from that case.
- The break statement terminates the switch block.
- If no case matches, the default block is executed (if present).
- Control moves to the statement following the switch block.
Flowchart of Switch Statement
The following flowchart illustrates how program control flows through a switch statement based on the value of the controlling expression.

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. However it is recommended to use it otherwise the following non-matching cases would also get executed until a break is found.
Using break in switch Statements
The break statement terminates the execution of a switch block. Without break, execution continues into subsequent cases until another break or the end of the switch statement is reached. This behavior is known as fall-through.
C++ `
#include using namespace std;
int main() {
int n = 1;
switch (n) {
case 1:
cout << "One ";
break;
case 2:
cout << "Two ";
break;
}
return 0; }
`
Examples
The below program demonstrates the uses of switch statement in C++ programs:
Print Day Name
The following program prints the day corresponding to a given day number.
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;}
`
Simple Calculator using switch
The following program performs basic arithmetic operations based on the operator entered by the user.
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
A nested switch statement is a switch statement placed inside another switch statement. The inner switch is executed only when control reaches its containing case in the outer switch.
C++ `
#include using namespace std;
int main() {
char branch = 'C';
int year = 2;
switch (year) {
case 1:
cout << "Elective Courses: English, Mathematics";
break;
case 2:
// Nested switch
switch (branch) {
case 'C':
cout << "Elective Courses: Machine Learning, Big Data";
break;
case 'E':
cout << "Elective Courses: Antenna Engineering";
break;
default:
cout << "Elective Courses: Optimization";
}
break;
default:
cout << "Invalid Year";
}
return 0;}
`
Output
Elective Courses: Machine Learning, Big Data
**Explanation: The outer switch first evaluates the value of year. Since year is 2, control enters case 2, where another switch statement is executed. The inner switch then evaluates the value of branch and executes the matching case.
Enum in switch Statement
C++ also allows enumeration constants to be used in a switch statement. Using enums improves code readability and makes programs easier to maintain.
C++ `
#include using namespace std;
enum Day { Sun, Mon, Tue, Wed, Thu, Fri, Sat };
int main() {
Day today = Wed;
switch (today) {
case Sun:
cout << "Sunday";
break;
case Mon:
cout << "Monday";
break;
case Tue:
cout << "Tuesday";
break;
case Wed:
cout << "Wednesday";
break;
case Thu:
cout << "Thursday";
break;
case Fri:
cout << "Friday";
break;
case Sat:
cout << "Saturday";
break;
}
return 0;}
`
Default Statement in switch
The default label specifies the statements that should execute when none of the case labels match the value of the switch expression. The default label can appear anywhere inside the switch block.
**Example 1: Default in the Middle
C++ `
#include using namespace std;
int main() {
int i = 2;
switch (i) {
default:
cout << "Default" << endl;
case 1:
cout << 1 << endl;
break;
case 2:
cout << 2 << endl;
case 3:
cout << 3 << endl;
}
return 0;}
`
**Example 2: Default at the Beginning
C++ `
#include using namespace std;
int main() {
int i = 5;
switch (i) {
default:
cout << "Default" << endl;
case 1:
cout << 1 << endl;
break;
case 2:
cout << 2 << endl;
case 3:
cout << 3 << endl;
}
return 0;}
`
**Note: The position of the default label does not affect when it is selected. It executes only when no matching case is found. However, because of fall-through behavior, statements after the default label may also execute if a break statement is not encountered.
Case Label Variations
The switch expression can be a variable or a valid expression. However, every case label must be a compile-time constant expression.
**Example 1: Using an Expression in switch
C++ `
#include using namespace std;
int main() {
int x = 2;
switch (x + 1) {
case 1:
cout << 1;
break;
case 2:
cout << 2;
break;
case 3:
cout << 3;
break;
default:
cout << "Default";
}
return 0;}
`
**Example 2: Case Label Cannot Be a Variable
C++ `
#include using namespace std;
int main() {
int x = 2;
int y = 1;
switch (x) {
case 1:
cout << 1;
break;
case 2:
cout << 2;
break;
case x + y: // Error
cout << 3;
break;
}
return 0;}
`
**Output
error: the value of 'x' is not usable in a constant expression
**Explanation: Case labels must be constant expressions known at compile time. Variables and variable expressions cannot be used as case labels.