Introduction to C++ | switch Statement
C++ switch Statement
The switch statement in C++ allows you to test a variable against multiple possible values and execute different blocks of code based on the value. It is useful when you have a large number of conditions to check and want a more concise and efficient way to handle them compared to using multiple if-else
statements.
What is a switch Statement?
The switch statement works by evaluating an expression and comparing it with multiple case labels. If a match is found, the corresponding block of code is executed. You can also include a default
case to execute code if no match is found.
Syntax:
Syntax of a switch Statement
switch (expression) {
case value1:
// code to be executed if expression == value1
break;
case value2:
// code to be executed if expression == value2
break;
default:
// code to be executed if expression doesn't match any case
}
Example of switch Statement
Here’s an example where the switch
statement is used to check the day of the week and print a corresponding message:
Code Example
#include <iostream>
using namespace std;
int main() {
int day;
cout << "Enter a number (1-7) for the day of the week: ";
cin >> day;
// switch statement to print the name of the day
switch (day) {
case 1:
cout << "Monday" << endl;
break;
case 2:
cout << "Tuesday" << endl;
break;
case 3:
cout << "Wednesday" << endl;
break;
case 4:
cout << "Thursday" << endl;
break;
case 5:
cout << "Friday" << endl;
break;
case 6:
cout << "Saturday" << endl;
break;
case 7:
cout << "Sunday" << endl;
break;
default:
cout << "Invalid input. Please enter a number between 1 and 7." << endl;
}
return 0;
}
Output
Wednesday
Default Case in switch
The default
case is optional but useful for handling invalid input or when the expression does not match any of the cases. If no default
case is provided, the program will do nothing when no match is found.
Example of using the default
case:
Code Example with Default Case
#include <iostream>
using namespace std;
int main() {
int number;
cout << "Enter a number between 1 and 5: ";
cin >> number;
// switch statement with default case
switch (number) {
case 1:
cout << "You entered One." << endl;
break;
case 2:
cout << "You entered Two." << endl;
break;
case 3:
cout << "You entered Three." << endl;
break;
case 4:
cout << "You entered Four." << endl;
break;
case 5:
cout << "You entered Five." << endl;
break;
default:
cout << "Invalid input! Please enter a number between 1 and 5." << endl;
}
return 0;
}
Output
Invalid input! Please enter a number between 1 and 5.
Multiple Case Labels
You can combine multiple case labels in a switch statement, so that several values can result in the same block of code. This can be helpful when different values should trigger the same behavior.
Example of combining case labels:
Code Example with Multiple Case Labels
#include <iostream>
using namespace std;
int main() {
int grade;
cout << "Enter your grade (0-100): ";
cin >> grade;
// switch statement with multiple case labels
switch (grade) {
case 90:
case 91:
case 92:
case 93:
case 94:
case 95:
case 96:
case 97:
case 98:
case 99:
case 100:
cout << "You got an A!" << endl;
break;
case 80:
case 81:
case 82:
case 83:
case 84:
case 85:
case 86:
case 87:
case 88:
case 89:
cout << "You got a B!" << endl;
break;
default:
cout << "You need to study more." << endl;
}
return 0;
}
Output
You got an A!
Pro Tip:
💡 Pro Tip
Switch statements are an excellent choice when you have multiple possible values to check, but they can only be used with values that are integral types (like integers or characters). When the number of possible conditions is small, if-else
statements might be easier to read. Also, always remember to use break
after each case unless you intentionally want to "fall through" to the next case.