The conditional SWITCH is a control structure that offers an alternative to the IF-ELSE conditional for making decisions based on multiple cases.
The SWITCH conditional allows evaluating an expression and executing different blocks of code based on the value of that expression.
In natural language, the SWITCH conditional
Depending on the value of ‘this’, do one of these things…
Generally, its syntax is as follows,
switch(expression):
case value1:
// code to execute if the expression is equal to value1
break;
case value2:
// code to execute if the expression is equal to value2
break;
case value3:
// code to execute if the expression is equal to value3
break;
default:
// code to execute if the expression does not match any of the previous cases
In the case of a SWITCH conditional, an expression that provides a value (for example, it can be a number or a string) is used instead of a condition.
During the execution of the SWITCH, the expression is evaluated, and its value is compared with each of the possible defined cases. If there is a match, the block of code corresponding to that case is executed.
The last case default
is optional, and it is executed when the expression does not match any of the previously defined cases.
You may have also noticed the use of the break
statement after each block of code, to exit the conditional and avoid the execution of other cases.
In some languages, this break
is not mandatory and allows the execution of more than one block in case of a match. This was called ‘fallthrough’ and generated difficult and error-prone code.
Nowadays, ‘fallthrough’ is considered an undesirable practice. So much so that many languages prohibit its use and require always putting break
.
Comparison with IF-ELSE
The code I showed earlier is equivalent to the following IF-ELSE block.
if (expression == value1)
{
// code to execute if the expression is equal to value1
}
else if (expression == value2)
{
// code to execute if the expression is equal to value2
}
else if (expression == value3)
{
// code to execute if the expression is equal to value3
}
else
{
// code to execute if the expression does not match any of the previous cases
}
In general, it is not much longer than the SWITCH conditional. Partly because the requirement to include the break makes the SWITCH unnecessarily long.
Examples of SWITCH conditional in different languages
Let’s look at examples of using the SWITCH conditional in different programming languages:
For example, in C++, Java, C# and JavaScript, the code would be as follows,
int weekDay = 3;
switch (weekDay)
{
case 1:
// actions to take if it's Monday
break;
case 2:
// actions to take if it's Tuesday
break;
case 3:
// actions to take if it's Wednesday
break;
case 4:
// actions to take if it's Thursday
break;
case 5:
// actions to take if it's Friday
break;
default:
// actions to take if it's the weekend
break;
}
For example, in the case of Python, starting from version 3.10, it incorporates a SWITCH structure, but in this case, the reserved word match
is used.
match weekDay:
case 1:
print("Monday")
case 2:
print("Tuesday")
case 3:
print("Wednesday")
case 4:
print("Thursday")
case 5:
print("Friday")
case _:
print("Weekend")
However, not all languages have a SWITCH conditional. In fact, as I mentioned, Python did not have this structure before version 3.10. In languages that do not have SWITCH, it must be done using the equivalent IF-ELSEIF code.
Use with enumerations
Often, SWITCH conditionals are used with enumerations, which greatly improves their readability. For example, the previous code would look like this,
enum WeekDay
{
Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday
}
var day = WeekDay.Wednesday;
switch (day)
{
case WeekDay.Monday:
// Actions to take if it's Monday
break;
case WeekDay.Tuesday:
// Actions to take if it's Tuesday
break;
case WeekDay.Wednesday:
// Actions to take if it's Wednesday
break;
case WeekDay.Thursday:
// Actions to take if it's Thursday
break;
case WeekDay.Friday:
// Actions to take if it's Friday
break;
default:
// Actions to take if it's the weekend
break;
}
A controversial conditional Advice
The use of the SWITCH conditional has generated some controversy and debate among developers. There is an opinion that considers that the SWITCH is unnecessary. We have even seen that modern programming languages have chosen not to include it in their syntax.
In general, it is considered a somewhat outdated practice. For certain developers, the equivalent block in IF-ELSEIF is more readable. Additionally, it is more powerful, as it has no restrictions on the conditionals to use.
On the other hand, there are other developers who argue that the SWITCH conditional offers a more compact and readable syntax in cases where multiple comparisons with a single variable are needed.
Moreover, the proper use of the SWITCH conditional can improve program performance compared to IF-ELSEIF structures. This is because the SWITCH conditional allows for a direct evaluation of the cases, avoiding the need to evaluate multiple conditions in cascade.
Well, I suppose it comes down to personal preference. It really isn’t a conditional that is as useful as it seems, and it is currently in disuse. Although, on the other hand, it is regaining popularity thanks to pattern matching.
In any case, here you have it explained. Many languages implement it, so it is interesting that, at the very least, you know of its existence and know how to use it.