Conditionals - The Decision Tree
Card 7 of 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

IF condition is True?
✓ YES
Execute this code
✗ NO
Skip this code

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 Decision Tree

Enter a number and click Evaluate

Real-World Example: Grade Calculator

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
Mental Model: A conditional is like a choose-your-own-adventure book. Based on the condition (your choice), you follow one path and skip the others.
[##############................] 07/15
> [WHY_IT_MATTERS]:
The ability to choose is what makes code intelligent. With conditionals, the machine listens and responds.
Cute Computer Mascot