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.
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.
Start with the if condition: The value 18 is compared with 30. The condition 18 >= 30 is false, so the if branch is skipped.
Check the elif condition: The value 18 is compared with 15. The condition 18 >= 15 is true, so the elif branch is selected.
Stop 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
Leaving out the colon after a condition or after else
The branch syntax requires a colon before the indented block begins.
Fix:
Write if score >= 50: before the indented statement.Using inconsistent indentation inside a branch
Statements in the same block must be indented consistently.
Fix:
Align statements that belong to the same branch.Putting a broader condition before a more specific condition
For a score of 70, the first condition already matches, so the later alternative is not selected.
Fix:
Order conditions so that the intended earlier cases are tested before broader cases.Tracing every branch instead of evaluating the condition
An if-else statement provides mutually exclusive branches, so exactly one branch executes.
Fix:
Evaluate 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
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
- An if-else statement creates two mutually exclusive branches for true and false outcomes.
- An elif chain organizes additional alternative conditions in a written order.
- Trace a chain by evaluating the conditions one at a time and following the selected branch.
- Use a colon after branch conditions and else, and indent each block consistently.
- Place conditions in an order that makes the intended branch reachable before a broader condition matches.
Key Takeaways
- 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.