"C++ Switch Case Example"
-switch case is a control structure in C++ that allows the program to branch based on a given expression. It's often used to test for a condition by trying out different expressions. Here's a simple example:
Consider a program that prints the day of the week based on a given number. The numbers from 1 to 7 can represent the days: 1 for Sunday, 2 for Monday, and so on. A switch statement can neatly handle this:
Basic Example of Switch Case in C++
The following C++ code snippet demonstrates the use of a switch case:
int dayNumber = 5;
switch (dayNumber) {
case 1:
cout << "Sunday"
;
break;
case 2:
cout << "Monday"
;
break;
// ... and so on for the rest of the week
}
Day Representation in Switch Case
The switch statement in this example `switch (dayNumber)` tests the value of `dayNumber`. If `dayNumber` is 1, it executes the associated code `cout << "Sunday"`. If it's 2, it executes `cout << "Monday"`, and so on.
Each `case` label specifies a value the expression may have. If any of these values match, the associated block of code is executed. When a match is found, the statement `break;` is used to exit the switch statement, preventing the `default` case from being executed if present.
Using Switch Case with a String
In C++17 and later, switch cases can also use strings, which increases their versatility. Here's an example:
string dayName = "Monday";
switch (dayName) {
case "Monday":
cout << "Start of the week"
;
break;
case "Friday":
cout << "End of the week"
;
break;
default:
cout << "Some other day"<
;
}
The `default` case is optional and executes when no case matches the switch's expression. In this example, it catches all days except Monday and Friday.
Switch Case with Ranged Values
Beginning with C++20, switch statements have gained support for the `ranges` feature, enabling more compact and readable code. Here's an example:
int grade = 84;
switch (grade / 10) {
case 9: case 10:
cout << "Excellent"
;
break;
case 8:
cout << "Good"
;
break;
default:
cout << "Needs improvement"
;
break;
}
In this code, `case 9` and `case 10` are combined using the fallthrough behavior, so if `grade` is between 85 and 100, it will print "Excellent". This example showcases how `switch` can efficiently handle multi-range values.
Nested Switch Case
Switch statements can also be nested inside other switch statements. This can lead to complex but manageable code,especially when dealing with multiple, intertwined conditions. Here's a simple example:
string month = "February";
int daysInMonth = (month == "February") ? (leapYear ? 29 : 28) : // Some other month
switch (daysInMonth) {
case 30:
case 31:
cout << "This month has 30 or 31 days."
;
break;
default:
cout << "This month has less than 30 days."
;
}
The outer `switch` uses the number of days in a month to decide the output, while the inner `if`-`else` statement with another `switch` handles the special case of February, considering leap years.
In conclusion, switch case is a powerful control structure in C++ that can significantly simplify your code, making it easier to write and maintain. Experiment with different scenarios to fully grasp its capabilities. Happy coding!