Teaching Karel to repeat actions until a condition becomes false โ building general solutions that work across any world.
Duration
60 Minutes
Level
Introductory CS
Platform
CodeHS.com
Warm-Up
Question of the Day
๐
"You're baking a cake and the recipe says: Mix until smooth.
How many times do you mix?"
You don't know in advance โ you keep mixing until the condition is met.
FOR LOOP THINKING
"Mix exactly 10 times" โ Fixed repetitions, known in advance
WHILE LOOP THINKING
"Mix until smooth" โ Unknown repetitions, condition-based
Learning Objective
Today's Goal
Students will construct while loops in Python-Karel to solve open-ended world problems by evaluating conditions and designing general solutions that function correctly across multiple configurations.
Some problems require one extra action after the loop ends. Always ask: does the last position need an action too?
Student Activities
CodeHS Exercises
1.12.1
While Loops in Karel โ Video Introduction
VIDEO
1.12.2
While Loops in Karel โ Check for Understanding
QUIZ
1.12.3
Move to Wall โ Karel moves until hitting an obstacle
CODE
1.12.4
Follow The Yellow Ball Road โ navigate while beepers remain
CODE
1.12.5
Lay Row of Tennis Balls โ place beepers until wall (fencepost)
CODE
1.12.6
๐ Big Tower โ face north + build upward while loop challenge
CHALLENGE
๐ก TESTING TIP
Test your loop body and condition independently before combining them. Run on at least 2 different world sizes!
Pedagogy
Teaching Considerations
Emphasize the difference: For loops = fixed count. While loops = unknown count based on world state. Repeat this distinction often.
Infinite loops: Show a live infinite loop (then stop it). Make the danger real and memorable before students encounter it.
Fencepost errors: Use the fence analogy โ 3 sections need 4 posts. Some problems need one extra action outside the loop.
General solutions: Always test on multiple world sizes. A program that only works on one world is not a real while loop solution.
Iterative testing: Build the condition first, test it. Then add the body, test again. Don't write the whole loop at once.
Vocabulary: Continuously reinforce: condition, iteration, body, infinite loop, general solution.
while loop
A loop that repeats while a condition remains True
condition
A True/False expression checked before each iteration
general solution
A program that works on many world configurations, not just one
infinite loop
A loop whose condition never becomes False โ runs forever
fencepost bug
When an action must occur one more time than the loop runs
iteration
One complete execution of the loop body
Closure Activity
Hands-On Exit Ticket
ON PAPER OR WHITEBOARD:
1
Trace the Code โ Given a 5-wide world, how many times does this loop run?
whilefront_is_clear(): move()
2
Fix the Bug โ This loop causes an infinite loop. Why?
while notfacing_north(): move()
3
Write Your Own โ Write a while loop to pick up all beepers on a row (use balls_present()).
REFLECTION PROMPTS:
When would you choose a while loop over a for loop? Give an example.
What question should you always ask before writing a while loop? (Will the condition eventually be False?)
In your own words: what makes a solution "general"?
EXIT TICKET
On a sticky note (or Google Form): Write the ONE thing you now understand about while loops that you didn't before today.
Review Game
Kahoot! Unit 1 Review
Last 30 minutes of the 90-min class. Covers Units 1.1 โ 1.12. Sample questions on next slide โธ
UNITS COVERED
1.1 Intro to Karel1.2 More Basic Karel1.3 Can't Turn Right1.4 Functions1.5 Top-Down Design1.6 Commenting1.7 Abstraction1.9 For Loops1.10 If Statements1.11 If/Else1.12 While Loops
GAME FORMAT
~22 questions โ approximately 2 per unit
Multiple choice โ 4 options, 20 seconds each
Mix of: syntax, concept, code-reading, debugging
Encourage discussion after tricky questions
kahoot!
Unit 1 Review ยท Sample Questions
20
QUESTION 1 / 6
What does this code do? while front_is_clear(): move()
โฒ
Moves Karel exactly 5 times
โ
Moves Karel until it hits a wall
โ
Turns Karel left forever
โ
Picks up all beepers
โ Correct: B โ Moves Karel until it hits a wall (front_is_clear() becomes False)