Language: EN

programacion-condicional-switch

The SWITCH conditional

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 you to evaluate an expression and execute different code blocks based on the value of that expression.

Put in natural language, the SWITCH conditional says:

Depending on what ‘this’ is worth, do one of these things…

In general, 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, instead of a condition, an expression that provides a value is used. For example, it can be a number, or text.

During the execution of the SWITCH, the expression is evaluated, and its value is compared with each of the defined possible cases. If there is a match, the corresponding code block for 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 previously.

You have also noticed the use of the break statement 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 difficult and error-prone code.

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

Comparison with IF-ELSE

The code I showed above 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. In large part, it is because the requirement to use the break statement 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 diaSemana = 3;
switch (diaSemana)
{
    case 1:
        // actions to take if it is Monday
        break;
    case 2:
        // actions to take if it is Tuesday
        break;
    case 3:
        // actions to take if it is Wednesday
        break;
    case 4:
        // actions to take if it is Thursday
        break;
    case 5:
        // actions to take if it is Friday
        break;
    default:
        // actions to take if it is the weekend
        break;
}

For example, in the case of Python, from version 3.10 onwards, it incorporates a SWITCH structure, but in this case, the reserved word match is used.

match diaSemana:
    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 a SWITCH conditional, it should be performed using the equivalent IF-ELSEIF code.

Use with enumerations

Often, SWITCH conditionals are used with enumerations, which greatly improves readability. For example, the previous code would look like this,

enum DiaSemana
{
    Lunes, Martes, Miercoles, Jueves, Viernes, Sabado, Domingo
}

var dia = DiaSemana.Miercoles;
switch (dia)
{
    case DiaSemana.Lunes:
        // Actions to take if it is Monday
        break;
    case DiaSemana.Martes:
        // Actions to take if it is Tuesday
        break;
    case DiaSemana.Miercoles:
        // Actions to take if it is Wednesday
        break;
    case DiaSemana.Jueves:
        // Actions to take if it is Thursday
        break;
    case DiaSemana.Viernes:
        // Actions to take if it is Friday
        break;
    default:
        // Actions to take if it is the weekend
        break;
}

A controversial conditional Tips

The use of the SWITCH conditional has generated some controversy and debate among developers. There is an opinion that considers the SWITCH conditional 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. It is also more powerful, as it has no restrictions on the conditionals to use.

On the other hand, there are other developers who believe that the SWITCH conditional offers a more compact and readable syntax in cases where multiple comparisons need to be made with a single variable.

Additionally, the proper use of the SWITCH conditional can improve the performance of the program compared to IF-ELSEIF structures. This is because the SWITCH conditional allows for a direct evaluation of cases, avoiding the need to evaluate multiple cascading conditions.

Well, I suppose there’s no accounting for taste. It is not a conditional that is as useful as it seems, and is currently falling into disuse. Although, on the other hand, it is regaining a new resurgence thanks to pattern matching.

In any case, there you have it explained. Many languages implement it, so it is interesting that, at least, you know of its existence and know how to use it.