The switch conditional in C++ is a control structure that allows executing different blocks of code based on the value of an expression.
If you want to learn more, check out the Introduction to Programming Course
Sometimes it is a cleaner and more readable alternative to a series of nested if-else statements. Although, many people think (myself included) that instead of improving readability, it often worsens it.
Basic Syntax
The basic syntax of a switch conditional in C++ is:
switch (expression) {
case value1:
// Code to execute if expression is equal to value1
break;
case value2:
// Code to execute if expression is equal to value2
break;
// You can add more cases here
default:
// Code to execute if expression does not match any case
break;
}
- expression: It’s the expression whose value is evaluated and compared with the case values.
- case: Each
caserepresents a specific value that is compared with the expression. - default: It’s an optional block that executes if no
casematches the expression’s value.
Let’s see a basic example of using switch:
#include <iostream>
int main() {
int number = 2;
switch (number) {
case 1:
std::cout << "One" << std::endl;
break;
case 2:
std::cout << "Two" << std::endl;
break;
case 3:
std::cout << "Three" << std::endl;
break;
default:
std::cout << "Invalid number" << std::endl;
break;
}
return 0;
}
In this example,
- Depending on the value of
numero, the program will print the corresponding number to the console. - If the value doesn’t match any of the defined cases, it will print Número no válido.
Fall-Through Between Cases
In C++ it is not mandatory to include a break at the end of each case (unlike other languages like C#).
If the break is omitted, the program flow will continue executing the code of the following case (this is known as “fall-through”).
#include <iostream>
int main() {
int number = 2;
switch (number) {
case 1:
case 2:
case 3:
std::cout << "Number between 1 and 3" << std::endl;
break;
default:
std::cout << "Invalid number" << std::endl;
break;
}
return 0;
}
In this example,
- Both cases
1,2, and3will execute the same block of code and then end with thebreak.
Although this can be useful in some cases, it can also lead to errors if not handled carefully \
In general, don’t do it
Practical Examples
Determine the name of a number
In this example, we use switch to determine the name of a number based on its value.
#include <iostream>
int main() {
int number = 2;
switch (number) {
case 1:
std::cout << "One" << std::endl;
break;
case 2:
std::cout << "Two" << std::endl;
break;
case 3:
std::cout << "Three" << std::endl;
break;
default:
std::cout << "Invalid number" << std::endl;
break;
}
return 0;
}
Determine the mathematical operation based on the operator
This code shows how to use switch to select the mathematical operation based on the provided operator.
#include <iostream>
int main() {
char operator = '+';
switch (operator) {
case '+':
std::cout << "Addition" << std::endl;
break;
case '-':
std::cout << "Subtraction" << std::endl;
break;
case '*':
std::cout << "Multiplication" << std::endl;
break;
case '/':
std::cout << "Division" << std::endl;
break;
default:
std::cout << "Invalid operator" << std::endl;
break;
}
return 0;
}
Determine the name of a day of the week
Here, we use a switch to determine the name of the day of the week based on its 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;
// Add more cases for other days
default:
std::cout << "Invalid day" << std::endl;
break;
}
return 0;
}
Determine the name of a month (with enumeration)
In this example, we use a switch to determine the name of a month based on its value in an enumeration.
#include <iostream>
enum Months { January = 1, February, March, April, May, June, July, August, September, October, November, December };
int main() {
Months month = May;
switch (month) {
case January:
std::cout << "January" << std::endl;
break;
case February:
std::cout << "February" << std::endl;
break;
// Add more cases for other months
default:
std::cout << "Invalid month" << std::endl;
break;
}
return 0;
}
These examples have the purpose of showing how to use the Switch conditional. It does not mean it’s the best way to solve the problem they address. Normally, there are better alternatives.
