The switch
statement in C++ provides a way to execute different parts of code based on the value of a variable or expression. It is an alternative to using multiple if-else if-else
statements and can make the code more readable and easier to maintain when dealing with multiple conditions.
Basic Syntax of switch
The syntax of a switch
statement is as follows:
switch (expression) {
case constant1:
// Code to execute if expression == constant1
break;
case constant2:
// Code to execute if expression == constant2
break;
// Add more cases as needed
default:
// Code to execute if expression doesn't match any case
}
expression
: This is evaluated once and compared with each case.case constant
: Ifexpression
matchesconstant
, the corresponding block of code is executed.break
: This statement terminates theswitch
block. Withoutbreak
, execution would continue to the next case.default
: This block is executed if no matching case is found. It is optional but useful as a fallback.
Example: Basic switch
Statement
Let’s see a simple example where a switch
statement is used to print the name of a day based on a number.
#include <iostream>
int main() {
int day = 3;
switch (day) {
case 1:
std::cout << "Monday" << std::endl;
break;
case 2:
std::cout << "Tuesday" << std::endl;
break;
case 3:
std::cout << "Wednesday" << std::endl;
break;
case 4:
std::cout << "Thursday" << std::endl;
break;
case 5:
std::cout << "Friday" << std::endl;
break;
case 6:
std::cout << "Saturday" << std::endl;
break;
case 7:
std::cout << "Sunday" << std::endl;
break;
default:
std::cout << "Invalid day" << std::endl;
break;
}
return 0;
}
In this example:
- The
switch
statement checks the value ofday
. - Depending on the value, it prints the corresponding day of the week.
- The
default
case handles any invalid values.
switch
with enum
Using switch
with enum
can make the code more readable, especially when dealing with a set of related constants.
Example: switch
with enum
#include <iostream>
enum class Color { RED, GREEN, BLUE };
int main() {
Color color = Color::GREEN;
switch (color) {
case Color::RED:
std::cout << "Red" << std::endl;
break;
case Color::GREEN:
std::cout << "Green" << std::endl;
break;
case Color::BLUE:
std::cout << "Blue" << std::endl;
break;
default:
std::cout << "Unknown color" << std::endl;
break;
}
return 0;
}
In this example:
- The
enum class Color
defines three colors:RED
,GREEN
, andBLUE
. - The
switch
statement handles each color case and prints the corresponding color name.
Fall-through in switch
If you omit the break
statement, the execution will “fall through” to the next case. This can be useful in some situations but should be used with caution.
Example: Fall-through
#include <iostream>
int main() {
int num = 2;
switch (num) {
case 1:
std::cout << "Number is 1" << std::endl;
case 2:
std::cout << "Number is 2" << std::endl;
case 3:
std::cout << "Number is 3" << std::endl;
break;
default:
std::cout << "Unknown number" << std::endl;
break;
}
return 0;
}
In this example:
- Since there’s no
break
aftercase 1
, the execution falls through tocase 2
andcase 3
, printing “Number is 2” and “Number is 3”.
Nested switch
Statements
You can also nest switch
statements, although this can make the code harder to read and should be avoided if possible.
Example: Nested switch
#include <iostream>
int main() {
int category = 1;
int item = 2;
switch (category) {
case 1:
std::cout << "Category 1" << std::endl;
switch (item) {
case 1:
std::cout << "Item 1.1" << std::endl;
break;
case 2:
std::cout << "Item 1.2" << std::endl;
break;
default:
std::cout << "Unknown item in Category 1" << std::endl;
break;
}
break;
case 2:
std::cout << "Category 2" << std::endl;
switch (item) {
case 1:
std::cout << "Item 2.1" << std::endl;
break;
case 2:
std::cout << "Item 2.2" << std::endl;
break;
default:
std::cout << "Unknown item in Category 2" << std::endl;
break;
}
break;
default:
std::cout << "Unknown category" << std::endl;
break;
}
return 0;
}
In this example:
- The outer
switch
handlescategory
. - The inner
switch
handlesitem
within each category.
Conclusion
The switch
statement in C++ is a powerful control structure that allows for cleaner and more efficient handling of multiple conditions. It can simplify code that would otherwise require multiple if-else if-else
statements. However, use it wisely, especially with fall-through behavior and nested switch
statements, to maintain code readability and maintainability. Happy coding!