Lesson 7 • Intermediate

Conditional Statements

25 minutes
Control Flow

🔀 Making Decisions in Code

Conditional statements allow your programs to make decisions and execute different code based on conditions. They're like crossroads in your program where it chooses which path to take.

Real-World Analogy
Think of everyday decisions:
  • IF it's raining, THEN take an umbrella
  • IF you have money, buy lunch, ELSE bring lunch from home
  • IF grade >= 90, it's an A, ELSE IF grade >= 80, it's a B
Conditional statements work the same way in programming!

📋 IF Statement

The basic IF statement executes code only if a condition is true:

If condition Then
    # Code executes only if condition is true
Endif
Example: Simple IF
Algorithm SimpleIF

var age = Input "Enter your age:"

If age >= 18 Then
    Print "You are an adult!"
    Print "You can vote."
Endif

Print "Program complete."

Endalgorithm
Example Output (age = 20)
Enter your age: 20 You are an adult! You can vote. Program complete.
Example Output (age = 15)
Enter your age: 15 Program complete.

Visual: IF Statement Flow

age >= 18 ?
✓ TRUE
Execute:
Print "Adult"
Print "Can vote"
✗ FALSE
Skip block
(Continue to next)
Continue Program

🔄 IF-ELSE Statement

IF-ELSE provides an alternative path when the condition is false:

If condition Then
    # Executes if condition is true
Else
    # Executes if condition is false
Endif
Example: IF-ELSE
Algorithm PassOrFail

var score = Input "Enter your test score:"

If score >= 60 Then
    Print "✅ PASS"
    Print "Congratulations!"
Else
    Print "❌ FAIL"
    Print "Study more and try again."
Endif

Endalgorithm

ELSE-IF Chains

Check multiple conditions in sequence:

If condition1 Then
    # Executes if condition1 is true
Else If condition2 Then
    # Executes if condition2 is true (and condition1 was false)
Else If condition3 Then
    # Executes if condition3 is true (and previous conditions were false)
Else
    # Executes if all conditions are false
Endif
Example: Grade Calculator
Algorithm GradeCalculator

var score = Input "Enter your score (0-100):"

Print ""
Print "Your score:", score

If score >= 90 Then
    Print "Grade: A - Excellent!"
Else If score >= 80 Then
    Print "Grade: B - Good job!"
Else If score >= 70 Then
    Print "Grade: C - Satisfactory"
Else If score >= 60 Then
    Print "Grade: D - Needs improvement"
Else
    Print "Grade: F - Failed"
Endif

Endalgorithm

🔗 Multiple Conditions

Combine conditions using and, or, and not:

Using AND

Both conditions must be true:

Example: AND Operator
Algorithm CanDrive

var age = Input "Enter your age:"
var hasLicense = Input "Do you have a license? (true/false):"

If age >= 18 and hasLicense == true Then
    Print "✅ You can drive legally!"
Else
    Print "❌ You cannot drive yet."
    
    If age < 18 Then
        Print "Reason: Too young"
    Endif
    
    If hasLicense == false Then
        Print "Reason: No license"
    Endif
Endif

Endalgorithm

Using OR

At least one condition must be true:

Example: OR Operator
Algorithm TicketDiscount

var age = Input "Enter your age:"

If age < 12 or age >= 65 Then
    Print "🎫 You qualify for a discount!"
    Print "Ticket price: $5"
Else
    Print "Regular ticket price: $12"
Endif

Endalgorithm

🏗️ Nested IF Statements

IF statements inside other IF statements for complex logic:

Example: Nested Conditions
Algorithm MovieRating

var age = Input "Enter your age:"
var hasParent = Input "Is a parent with you? (true/false):"

Print ""
Print "=== MOVIE RATINGS ==="

If age >= 18 Then
    Print "✅ You can watch:"
    Print "- G rated movies"
    Print "- PG rated movies"
    Print "- PG-13 rated movies"
    Print "- R rated movies"
