How Do Programs Make Decisions?
"Conditionals is how we allow our programs to make a decision. We use a condition to decide if we should execute a given block of code."
The Decision Structure
The Bonsai Decision Tree:
Execution starts at the trunk (if) and branches outward (elif / else). Only one branch blooms per run!
✓ YES (True)
Execute this branch's indented code
✗ NO (False)
Skip this branch & check next
The If-Elif-Else Chain
if condition1:
# Execute if condition1 is True
elif condition2:
# Execute if condition1 False, condition2 True
elif condition3:
# Execute if above False, condition3 True
else:
# Execute if ALL above are False
Critical Concept: Indentation creates hierarchy. The indented code belongs to the condition above it. This is how Python knows what code to run when a condition is True.
Interactive Bonsai Decision Tree
Enter a number and click Evaluate
CODE EXAMPLES
Real-World Waterfall: Grade Calculator & Key Principles
+ Explore
score = 85
if score >= 90:
grade = "A"
elif score >= 80:
grade = "B" # This runs! (85 >= 80)
elif score >= 70:
grade = "C" # Skipped (already found match)
else:
grade = "F" # Skipped (already found match)
print(grade) # Output: "B"
"Once a condition is True, the rest are skipped. It's like a waterfall — water flows down the first path it finds."
Key Principles
- if: Always comes first, tests one condition
- elif: Optional, can have many, tests additional conditions
- else: Optional, comes last, catches everything else
- Only ONE block executes — first True condition wins
Woodland Milestone: Your programs can now think and choose paths! Next, we'll place decisions inside decisions with Nested Conditionals.