Chaining Conditions with elif
An if-else statement creates two mutually exclusive branches: one executes if the condition is true, the other if it is false.
Try it: if / elif / else
How Python tests if / elif conditions from the top, runs only the first true branch, and how and / or stop evaluating as soon as the answer is known (short-circuiting).
How it works
- Python evaluates the if condition first.
- If it is False, it tries each elif in order; the first True one runs and the rest are skipped without being tested.
- If none is True, the else branch runs.
- and stops at the first False, or stops at the first True — the right side may never be evaluated.
Default run (5 steps): score = 84; A = 90, B = 80, C = 70. Python tests the conditions from the top. … print(grade) shows B.
Loading the simulation…
Choosing a Path
Programs often need to respond differently depending on a condition. An if-else statement creates two mutually exclusive branches: one runs when the condition is true, and the other runs when the condition is false. Chaining conditions with elif extends this alternative-execution idea so a program can organize several possible tests instead of treating every situation as only true or false.
Reading a Condition Chain
A basic if-else statement has two possible branches. The condition is evaluated first. If it is true, the indented statements under if run. If it is false, the indented statements under else run. Because a condition must be either true or false, exactly one of these two branches executes.
An elif chain applies the same alternative-execution reasoning to several conditions. The program checks the conditions in their written order. When a condition matches, its branch is selected and the remaining alternatives are not selected for that decision. An else branch provides the remaining alternative when none of the preceding conditions matches.
Tracing a Selection
Following the First Matching Branch
Trace the condition chain for temperature = 18 and determine which message is produced.
- 1Start with the if condition
The value 18 is compared with 30. The condition 18 >= 30 is false, so the if branch is skipped.
- 2Check the elif condition
The value 18 is compared with 15. The condition 18 >= 15 is true, so the elif branch is selected.
- 3Stop checking alternatives
The selected branch produces its message. The else branch is not selected because an earlier condition matched.
The program produces the message Mild.
MildThe important tracing habit is to evaluate one condition at a time and follow only the path that matches. Do not read every indented print statement as if it will run. Once a branch is selected, the other alternatives in that chain are not selected for the same decision.
Writing the Structure
The colon marks the start of each branch's indented block. The statements belonging to a branch must be indented consistently. The conditions are arranged as an ordered chain: the initial if condition is considered first, an elif condition provides another alternative, and else represents the remaining alternative.
Mistakes That Change the Path
Common mistake Leaving out the colon after a condition or after else
What it looks likeif score >= 50 print("Pass")Why it failsThe branch syntax requires a colon before the indented block begins.
Do this insteadWrite if score >= 50: before the indented statement.
Common mistake Using inconsistent indentation inside a branch
What it looks likeif ready: print("Start") print("Go")Why it failsStatements in the same block must be indented consistently.
Do this insteadAlign statements that belong to the same branch.
Common mistake Putting a broader condition before a more specific condition
What it looks likeif score >= 0: print("Nonnegative") elif score >= 50: print("Pass")Why it failsFor a score of 70, the first condition already matches, so the later alternative is not selected.
Do this insteadOrder conditions so that the intended earlier cases are tested before broader cases.
Common mistake Tracing every branch instead of evaluating the condition
What it looks likeAssuming that both the if message and the else message will print
Why it failsAn if-else statement provides mutually exclusive branches, so exactly one branch executes.
Do this insteadEvaluate the condition and follow only the matching branch.
Practice the Trace
What do you think happens?
What message does this chain produce when points is 42?
Reveal answer
Pass
The first condition, points >= 80, is false. The elif condition, points >= 40, is true, so its branch is selected and the else branch is skipped.
Create an if-elif-else chain for a variable named balance. Print Positive when balance is greater than 0, Zero when balance equals 0, and Negative for the remaining case. Before running it, trace the chain for balance = 0 and predict which message appears.
Hints
- Place the test for balance greater than 0 in the if branch.
- Place the equality test in the elif branch.
- Use else for the remaining case.
- Check the colon and indentation on every branch.
Key Takeaways
- 1An if-else statement creates two mutually exclusive branches for true and false outcomes.
- 2An elif chain organizes additional alternative conditions in a written order.
- 3Trace a chain by evaluating the conditions one at a time and following the selected branch.
- 4Use a colon after branch conditions and else, and indent each block consistently.
- 5Place conditions in an order that makes the intended branch reachable before a broader condition matches.
At a glance
- Conditional statements direct execution into alternative branches.
- An if-elif-else chain checks conditions in order and selects a matching path.
- Exactly one branch of an if-else decision executes.
- Correct colons, consistent indentation, and careful condition ordering are essential.
- Tracing begins by evaluating the condition, then following only the selected branch.