Nested Conditionals

An if inside an if, why the indentation climbs so fast, and the guard clause that flattens most of it back out.

Overview

When nesting is just `and`

if age >= 18:
    if member:
        allow()

If the inner if is the entire body of the outer one, and there is no else on either, the two conditions are simply both required:

if age >= 18 and member:

Same behaviour, one level of indentation, and the condition reads as one thought instead of two.

nested_if.py

nested_if.py Python 3
Output

                    

combining.py

combining.py Python 3
Output

                    

Worth knowing

If the inner if is the only thing in the outer one, and says the same thing flatter.
A guard clause handles one case and returns, so the rest of the function stops being indented.
elif is a chain, not a nest. Same indentation, one decision.
Three levels of indentation is the usual signal to extract a function.

Nested Conditionals: A Practical Guide

An if inside an if is sometimes exactly right and more often a shape that grew. The useful skill is recognising which one you have, because most nesting can be flattened without changing the logic.

When it genuinely nests

Nesting earns its place when the branches diverge — when the inner decision only makes sense inside the outer one, and each has its own alternative:

if age < 18:
    return 0 if member else 5
return 8 if member else 12

Here "member" means something different in each branch. That is a real tree, not an accidental one.

Guard clauses flatten the rest

The most common nested shape is a series of refusals, each with an else:

if active:
    if no_fines:
        if available:
            return "yes"

Four levels deep, and the success case — the thing the function is for — is buried furthest from the left margin, while every failure sits in an else far from the condition that caused it.

Inverted, each failure handles itself and leaves:

if not active:    return "membership inactive"
if fines > 0:    return "unpaid fines"
if not available: return "book is out"
return "yes"

Now every rule sits beside its own message, the happy path is the last line at zero indentation, and adding a rule is one line rather than another level. The page runs both over the same four cases and prints them side by side, because the point is that the behaviour is identical and only the shape changed.

elif is not nesting

if n > 100: ... elif n > 50: ... else: ...

An elif chain is one decision with several outcomes, all at the same indentation. Reaching for a nested if where an elif would do is how a flat choice becomes a staircase.

The rule of thumb

Two levels is normal. Three is worth a second look. Four almost always means either a set of guard clauses waiting to be inverted, or a block that wants to be its own function.

Check yourself

0 of 3

Answer without scrolling back up.

  1. `if a:` containing only `if b:` with no else is the same as what?

  2. What is a guard clause?

  3. How does an `elif` chain differ from nested ifs?

Cheat sheet

Nested Conditionals

An if inside an if is sometimes exactly right and more often a shape that grew. The useful skill is recognising which one you have, because most nesting can be flattened without changing the logic.

PYTHON · vizlearn.in/python/nested_conditionals.html

About the author

Ashish Jangra builds and maintains VizLearn. Every module here is written and the visualisation behind it hand-built, so the numbers in a readout come from the same code that draws the picture. Corrections are genuinely welcome and get priority over everything else — if a page states something wrong, or an animation misrepresents what the algorithm does, get in touch.