1.2.15 · D2Introduction to Programming (Python)

Visual walkthrough — if - elif - else — syntax, indentation rules

1,907 words9 min readBack to topic

This is the visual companion to the parent topic. We build the central result — first-True-wins, exactly-one-branch — from absolute zero.


Step 1 — A program with no choices is a straight line

WHAT. Before any if, a program is just a list of instructions run top to bottom. Line 1, then line 2, then line 3. No skipping, no choosing.

WHY. We must first feel the limitation to appreciate what branching adds. A straight line of code always does the same thing, no matter the data.

PICTURE. Look at the figure. The blue arrow is "control flow" — the invisible finger that points at the line Python is running right now. It only ever moves straight down. There is no fork anywhere.


Step 2 — A condition is a yes/no gate

WHAT. A condition is any expression that is either True or False. Example: marks >= 40. Feed it data, it answers one bit: yes or no.

WHY. To choose a path, the program needs something to choose on. That something is a boolean — see Booleans and comparison operators. The condition is the question written on a door.

PICTURE. The yellow diamond is the condition. One arrow goes in; two arrows come out — a green "True" exit and a red "False" exit. Exactly one of them fires, decided entirely by the data.


Step 3 — One if bends the line into a fork

WHAT. Attach a block to the True exit. Now the dot either enters the block (green path) or skips it (red path). Both paths rejoin afterwards.

WHY. This is the smallest possible branch. Notice the red path skips the indented lines but still lands on the code below — nothing is lost, only some lines are optionally jumped.

PICTURE. Two colours of arrow now leave the diamond. The green path dives into the indented block (the box), the red path goes around it. They merge at the un-indented line below. This merge point is why "print("done") at the same indent runs regardless" (parent Example 4).


Step 4 — else fills the red path so nothing falls through

WHAT. Put a block on the red (False) exit too. Now both exits have somewhere to go.

WHY. With if alone, "False" meant "do nothing extra". With else, "False" means "do this instead". The two blocks are mutually exclusive: the dot cannot be on the green and red arrow at the same time.

PICTURE. Green exit → block A. Red exit → block B. Trace the dot: it is physically impossible to touch both boxes, because it left the diamond through only one arrow. Exactly one of A, B runs.


Step 5 — elif stacks gates, and each one inherits "all above were False"

WHAT. An elif is a second gate placed on the red exit of the first. You only reach it if the first said False. Chain more elifs and you get a ladder of gates.

WHY. This is the key insight of the whole topic. Reaching gate already means gates all answered False — that fact is baked into the wiring, not re-tested. So each branch secretly carries all the "not" conditions before it.

PICTURE. Follow the red arrows down the ladder. To land on the diamond you had to leave through red, i.e. . To land on : . Every deeper rung inherits more "nots" (the red chain on the left).


Step 6 — The ladder is exhaustive and exclusive

WHAT. Add the final else at the bottom of the red chain. Now every possible input lands in exactly one box.

WHY. Two guarantees, provable by staring at the ladder:

  • Exclusive (never two): a green exit ends the ladder, so you can't reach a later gate.
  • Exhaustive (never zero): if all gates say False, the red chain flows all the way to else.

PICTURE. The colours partition the input space into stripes with no gaps and no overlaps. Every value of score falls into exactly one coloured region — A, B, C, or F.


Step 7 — Degenerate cases: what if the ladder is broken?

WHAT. We now stress-test the wiring with the parent's classic mistakes and edge inputs.

WHY. A derivation must cover every scenario, including the ones that break the guarantees. Each breakage maps to a broken picture.

PICTURE (left → right):

  1. Separate ifs (no red chain): both gates take input independently, so two boxes can run and a later one overwrites the earlier — the stripes overlap. This is the parent's "many independent ifs" bug.
  2. No else and all gates False: the red chain flows off the bottom into empty spacezero blocks run. That's fine only if you want "do nothing".
  3. A gate that is True for everything below it placed first (e.g. >=70 before >=90): it swallows the whole region, starving lower gates — the ladder is exclusive but wrong-ordered, so 95 wrongly gets C.

The one-picture summary

Everything above compressed into a single diagram: one input drops in the top, follows green when a gate says True (ending the ladder) or red when it says False, and always lands in exactly one coloured block — with else catching whatever the gates left behind.

Recall Feynman retelling — the whole walkthrough in plain words

Picture a marble dropped into a pinball machine of gates. Each gate asks one yes/no question about the marble. Green means "yes, fall into this bucket and stop" — the marble never sees another gate. Red means "no, roll down to the next gate." Because "roll down" is the only way to reach a lower gate, arriving there already proves every gate above said no — that's the secret pile of "not" conditions each branch carries for free. If the marble reaches the very bottom having been rejected by all gates, a final wide else bucket catches it so nothing ever falls on the floor. Two facts fall out just from watching the marble: it can only be in one bucket (a green exit ends the ride), and it's always in some bucket (the else floor). That's the entire meaning of if / elif / else: exactly one path, chosen by the first "yes".


Active recall

Why can no two branches of a clean if/elif/else chain run?
A green (True) exit ends the ladder, so control never reaches a later gate.
Why is a chain with else guaranteed to run at least one block?
If every gate says False, the red chain flows all the way into the else bucket.
What does reaching the elif for secretly guarantee?
That — all earlier gates were False.
What breaks when you use separate ifs instead of elif?
The red chain vanishes, gates read input independently, stripes overlap, and later matches overwrite earlier ones.
For score = 85 in >=90 / >=80 / >=70, why is the >=70 gate never checked?
The >=80 gate fired True and ended the ladder before >=70 was reached.

Connections

  • Booleans and comparison operators — each gate's yes/no bit
  • Logical operators and, or, not — the and in the branch rule
  • Truthiness in Python — what counts as "True" at a gate
  • Indentation and code blocks — how the boxes are drawn
  • while and for loops — gates that loop back up
  • match-case statement — a wide ladder for one variable
  • Ternary conditional expression — a two-gate ladder squeezed onto one line