Conditionals - The Decision Tree
7 / 15

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

Pixel Bonsai Decision Tree
The Bonsai Decision Tree:
Execution starts at the trunk (if) and branches outward (elif / else). Only one branch blooms per run!
IF condition is True?
✓ 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

Branch 1: if num > 0
Waiting...
Branch 2: elif num < 0
Waiting...
Branch 3: else
Waiting...
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
Ivory Tulip
Woodland Milestone: Your programs can now think and choose paths! Next, we'll place decisions inside decisions with Nested Conditionals.
An if-elif-else chain is a branching tree. Python takes the first branch whose condition is True and skips the rest.
Wizard Guide