Choosing Which Lines Run
An if / elif / else statement runs exactly one branch, chosen by the first boolean test that comes out True. · 12 min
A boolean expression answers a question; an if statement acts on the answer. It reads a test, and when the test is True it runs the indented lines beneath it — otherwise it skips them. Add elif (else-if) branches to offer more tests, and a final else to catch everything left over. The iron rule: Python checks the tests from top to bottom, runs the block under the first one that is True, then leaves the whole statement. Exactly one branch runs, never two.
Guess before you learn
Suppose x is 5. The code tests if x > 0: first, then elif x > 3:. Both tests are true for 5. Which branch runs?
Only the first branch runs. Python checks the tests top to bottom and stops at the first True one — x > 0 — running its block and skipping the rest. Even though x > 3 is also true, its branch never gets a turn. If you guessed both, you have found the single most important fact about elif: at most one branch ever runs.
9–12
3–5
if runs some lines only when a test is true. if score >= 60: might print pass. Add else: and Python runs those other lines instead when the test is false. So one of the two always happens — pass or not — but never both.
Need more than two choices? elif adds another test in the middle. Python tries each test in order and takes the block under the first one that fits.
6–8
The shape is: an if test, optional elif tests, an optional else. Each header ends in a colon, and the lines it controls are indented beneath it. Python evaluates the tests top to bottom and runs the block under the first True test, then jumps past the whole statement. else has no test — it runs only when every test above it failed.
Indentation is not decoration in Python; it is how the language knows which lines belong to a branch. Lines indented under an if run only when its test is true; lines at the outer level run no matter what the test decided.
9–12
Order matters when tests overlap. If you check score >= 60 before score >= 90, the second can never fire — the first catches every high score first. Put the most specific or highest tests earliest. A chain of separate if statements differs from one if / elif chain: separate ifs each get evaluated, so several can run; an elif chain runs at most one.
else is optional. Without it, an if / elif chain can finish having run no branch at all, if every test was false. When you need a guaranteed fallback — some branch that always runs — supply the else.
K–2
Look outside. If it is raining, take an umbrella. Else, leave it home. You check one thing, then do one thing. You never take the umbrella and leave it home at the same time.
Undergrad
An if / elif / else chain is a selection construct: a sequence of guarded blocks whose guards are evaluated in order, with the first satisfied guard's block executed. It is equivalent to nested if/else, flattened for readability. Because evaluation stops at the first true guard, the branches are effectively mutually exclusive even when their conditions overlap.
A common refactor is the guard clause: handle edge cases with early returns at the top of a function rather than nesting the main logic deep inside an if. The result reads top to bottom as a short list of rejections followed by the normal path.
Postgrad
In the vocabulary of structured programming, selection is one of the three canonical control structures alongside sequence and iteration; the Bohm-Jacopini theorem shows these three suffice to express any computable control flow. The if / elif / else chain is a linearized decision list, distinct from a true multi-way branch such as a jump table.
Reasoning about a branch uses case analysis: to prove a postcondition holds after the statement, show it holds at the end of every branch given that branch's guard, and that the guards are exhaustive. Python guarantees exhaustiveness only when an else is present; otherwise the empty fall-through path must satisfy the postcondition too.
branch
One of the blocks in an if / elif / else statement. At most one branch runs each time the statement is reached.
The tests in a chain are just the boolean expressions from the last folio, now put to work. To predict what a chain does, you trace it: read the value of each variable, evaluate each test in turn, and follow the first one that is True. When several tests could be true, only their order decides the outcome — which is exactly where careful reading pays off.
Trace this chain with `n = 7` — the steps fade as you master them
n < 0.n < 0 -> False
n == 0.n == 0 -> False
n < 10.n < 10 -> True
print('small positive')
Why is this true?
Why does putting a broad test like x > 0 before a narrow one like x > 100 make the narrow branch unreachable?
Python runs the first branch whose test is True and then stops. Every value that passes x > 100 also passes x > 0, so the broad test always fires first and the narrow branch never gets a turn. Order specific tests before general ones.
An if chooses whether a block runs; elif and else extend that into a clean pick-exactly-one decision. You now have tests and the machinery to act on them. But a program that runs each line only once is limited. The next unit introduces repetition: the while loop, which runs a block again and again for as long as a test stays True.
Practice — new ink and old, interleaved
1.Evaluate 10 % 3, the remainder of 10 divided by 3.
2.Which grouping does Python use for not True and False?
3.(4 < 2) or not (3 == 3) evaluates to?
4.Match each operator to its meaning.
5.Review from folio 1: put these lines in the order they run.
- print("Ready?")
- answer = input()
- print("Go!")
6.Review from folio 2: after x = 6 then x = x + x, what is the value of x?
7.x = 0. if x: print('yes') / else: print('no'). What is printed?
8.Order the tests as Python evaluates them for grade = 85 in a chain checking >= 90, then >= 80, then else.
- Test 85 >= 90, which is False
- Test 85 >= 80, which is True
- Assign 'B' and stop
9.Without looking back: in an if / elif / else chain, which branch runs, and what does else do?
The branch under the first test that is True runs, and only that one; else runs when every test above it was False.
How close were you? Grade yourself honestly — it sets your review date.