The switch statement is a control statement that allows you to select one of many code blocks to be executed. It is a better alternative than using multiple if-else statements when you have several possible values for a variable.
1️⃣ Syntax of switch Statement
switch (expression) {
case value1:
// Code to execute if expression == value1
break;
case value2:
// Code to execute if expression == value2
break;
case value3:
// Code to execute if expression == value3
break;
default:
// Code to execute if no case matches
}
Key Points:
- The
expressionis usually a variable or constant. - The
casevalues are the possible values thatexpressioncan match. - The
breakstatement is used to exit theswitchstatement after a match is found. If you omitbreak, the program will "fall through" and execute the code for the next case. - The
defaultcase is optional and executes if no case matches the expression.
2️⃣ Example: Simple switch Statement
#include <iostream>
using namespace std;
int main() {
int day;
cout << "Enter a number (1-7) for the day of the week: ";
cin >> 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 day number!" << endl;
}
return 0;
}
Output:
Enter a number (1-7) for the day of the week: 3
Wednesday
📌 If you input a number outside 1-7, it will print "Invalid day number!" due to the default case.
3️⃣ Example: Using switch to Choose a Menu Option
#include <iostream>
using namespace std;
int main() {
int choice;
cout << "Enter your choice (1-3):\n";
cout << "1. Add\n";
cout << "2. Subtract\n";
cout << "3. Multiply\n";
cout << "Choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "You chose Add." << endl;
break;
case 2:
cout << "You chose Subtract." << endl;
break;
case 3:
cout << "You chose Multiply." << endl;
break;
default:
cout << "Invalid choice!" << endl;
}
return 0;
}
Output 1 (if input is 2):
You chose Subtract.
Output 2 (if input is 5):
Invalid choice!
4️⃣ Example: Switch with Multiple Cases for the Same Block
In some situations, you may want multiple case values to execute the same code. You can simply list them one after another, without break statements between them.
#include <iostream>
using namespace std;
int main() {
int grade;
cout << "Enter your grade (A-D): ";
cin >> grade;
switch (grade) {
case 'A':
case 'B':
cout << "You passed!" << endl;
break;
case 'C':
case 'D':
cout << "You need to improve." << endl;
break;
default:
cout << "Invalid grade!" << endl;
}
return 0;
}
Output 1 (if input is A):
You passed!
Output 2 (if input is C):
You need to improve.
5️⃣ Example: Switch with Expressions (Not Just Constants)
You can use a switch statement with expressions, but the expression must be constant or evaluated at compile-time.
#include <iostream>
using namespace std;
int main() {
int a = 3;
int b = 4;
switch (a + b) {
case 7:
cout << "The sum is 7!" << endl;
break;
case 10:
cout << "The sum is 10!" << endl;
break;
default:
cout << "The sum is neither 7 nor 10." << endl;
}
return 0;
}
Output:
The sum is 7!
6️⃣ Important Points to Remember About switch
switchworks best when you have multiple conditions that depend on the same variable.- No need for
else, asswitchhandles multiple conditions on its own. breakstatement: Withoutbreak, execution continues to the next case (fall-through).defaultcase: It's optional, but good practice to handle unexpected values.
7️⃣ switch vs if-else
switchis better when comparing one variable against multiple constant values (e.g., days of the week, menu options).if-elseis better when checking for more complex conditions (e.g., ranges, multiple variables).
Would you like an example where switch is used with more complex conditions or user input validation?
0 Comments