5  Making Decisions: If Statements

5.1 The Concept First

Life is full of decisions. Every moment, we evaluate conditions and choose different actions based on what we find. Programs need this same ability - to look at information and decide what to do next.

This is the power that transforms programs from simple calculators into intelligent assistants.

5.2 Understanding Through Real Life

We Make Decisions Constantly

Think about your morning routine: - IF it’s raining → Take an umbrella - IF it’s cold → Wear a jacket
- IF alarm didn’t go off → Rush! - IF it’s weekend → Sleep in

Each decision follows a pattern: 1. Check a condition 2. If true, do something 3. If false, do something else (or nothing)

Decisions in Technology

Your phone makes thousands of decisions per second: - IF battery < 20% → Show low battery warning - IF face recognized → Unlock phone - IF notification arrives → Display alert - IF no internet → Show offline message

The Universal Pattern

Every decision has the same structure:

IF (something is true)
    THEN do this
ELSE
    do that instead

5.3 Discovering Decisions with Your AI Partner

Let’s explore how programs make intelligent choices.

Exploration 1: Types of Decisions

Ask your AI:

Give me 5 examples of decisions a smart home system makes, showing the IF-THEN pattern

Notice how each follows: condition → action.

Exploration 2: Real App Logic

Try this prompt:

What decisions does a music app make when you press play? List them as IF-THEN rules.

You’ll see layers of decisions that create smooth user experience.

Exploration 3: Decision Trees

Ask:

Draw a simple decision tree for an ATM withdrawal process

This reveals how decisions can branch and create complex behavior from simple rules.

5.4 From Concept to Code

Let’s see how Python expresses these decision patterns.

The Simplest Expression

Ask your AI:

Show me the absolute simplest Python if statement that checks if a number is positive. No functions or complexity.

You’ll get something like:

number = 10
if number > 0:
    print("It's positive!")

That’s it! The pattern is: - if - the decision keyword - condition - what to check - : - start of the action - Indented lines - what to do if true

Understanding the Flow

Let’s trace through:

age = 15
if age >= 13:
    print("You're a teenager!")
  1. Check: Is 15 >= 13?
  2. Yes (True)
  3. Do the indented action
  4. Continue with program

5.5 Mental Model Building

Model 1: The Fork in the Road

     Program flow
          ↓
    [IF condition?]
       ↙     ↘
    True    False
     ↓        ↓
  [Action]  [Skip]
     ↓        ↓
     → → → ←
   Continue program

Model 2: The Gatekeeper

if password == "secret123":
    🚪 Gate Opens → Enter
else:
    🚫 Gate Stays Closed → Stay Out

Model 3: The Traffic Light

if light == "green":
    → GO
elif light == "yellow":  
    → SLOW DOWN
else:  # red
    → STOP

5.6 Prompt Evolution Exercise

Practice getting decision examples from AI.

Round 1: Too Vague

show me if statements

You’ll get complex nested conditions and advanced patterns!

Round 2: More Specific

show me Python if statement examples

Better, but still might include functions and complex logic.

Round 3: Learning-Focused

I'm learning how programs make decisions. Show me the simplest possible if statement that checks user input.

Now we’re learning-sized!

Round 4: Building Understanding

Using that example, trace through what happens when the user enters different values

This builds deep understanding of flow.

5.7 Common AI Complications

When you ask AI about if statements, it often gives you:

def validate_user_input(value, min_val=0, max_val=100):
    """Validate user input with comprehensive checks"""
    if not isinstance(value, (int, float)):
        raise TypeError(f"Expected number, got {type(value).__name__}")
    
    if value < min_val or value > max_val:
        raise ValueError(f"Value must be between {min_val} and {max_val}")
    
    if value == min_val:
        print("Warning: At minimum threshold")
    elif value == max_val:
        print("Warning: At maximum threshold")
    elif value > (max_val - min_val) * 0.9 + min_val:
        print("Warning: Approaching maximum")
    elif value < (max_val - min_val) * 0.1 + min_val:
        print("Warning: Approaching minimum")
    
    return value

try:
    user_value = float(input("Enter value: "))
    validated = validate_user_input(user_value)
    print(f"Valid value: {validated}")
except (TypeError, ValueError) as e:
    print(f"Error: {e}")

Functions! Exceptions! Type checking! Complex math! This is enterprise code, not learning code.

5.8 The Learning Approach

Build understanding progressively:

Level 1: Single Decision

# Simplest decision
temperature = 30
if temperature > 25:
    print("It's hot today!")

Level 2: Two-Way Decision

# if-else: choosing between two options
password = input("Enter password: ")
if password == "opensesame":
    print("Welcome!")
else:
    print("Access denied!")

Level 3: Multiple Choices

# elif: checking multiple conditions
grade = int(input("Enter your score: "))
if grade >= 90:
    print("A - Excellent!")
elif grade >= 80:
    print("B - Good job!")
elif grade >= 70:
    print("C - Passing!")
else:
    print("Need more practice!")

Level 4: Combining Conditions

# Using 'and' and 'or'
age = int(input("Your age: "))
day = input("Is it weekend? (yes/no): ")

if age < 18 and day == "no":
    print("Time for school!")
elif age < 18 and day == "yes":
    print("Enjoy your weekend!")
else:
    print("You're an adult - your choice!")
NoteExpression Explorer: Boolean Logic

Conditions create True/False values (booleans): - Comparisons: <, >, <=, >=, ==, != - Combining: and (both true), or (at least one true), not (opposite) - age < 18 and day == "no" is only True when BOTH conditions are True

