Conditional statements
Implement a decision table and check its boundaries in the right order.
On this page
The concept
An if/elif/else chain selects the first true branch. Later branches are skipped even if their conditions would also be true. This makes ordering part of the program's logic. Reject an impossible score first, then test the more specific grade thresholds before the more general ones.
Independent if statements behave differently: several can run in one execution. Choose an elif chain when the outcomes are mutually exclusive. A conditional expression, a if condition else b, chooses a value and is useful for short alternatives, but it should not hide a complicated decision tree.
Worked example
score = 82
if not 0 <= score <= 100:
print("Score must be between 0 and 100.")
elif score >= 90:
print("Excellent")
elif score >= 70:
print("Good")
elif score >= 50:
print("Passed")
else:
print("More practice needed")
age = 17
category = "Adult" if age >= 18 else "Child"
print(category)Trace the result
- The first condition rejects scores outside 0 through 100.
- For score 82, the >=90 branch fails and the >=70 branch succeeds, so only Good is printed.
- The final conditional expression chooses the category for age 17 independently of the score decision.
Good
ChildLine-by-line walkthrough
Line 1: score = 82
Evaluate 82, then bind or update score with the result. This literal is prepared example data. Assignment itself does not imply a deep copy.
Lines 2–11: if not 0 <= score <= 100:
Evaluate not 0 <= score <= 100. Execute the indented body if it is truthy; otherwise follow the alternative branch, if present. Branch order and indentation are part of the behavior.
Line 3: print("Score must be between 0 and 100.")
Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.
Alternative or completion suite: read the statements below as part of this branch.
Lines 4–11: elif score >= 90:
Evaluate score >= 90. Execute the indented body if it is truthy; otherwise follow the alternative branch, if present. Branch order and indentation are part of the behavior.
Line 5: print("Excellent")
Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.
Alternative or completion suite: read the statements below as part of this branch.
Lines 6–11: elif score >= 70:
Evaluate score >= 70. Execute the indented body if it is truthy; otherwise follow the alternative branch, if present. Branch order and indentation are part of the behavior.
Line 7: print("Good")
Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.
Alternative or completion suite: read the statements below as part of this branch.
Lines 8–11: elif score >= 50:
Evaluate score >= 50. Execute the indented body if it is truthy; otherwise follow the alternative branch, if present. Branch order and indentation are part of the behavior.
Line 9: print("Passed")
Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.
Alternative or completion suite: read the statements below as part of this branch.
Line 11: print("More practice needed")
Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.
Line 12: age = 17
Evaluate 17, then bind or update age with the result. This literal is prepared example data. Assignment itself does not imply a deep copy.
Line 13: category = "Adult" if age >= 18 else "Child"
Evaluate 'Adult' if age >= 18 else 'Child', then bind or update category with the result. Assignment itself does not imply a deep copy.
Line 14: print(category)
Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.
Common mistake
Testing >=50 before >=90 would capture high scores too early. Test 49, 50, 69, 70, 89, 90, and invalid boundaries.