Important Points About Switch Case Statements
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.
Example of switch cases without the break statement
// C Program to demonstrate the behaviour of switch case
// without break
#include <stdio.h>
int main()
{
int var = 2;
// switch case without break
switch (var) {
case 1:
printf("Case 1 is executed.\n");
case 2:
printf("Case 2 is executed.\n");
case 3:
printf("Case 3 is executed.\n");
case 4:
printf("Case 4 is executed.");
}
return 0;
}
Output
Case 2 is executed. Case 3 is executed. Case 4 is 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.
Example of Nested Switch Statement
// C program to illustrate the use of nested switch
#include <iostream>
using namespace std;
int main()
{
int var1 = 1;
int var2 = 0;
// outer switch
switch (var1) {
case 0:
cout << "Outer Switch Case 0\n";
break;
case 1:
cout << "Outer Switch Case 1\n";
// inner switch
switch (var2) {
case 0:
cout << "Inner Switch Case 0\n";
break;
}
break;
default:
cout << "Default Case of Outer Loop";
break;
}
return 0;
}
Output
Outer Switch Case 1 Inner Switch Case 0
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).
Example: In the below example we will identify the weekday through (1-7) numbers.
// C++ program to demonstrate the placement of default
// anywhere
#include <iostream>
using namespace std;
int main()
{
int day;
cout << "Enter a day number (1-7): ";
cin >> day;
switch (day) {
default: // Default placed first for demonstration
cout << "Not a valid weekday." << endl;
break;
case 1:
cout << "It's Monday!" << endl;
break;
case 2:
cout << "It's Tuesday!" << endl;
break;
case 3:
cout << "It's Wednesday!" << endl;
break;
case 4:
cout << "It's Thursday!" << endl;
break;
case 5:
cout << "It's Friday!" << endl;
break;
case 6:
cout << "It's Saturday!" << endl;
break;
case 7:
cout << "It's Sunday!" << endl;
break;
}
return 0;
}
Output
Enter a day number (1-7): 8
Not a valid weekday.
Switch Statement in C++
The C++ Switch case statement evaluates a given expression and based on the evaluated value(matching a certain condition), it executes the statements associated with it. It is an alternative to the long if-else-if ladder which provides an easy way to dispatch execution to different parts of code based on the value of the expression.