Conditional Logic and the Selection Control Structure
Learning Objectives: By the end of this lesson, you will be able to:
- Use IF/ELSE statements in pseudocode to represent decision-making
- Apply comparison and logical operators to control program flow
- Write clear, structured pseudocode for selection-based problems
1. Welcome and Introduction (5 minutes)
Icebreaker Activity
Have you ever had to make a decision based on a condition, like “If it rains, take an umbrella; otherwise, wear sunglasses”? How do you decide what to do when faced with choices?
Lesson Overview
- Duration: 90 minutes
- Prerequisites: Day 1 and Day 2 lessons (algorithms, pseudocode basics, IPO pattern)
- Materials Needed: Pen and paper or a text editor for writing pseudocode
2. Learning Objectives and Session Outline (5 minutes)
What We’ll Cover Today:
- IF/ELSE statements and selection logic
- Comparison and logical operators
- Writing pseudocode for real-world decisions
- Hands-on practice and exercises
- Common pitfalls and troubleshooting
Success Criteria:
By the end of this session, you should be able to write pseudocode that makes decisions using IF/ELSE and handles different outcomes.
3. Foundation Knowledge (15-20 minutes)
Core Concepts
IF/ELSE Statements
- IF…THEN…ENDIF: Executes a block of code if a condition is true
- IF…THEN…ELSE…ENDIF: Chooses between two actions based on a condition
- ELSE IF: Handles multiple conditions in sequence
Real-World Analogy: Think of a traffic light: “If the light is red, stop. Else if it’s yellow, prepare to stop. Else (it’s green), go.”
Why This Matters: Decision-making is at the heart of programming. Programs need to adapt to different situations based on input or changing conditions.
Comparison and Logical Operators
- Comparison:
==,!=,>,<,>=,<= - Logical:
AND,OR,NOT
Example:
IF age >= 13 THEN(checks if someone is old enough)IF (score > 80) AND (attendance == 100)(checks two conditions)
Key Terminology
- Condition: A statement that can be true or false
- Branch: A possible path the program can take
- Selection: Choosing which branch to follow based on a condition
4. Detailed Explanation and Examples (25-30 minutes)
Understanding Selection in Pseudocode
Selection allows your algorithm to choose different actions based on conditions. This is essential for making programs flexible and responsive.
Practical Applications
- Traffic lights
- User authentication (login success/failure)
- Grading systems (pass/fail)
Common Misconceptions
- Misconception: IF/ELSE is only for complex programs. Reality: Even simple tasks often require decisions.
- Misconception: You can skip ELSE if you only care about one outcome. Reality: Always consider what should happen if the condition is false.
5. Live Coding Demonstration (20-25 minutes)
Example 1: Traffic Light Logic
START
INPUT lightColor
IF lightColor == "Red" THEN
OUTPUT "Cars stop."
ELSE IF lightColor == "Yellow" THEN
OUTPUT "Cars prepare to stop."
ELSE IF lightColor == "Green" THEN
OUTPUT "Cars go."
ELSE
OUTPUT "Invalid color."
ENDIF
END
Explanation:
- Each IF/ELSE IF checks a different possible value
- ELSE handles unexpected input
Example 2: Even or Odd Number
START
INPUT number
IF number % 2 == 0 THEN
OUTPUT "Even"
ELSE
OUTPUT "Odd"
ENDIF
END
Key Points:
%is the modulus operator (remainder after division)- Only one branch is executed
Example 3: Age Check for Ticket Purchase
START
INPUT age
IF age >= 13 THEN
OUTPUT "You can buy a ticket."
ELSE
OUTPUT "You are too young to buy a ticket."
ENDIF
END
Debugging Demonstration
Common mistake: Forgetting to handle all possible cases.
// Incorrect
START
INPUT lightColor
IF lightColor == "Red" THEN
OUTPUT "Cars stop."
ENDIF
END
// Corrected version
START
INPUT lightColor
IF lightColor == "Red" THEN
OUTPUT "Cars stop."
ELSE IF lightColor == "Yellow" THEN
OUTPUT "Cars prepare to stop."
ELSE IF lightColor == "Green" THEN
OUTPUT "Cars go."
ELSE
OUTPUT "Invalid color."
ENDIF
END
6. Hands-On Practice (30-40 minutes)
Exercise 1: Basic Practice (10 minutes)
Objective: Write pseudocode to check if a number is positive, negative, or zero.
Instructions:
- Input a number
- Use IF/ELSE IF/ELSE to check the value
- Output the result
Starter Code:
START
// Your steps here
END
Solution:
START
INPUT number
IF number > 0 THEN
OUTPUT "Positive"
ELSE IF number < 0 THEN
OUTPUT "Negative"
ELSE
OUTPUT "Zero"
ENDIF
END
Exercise 2: Intermediate Challenge (15 minutes)
Objective: Write pseudocode to check if a user can access a website (must be at least 18 years old and have an account).
Requirements:
- Input age and hasAccount (true/false)
- Use AND operator
- Output access granted or denied
Hints:
IF (age >= 18) AND (hasAccount == TRUE)- Remember to handle all cases
Exercise 3: Creative Application (15 minutes)
Objective: Write pseudocode for a simple grading system.
Challenge:
- Input a student’s score
- Output “A” for 90+, “B” for 80-89, “C” for 70-79, “D” for 60-69, “F” for below 60
Extension Ideas:
- Add a congratulatory message for “A”
- Handle invalid scores (e.g., negative numbers, scores above 100)
7. Common Pitfalls and Troubleshooting (10 minutes)
Frequent Mistakes
- Problem: Not covering all possible conditions Cause: Forgetting ELSE or ELSE IF branches Solution: Always consider every possible input
- Problem: Using assignment (=) instead of comparison (==) Cause: Mixing up operators Solution: Use == for comparison, = for assignment
Debugging Tips
- Test your pseudocode with different inputs
- Check that every possible case is handled
- Read your logic aloud to spot errors
8. Best Practices and Professional Tips (5 minutes)
Industry Standards
- Use clear, consistent IF/ELSE structures
- Handle unexpected or invalid input
- Keep conditions simple and readable
Next Steps in Your Learning Journey
- Practice writing selection logic for real-world scenarios
- Learn about nested IF statements and switch/case structures
- Explore how selection is implemented in programming languages
9. Summary and Review (5 minutes)
Key Takeaways
- IF/ELSE allows programs to make decisions
- Comparison and logical operators control program flow
- Always handle all possible cases in your logic
Quick Review Questions
- What is the purpose of an IF/ELSE statement?
- How do you check if two values are equal in pseudocode?
- Why is it important to handle all possible input cases?
For Instructors
Teaching Tips
- Use relatable, real-world decision scenarios
- Encourage students to test their pseudocode with edge cases
- Discuss the difference between assignment and comparison
Assessment Ideas
- Have students write pseudocode for a new selection problem
- Peer review: students check each other’s logic for completeness
- Short quiz on IF/ELSE and operators
Adaptation Notes
- For advanced students, introduce nested IFs or logical combinations
- For online delivery, use interactive quizzes or branching scenarios
- Adjust timing based on student engagement and questions