← Back to course
Intro to CS 9-10 / Week 04 / Wednesday
3/6
Week 04 · Loops

Wednesday

Beacon Lab: The fill loop that never stops
// Doing a job many times, and knowing when to stop
⏱ about 20 min

Wednesday: Beacon Lab: The Fill Loop That Never Stops

Wren pins a printout to the greenhouse board. It is Beacon's new tank fill routine, and it has a problem.

"In the paper test, the tank should stop filling at 75," he says. "But the trace just keeps going."

Comet frowns. "The loop says stop at 75. I wrote it myself."

Nova hovers beside the printout and dims her lights to lab mode. "Let's debug together," she says.

"Count every pass. Write every value. The loop will tell you its own secret."

Comet hands out pencils. Wren draws the first column of a trace table.

"What do you notice," Wren asks, "about the numbers the tank can reach?"

Your mission

You will hand trace Beacon's fill loop with a counter and find out why it never stops.

Then you will fix the stop condition and trace the fixed loop to prove it works.

Remember: with REPEAT UNTIL, the condition is checked before each pass.

  • Paper and a pencil
  • A ruler for drawing your trace table
  • Ten small counters, such as buttons, beads or paper squares
  • A partner, if you have one, to read the code aloud
waterLevel ← 40
passes ← 0
REPEAT UNTIL (waterLevel = 75)
{
  waterLevel ← waterLevel + 10
  passes ← passes + 1
}
DISPLAY("Tank full")
  1. Draw a trace table with three columns: pass, waterLevel and passes.
  2. Write the starting row: waterLevel is 40 and passes is 0.
  3. Check the condition. Is waterLevel equal to 75? If yes, the loop stops.
  4. If no, run the body once. Put down one counter to mark the pass.
  5. Write the new values of waterLevel and passes in a new row.
  6. Repeat until the loop stops, or until you have used all ten counters.
PasswaterLevelpassesIs waterLevel equal to 75?
Start400no
1501no
2602no
3?3?
4?4?

Copy this table onto paper and keep going past pass 4.

PREDICT, THEN TRACE
  • Read the question.
  • Tap your answer.
Before you trace far, predict: will this loop ever stop?
What is waterLevel after pass 3?
What is waterLevel after pass 4?
What the trace showsTrue or false?
waterLevel always ends in a zero, so it can never equal 75.?
The loop stops after pass 4 because 80 is more than 75.?
This is an infinite loop: its ending condition never becomes true.?
The counter passes would keep growing for as long as the loop runs.?
WHY THIS EXERCISEAn infinite loop happens when the ending condition will never evaluate to true, and a trace makes that visible.

Fix the stop condition

Wren suggests a new condition: stop when waterLevel is at least 75.

Here is the fixed line. Trace the fixed loop from 40 again.

REPEAT UNTIL (waterLevel ≥ 75)
TRACE THE FIX
  • Read the question.
  • Tap your answer.
With the fix, how many passes run before the loop stops?
What is the final waterLevel with the fix?
ONE MORE TEST
  • Read the question.
  • Tap your answer.
The tank already reads 90 when the fixed routine starts. How many times does the body run?
A loop whose ending condition never comes true is an ____ loop.
The fixed loop stops when waterLevel is at ____ 75.
About the fixTrue or false?
Changing equals 75 to at least 75 makes the loop stop.?
The fixed loop still runs forever if the tank starts at 40.?
A fixed loop should be traced again to prove the fix works.?
WHY THIS EXERCISETesting a fix with a trace is how developers make sure they did not trade one bug for another.
On paper, make a bar chart of waterLevel after each pass of the fixed loop: 40, 50, 60, 70, 80. Draw a line at 75.
Try it
Ask a partner to read the broken loop aloud while you trace it with counters.
Stop after six counters. Explain to your partner why you stopped.

Great debugging, developer. Tomorrow you will choose between loops and explain your choice.

← Tuesday