Else If age >= 13 Then
    Print "✅ You can watch:"
    Print "- G rated movies"
    Print "- PG rated movies"
    Print "- PG-13 rated movies"
    
    If hasParent == true Then
        Print "- R rated movies (with parent)"
    Endif
Else
    Print "✅ You can watch:"
    Print "- G rated movies"
    
    If hasParent == true Then
        Print "- PG rated movies (with parent)"
    Endif
Endif

Endalgorithm

Real-World Examples

Example: Shopping Discount Calculator
Algorithm ShoppingDiscount

Print "=== DISCOUNT CALCULATOR ==="
Print ""

var totalAmount = Input "Enter purchase amount:"
var isMember = Input "Are you a member? (true/false):"

var discount = 0
var discountPercent = 0

# Determine discount based on amount and membership
If totalAmount >= 100 Then
    If isMember == true Then
        discountPercent = 20  # 20% for members on large purchases
    Else
        discountPercent = 10  # 10% for non-members
    Endif
Else If totalAmount >= 50 Then
    If isMember == true Then
        discountPercent = 10  # 10% for members
    Else
        discountPercent = 5   # 5% for non-members
    Endif
Else
    If isMember == true Then
        discountPercent = 5   # 5% for members only
    Endif
Endif

# Calculate final price
discount = totalAmount * (discountPercent / 100)
var finalAmount = totalAmount - discount

# Display results
Print ""
Print "=== RECEIPT ==="
Print "Original amount: $", totalAmount
Print "Discount (", discountPercent, "%): $", discount
Print "---------------"
Print "Final amount: $", finalAmount
Print "You saved: $", discount

Endalgorithm
Example: Weather Advisor
Algorithm WeatherAdvisor

var temperature = Input "Enter temperature (°C):"
var isRaining = Input "Is it raining? (true/false):"

Print ""
Print "=== WEATHER ADVICE ==="

# Clothing advice based on temperature
If temperature < 0 Then
    Print "🧥 Wear: Heavy winter coat, gloves, hat"
Else If temperature < 10 Then
    Print "🧥 Wear: Warm jacket"
Else If temperature < 20 Then
    Print "👕 Wear: Light jacket or sweater"
Else If temperature < 30 Then
    Print "👕 Wear: T-shirt"
Else
    Print "🩳 Wear: Light clothing, stay hydrated!"
Endif

# Rain advice
If isRaining == true Then
    Print "☔ Don't forget: Umbrella and rain jacket!"
Endif

Endalgorithm

Common Mistakes

Mistake 1: Forgetting Endif

❌ Wrong:

If age >= 18 Then
    Print "Adult"
# Missing Endif!

✅ Correct:

If age >= 18 Then
    Print "Adult"
Endif
Mistake 2: Using = Instead of ==

❌ Wrong (assignment):

If age = 18 Then  # This assigns, doesn't compare!

✅ Correct (comparison):

If age == 18 Then  # This compares

Key Takeaways

  • ✅ IF statements let your program make decisions
  • ✅ Use If...Then...Endif for single conditions
  • ✅ Use Else to provide an alternative path
  • ✅ Use Else If to check multiple conditions
  • ✅ Combine conditions with and, or, not
  • ✅ Nest IF statements for complex logic
  • ✅ Always close with Endif
  • ✅ Use == for comparison, = for assignment

💪 Practice Exercises

Try These Exercises

Exercise 1: BMI Category
Calculate BMI and categorize it: Underweight (<18.5), Normal (18.5-24.9), Overweight (25-29.9), Obese (>=30).
Exercise 2: Password Validator
Check if a password is valid: length >= 8 AND contains numbers. Provide specific feedback for each requirement.
Exercise 3: Tax Calculator
Calculate income tax with brackets: 0-10k (0%), 10k-30k (10%), 30k-70k (20%), 70k+ (30%). Use nested conditions.
Exercise 4: Game Character Stats
Based on health and energy levels, determine character status (Critical, Tired, Ready, Excellent) and suggest actions.
Great Progress!
You now understand how to make decisions in your programs! In the next lesson, we'll learn about loops to repeat actions multiple times.