Language: EN

programacion-anidacion-condicionales

Nested Conditionals

Nested conditionals are a technique that allows us to structure the decision logic of a program at multiple levels.

Basically, by nesting conditionals, we put one conditional inside another conditional. Of course, we can place them in both the IF and the ELSE.

This way, instead of having a single condition followed by a single instruction, we can evaluate additional conditions within nested conditional blocks.

programacion-condicional-nested

Translated into code, it would look like this:

if(condicion1) // external conditional
{
    if(condicion2) // internal conditional
	{	    
	}
	else
	{
	}
}
else
{
}

Of course, we could put the internal conditional in both the IF block and the ELSE block. We could even go further, and nest more nested conditionals within the nested ones, and more within the nested ones… until ending up with a mess that no one understands 😉.

Examples of nested conditionals in different languages

It will be easier to understand if we see an example in different programming languages.

In these languages, which inherit the syntax of C, the nesting of components would look something like this.

if (condicion1)
{
    if (condicion2)
    {
        // Action if both conditions are true
    }
    else
    {
        // Action if condition2 is false
    }
}
else
{
    // Action if condition1 is false
}

Which would be very similar to the case of Python, simply taking into account the difference in syntax when making the conditional.

if condicion1:
    if condicion2:
        # Action if both conditions are true
    else:
        # Action if condition2 is false
else:
    # Action if condition1 is false

In all these cases

  • If condition1 is met, the internal conditional block would be executed.
    • If condition2 is met, the IF block of the internal conditional would be executed.
    • If condition2 is not met, the ELSE block of the internal conditional would be executed.
  • If condition1 is not met, the ELSE block of the external conditional would be executed.

Cleaning Tips Tips

Nested conditionals are a very common practice. You will use them frequently and, in itself, it doesn’t have to be a bad thing.

However, nested conditionals always introduce a degree of complexity when reading and interpreting the code.

For example, suppose the following code. Interpreting this code “forces me to think too much”. Thinking is fine, but if you are looking for an error, or reading someone else’s code, or it’s Thursday and you’ve been reviewing code for 8 hours a day… the easier you make it for me, the better.

if (isValidUser)
{
    if (hasPreviusLog)
    {
        // isValidUser and hasPreviusLog
    }
    else
    {
        // isValidUser and NOT hasPreviusLog
    }
}
else
{
    // Not isValidUser
}

With nested conditionals “I have to think” about what conditions I end up executing each block of code. For example, the previous code is equivalent to the following, which is much easier to read and interpret.

if (isValidUser && hasPreviusLog)
{
}

if (isValidUser && hasPreviusLog == false)
{
}

if (isValidUser == false)
{
}

It’s a very simple example. Imagine the little hell that can be created when someone makes a code fragment with a conditional, inside a conditional, inside a conditional.

It is very important to avoid excessive nesting since it can make it difficult to understand the code. If the conditional becomes too complicated, consider other alternatives, such as using logical operators like AND (&&) and OR (||) to combine conditions.

It is also important to consider that nesting is much less intuitive in the ELSE blocks. This is because negated logic is always much more difficult to process mentally than positive logic.

In short, always keep in mind the clarity of your code and keep the structure of the code blocks clear and readable.