programacion-condicional-switch

The SWITCH conditional

  • 5 min

The SWITCH conditional allows evaluating an expression and executing different code blocks based on the value of that expression.

It is a control structure that offers an alternative to the IF-ELSE conditional for making decisions based on multiple cases.

In natural language, it means:

Depending on the value of ‘this’, do one of these things…

In general, its syntax is as follows:

switch(expresion):
    case valor1:
        // code to execute if expression equals valor1
        break;
    case valor2:
        // code to execute if expression equals valor2
        break;
    case valor3:
        // code to execute if expression equals valor3
        break;
    default:
        // code to execute if expression doesn't match any of the previous cases
Copied!

In the case of a SWITCH conditional, instead of a condition, an expression that provides a value is used (for example, it can be a number or text).

During execution, the expression is evaluated, and its value is compared with each of the defined possible cases. If there is a match, the code block corresponding to that case is executed.

The last default case is optional and is executed when the expression does not match any of the cases we have defined earlier.

You have also noticed the use of the break instruction after each code block 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 code that was difficult and prone to errors.

Nowadays, ‘fallthrough’ is considered a discouraged practice. So much so that many languages prohibit its use and always require the break.

Comparison with IF-ELSE

The code I showed earlier is equivalent to the following IF-ELSE block.

if (expresion == valor1)
{
    // code to execute if expression equals valor1
}
else if (expresion == valor2)
{
    // code to execute if expression equals valor2
}
else if (expresion == valor3)
{
    // code to execute if expression equals valor3
}
else
{
    // code to execute if expression doesn't match any of the previous cases
}
Copied!

In general, it’s not much longer than the SWITCH conditional. Largely because the requirement to include the break makes the SWITCH unnecessarily long.

Examples of SWITCH conditional in different languages

Let’s see 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 diaSemana = 3;
switch (diaSemana)
{
    case 1:
        // actions to perform if it's Monday
        break;
    case 2:
        // actions to perform if it's Tuesday
        break;
    case 3:
        // actions to perform if it's Wednesday
        break;
    case 4:
        // actions to perform if it's Thursday
        break;
    case 5:
        // actions to perform if it's Friday
        break;
    default:
        // actions to perform if it's Weekend
        break;
}
Copied!

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")
Copied!

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;
}
Copied!

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.