Repetition and the Iteration Control Structure
Learning Objectives: By the end of this lesson, you will be able to:
- Use WHILE, FOR, and REPEAT loops in pseudocode to represent repetition
- Identify and use loop control variables
- Write clear, structured pseudocode for iterative problems
1. Welcome and Introduction (5 minutes)
Icebreaker Activity
Have you ever had to do something over and over again, like counting steps, practicing a skill, or searching for a friend in a crowd? How do you keep track of what you’ve already done?
Lesson Overview
- Duration: 90 minutes
- Prerequisites: Day 1–3 lessons (algorithms, pseudocode basics, selection logic)
- 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:
- Loop structures: WHILE, FOR, REPEAT
- Loop control variables and avoiding infinite loops
- Writing pseudocode for repetitive tasks
- 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 repeats actions using loops and manages loop variables correctly.
3. Foundation Knowledge (15-20 minutes)
Core Concepts
Loop Structures in Pseudocode
- WHILE…DO…ENDWHILE: Repeat as long as a condition is true
- FOR…TO…DO…ENDFOR: Repeat a set number of times
- REPEAT…UNTIL: Repeat until a condition becomes true (always runs at least once)
Real-World Analogy: Think of practicing a song until you get it right (REPEAT…UNTIL), or counting out 10 push-ups (FOR loop).
Why This Matters: Loops are essential for processing lists, automating repetitive tasks, and handling large amounts of data efficiently.
Loop Control Variables
A loop control variable keeps track of the number of repetitions or the current position in a list. For example, a counter in a FOR loop.
Avoiding Infinite Loops
An infinite loop happens when the loop’s condition never becomes false. Always make sure your loop will eventually stop!
Key Terminology
- Iteration: Repeating a set of instructions
- Loop: A structure that enables iteration
- Control Variable: A variable that determines when the loop stops
4. Detailed Explanation and Examples (25-30 minutes)
Understanding Iteration in Pseudocode
Iteration allows you to automate repetitive tasks, making your algorithms more powerful and efficient.
Practical Applications
- Searching for an item in a list
- Summing numbers
- Printing messages multiple times
Common Misconceptions
- Misconception: Loops are only for advanced problems. Reality: Many simple tasks require repetition.
- Misconception: You don’t need to update the control variable. Reality: Failing to update the control variable causes infinite loops.
5. Live Coding Demonstration (20-25 minutes)
Example 1: Searching for a Name in a List (FOR Loop)
START
INPUT nameToFind
SET found TO FALSE
FOR i FROM 0 TO LENGTH(namesList) - 1 DO
IF namesList[i] == nameToFind THEN
SET found TO TRUE
OUTPUT "Found!"
BREAK
ENDIF
ENDFOR
IF found == FALSE THEN
OUTPUT "Not found."
ENDIF
END
Explanation:
- The FOR loop goes through each name
- The control variable
itracks the current position - The loop stops early if the name is found
Example 2: Print “Hello” 5 Times (FOR Loop)
START
FOR count FROM 1 TO 5 DO
OUTPUT "Hello"
ENDFOR
END
Key Points:
- FOR loops are ideal for a known number of repetitions
Example 3: Sum Numbers from 1 to 100 (WHILE Loop)
START
SET sum TO 0
SET number TO 1
WHILE number <= 100 DO
SET sum TO sum + number
SET number TO number + 1
ENDWHILE
OUTPUT sum
END
Example 4: REPEAT…UNTIL Loop
START
SET guess TO 0
REPEAT
INPUT guess
UNTIL guess == 7
OUTPUT "You guessed the secret number!"
END
Debugging Demonstration
Common mistake: Forgetting to update the control variable.
// Incorrect
START
SET number TO 1
WHILE number <= 5 DO
OUTPUT number
ENDWHILE
END
// Corrected version
START
SET number TO 1
WHILE number <= 5 DO
OUTPUT number
SET number TO number + 1
ENDWHILE
END
6. Hands-On Practice (30-40 minutes)
Exercise 1: Basic Practice (10 minutes)
Objective: Write pseudocode to print numbers 1 to 10.
Instructions:
- Use a FOR or WHILE loop
- Output each number
Starter Code:
START
// Your steps here
END
Solution:
START
FOR i FROM 1 TO 10 DO
OUTPUT i
ENDFOR
END
Exercise 2: Intermediate Challenge (15 minutes)
Objective: Write pseudocode to find the sum of all even numbers from 1 to 50.
Requirements:
- Use a loop
- Use an IF statement to check for even numbers
- Output the final sum
Hints:
- Use
number % 2 == 0to check for even - Initialize sum to 0
Exercise 3: Creative Application (15 minutes)
Objective: Write pseudocode for a simple password guessing game.
Challenge:
- Allow the user to keep guessing until they enter the correct password
- Output a message for each incorrect guess
- Output a success message when correct
Extension Ideas:
- Limit the number of attempts
- Give hints after each guess
7. Common Pitfalls and Troubleshooting (10 minutes)
Frequent Mistakes
- Problem: Infinite loops Cause: Not updating the control variable or incorrect loop condition Solution: Always update the control variable and check your condition
- Problem: Off-by-one errors Cause: Loop starts or ends at the wrong value Solution: Double-check your loop boundaries
Debugging Tips
- Trace your loop with sample values
- Print the control variable each iteration to check progress
- Make sure your loop will eventually stop
8. Best Practices and Professional Tips (5 minutes)
Industry Standards
- Use clear, consistent loop structures
- Name control variables meaningfully
- Avoid deeply nested loops when possible
Next Steps in Your Learning Journey
- Practice writing loops for different scenarios
- Learn about nested loops and more complex patterns
- Explore how loops are implemented in programming languages
9. Summary and Review (5 minutes)
Key Takeaways
- Loops automate repetitive tasks in algorithms
- Control variables are essential for managing loops
- Always ensure your loops will stop (no infinite loops)
Quick Review Questions
- What is the difference between a FOR and a WHILE loop?
- How do you avoid infinite loops?
- Why are control variables important?
For Instructors
Teaching Tips
- Use real-world repetition examples (e.g., counting, searching)
- Encourage students to trace loops by hand
- Discuss common loop errors and how to fix them
Assessment Ideas
- Have students write pseudocode for a new repetitive task
- Peer review: students check each other’s loops for correctness
- Short quiz on loop types and control variables
Adaptation Notes
- For advanced students, introduce nested loops or early exits (BREAK)
- For online delivery, use interactive loop tracing tools
- Adjust timing based on student engagement and questions