C++ switch statement
The switch
statement in C++ is used to simplify the code when you have multiple possible values for a single expression. It evaluates an expression and matches it against multiple case
values, executing the code associated with the matching case. This structure is often used as an alternative to a series of if-else if
statements when checking for equality.
Syntax of switch
Statement
switch (expression) {
case value1:
// Code to execute if expression == value1
break;
case value2:
// Code to execute if expression == value2
break;
// More cases...
default:
// Code to execute if no case matches
}
expression
: This is evaluated once and compared with eachcase
value.case value:
: The value to match against the expression.break
: Used to exit theswitch
block. Withoutbreak
, the code will continue executing the subsequent cases, known as "fall-through."default:
: The default block executes if none of thecase
values match. It is similar to theelse
statement in anif-else
structure.
Example: Days of the Week
Here is an example using switch
to determine the day of the week:
#include <iostream>
int main() {
int day;
std::cout << "Enter a day number (1-7): ";
std::cin >> day;
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 number" << std::endl;
}
return 0;
}
Explanation
switch (day)
: The value ofday
is evaluated, and theswitch
block attempts to match it with one of thecase
values.- Each
case
represents a possible value forday
. For instance, if the user enters3
, the program outputs "Wednesday." - The
break
statement is used to terminate acase
block. Withoutbreak
, the program will "fall through" and execute the code in the following cases, which is often undesirable. default
is used to handle cases where none of thecase
values match the givenday
. For instance, if the user enters a value outside of1-7
, the output will be "Invalid day number."
Example with Fall-Through
If you omit the break
statement, the code continues to execute the following cases even if a match was found. This is called "fall-through" and can sometimes be useful.
#include <iostream>
int main() {
int num = 2;
switch (num) {
case 1:
std::cout << "Number is One" << std::endl;
case 2:
std::cout << "Number is Two" << std::endl;
case 3:
std::cout << "Number is Three" << std::endl;
default:
std::cout << "End of switch" << std::endl;
}
return 0;
}
Output:
Number is Two
Number is Three
End of switch
In this example, since there are no break
statements, the code falls through from case 2
to case 3
and then to default
.
Key Points
- Single Expression: The
switch
statement works best for a single expression that can be compared against multiple constant values. break
Statement: Always usebreak
unless you intentionally want to fall through to the next case.default
: Thedefault
block is optional, but it is recommended to handle unexpected values.
Use Cases
- Simplifies code when you have multiple possible discrete values for a single variable.
- Often used for menu selection, input handling, and managing different modes or states in a program.
The switch
statement provides an easy-to-read alternative to complex if-else if
chains when dealing with many specific values for a single variable.