Ask AI: “Show me a truth table for ‘and’ and ‘or’ with simple examples”

5.9 Exercises

Exercise 4.1: Concept Recognition

Identifying Decision Patterns

For each scenario, identify: 1. What condition is checked 2. What happens if true 3. What happens if false

Scenario A: Automatic doors at a store Scenario B: Phone screen rotating Scenario C: Microwave timer reaching zero

Check Your Analysis

Scenario A - Automatic Doors: - Condition: Motion detected? - If True: Open doors - If False: Keep doors closed

Scenario B - Phone Rotation: - Condition: Phone tilted sideways? - If True: Rotate to landscape - If False: Stay in portrait

Scenario C - Microwave Timer: - Condition: Timer == 0? - If True: Beep and stop - If False: Keep counting down

Exercise 4.2: Prompt Engineering

Getting Clear Decision Examples

Start with: “password checker”

Evolve this prompt to get AI to show you: 1. A simple password check (one correct password) 2. Uses if-else structure 3. Clear messages for success/failure 4. No functions or complexity

Document your prompt evolution.

Effective Final Prompt “Show me a simple Python program that: 1. Asks for a password 2. Checks if it equals ‘secret’ 3. Prints ‘Access granted’ if correct 4. Prints ‘Access denied’ if wrong Use only if-else, no functions or loops”

Exercise 4.3: Pattern Matching

Finding Core Decision Logic

Ask AI for a “professional game menu system”. In the complex code: 1. Find all if statements 2. Identify the essential decisions 3. Rewrite as 5-10 simple if statements

Core Decisions Might Include
  • If choice == “start” → Begin game
  • If choice == “load” → Load saved game
  • If choice == “quit” → Exit program
  • If save exists → Show load option
  • If in game → Show different menu

Exercise 4.4: Build a Model

Visualizing Decision Flow

Create three different models showing how if-elif-else works: 1. A flowchart 2. A real-world analogy (not traffic lights) 3. A step-by-step story

Test your models by explaining to someone how programs decide.

Exercise 4.5: Architect First

Design Decision-Based Programs

Design these programs before coding:

  1. Simple Thermostat
    • Decisions: Too cold? Too hot? Just right?
    • Actions: Heat on/off, AC on/off, do nothing
  2. Movie Ticket Pricer
    • Decisions: Child? Senior? Weekend?
    • Actions: Apply different prices
  3. Simple Adventure Game
    • Decisions: Go left? Go right? Open door?
    • Actions: Different story outcomes

Write your design as: - List all conditions to check - Define actions for each condition - Plan the decision order (what to check first)

Then ask AI: “Implement this exact decision logic: [your design]”

Design Example Thermostat Design: - Get current temperature - If temp < 18: Print “Heating on” - Elif temp > 25: Print “AC on” - Else: Print “Temperature OK”

5.10 AI Partnership Patterns

Pattern 1: Decision Tables

Ask AI to create decision tables: - “Show this if statement as a decision table” - “Create a truth table for these conditions” - “Map all possible paths through this logic”

Pattern 2: Simplification Practice

Guide AI to simpler versions: 1. “Show a complex if statement” 2. “Now show the same logic more simply” 3. “Now make it beginner-friendly” 4. “Now use only concepts from chapters 1-3”

Pattern 3: Real-World Mapping

Connect decisions to life: - “Show if statements using a vending machine example” - “Explain elif using a restaurant menu” - “Compare nested ifs to decision trees”

5.11 Common Misconceptions

“else is required”

Reality: else is optional. Sometimes you only need to act when something is true:

if battery_low:
    show_warning()
# No else needed - just continue normally

“Conditions must be simple”

Reality: You can combine conditions:

if age >= 18 and has_id and not banned:
    allow_entry()

“Order doesn’t matter”

Reality: Order matters with elif - first match wins:

score = 85
if score >= 70:
    print("C")  # This runs
elif score >= 80:
    print("B")  # Never reached!

5.12 Real-World Connection

Every app uses decisions:

Social Media Feed:

if post.likes > 1000:
    mark_as_trending()
if user in post.friends:
    show_in_feed()
if content.is_video:
    add_play_button()

Online Shopping:

if item.in_stock:
    show_buy_button()
else:
    show_notify_me()
    
if cart.total >= 50:
    apply_free_shipping()

Video Games:

if player.health <= 0:
    game_over()
elif player.score >= next_level_score:
    advance_level()

5.13 Chapter Summary

You’ve learned: - Programs make decisions by checking conditions - if statements let programs choose different paths - elif handles multiple related choices - else provides a default action - Decision logic creates intelligent behavior

5.14 Reflection Checklist

Before moving to Chapter 5, ensure you:

5.15 Your Learning Journal

For this chapter, record:

  1. Decision Mapping: List 10 decisions your phone makes
  2. Flow Practice: Draw the flow of a simple if-elif-else
  3. Design Patterns: What order should conditions be checked?
  4. Real Programs: How would you add decisions to previous programs?
TipThe Power of Decisions

With variables (memory) and decisions (intelligence), your programs can now: - Remember user preferences - Respond differently to different inputs - Create personalized experiences - Handle errors gracefully

You’re no longer writing calculators - you’re creating responsive programs!

5.16 Next Steps

In Chapter 5, we’ll discover how to make programs repeat actions with loops. Combined with decisions, this will let you create programs that can handle any number of items, retry on errors, and process data efficiently.

Remember: Decisions aren’t about memorizing if-elif-else syntax. They’re about teaching programs to respond intelligently to different situations - just like we do in real life!