Language: EN

programacion-anidacion-condicionales

Nested Conditionals

Nesting conditionals is 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 nest them in both the IF and the ELSE.

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

programacion-condicional-nested

Nested IF Scheme

Which, translated into code, would look like this:

if(condition1) // external conditional
{
    if(condition2) // internal conditional
	{	    
	}
	else
	{
	}
}
else
{
}

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

Examples of nesting conditionals in different languages

Let’s make it easier by looking at an example in different programming languages.

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

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

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

if condition1:
    if condition2:
        # 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 true, the block of the internal conditional would be executed.
    • If condition2 is true, the IF block of the internal conditional would be executed.
    • If condition2 is false, the ELSE block of the internal conditional would be executed.
  • If condition1 is false, the ELSE block of the external conditional would be executed.

Best practices Tips

Nesting conditionals is a very common practice. You will use it frequently. (and, in itself, it doesn’t have to be a bad thing).

However, nesting conditionals always introduces a degree of complexity when reading and interpreting the code.

For example, let’s consider the following code. Interpreting this code “forces me to think too much” (think about whether 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… the easier you make it, the better).

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

With nested conditionals, “I have to think” about which conditions 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 && hasPreviousLog)
{
}

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

if (isValidUser == false)
{
}

It’s a very simple example. Imagine the little hell that can be created when someone writes a piece of code with a conditional inside a conditional inside a conditional.

It is very important to avoid excessive nesting as it can hinder code comprehension. If a 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 ELSE blocks. This is because negated logic is always much harder to process mentally than positive logic.

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