← Back to course
Intro to CS 9-10 / Week 04 / Thursday
4/6
Week 04 · Loops

Thursday

Choosing the right loop
// Doing a job many times, and knowing when to stop
⏱ about 20 min

Thursday: Choosing the Right Loop

The greenhouse team sends two more jobs for Beacon.

The first: take a photo of the herb shelf every hour for a whole day.

The second: tell Lena to keep the misting fan on until the air is damp enough.

Comet reaches for REPEAT 24 TIMES for both. "It worked last time," she says.

Wren tilts his head. "How many times will the fan need to run?"

Comet pauses. "It depends on how dry the air is. Nobody knows ahead of time how many checks it needs."

Nova blinks thoughtfully. "Interesting," she says. "Then maybe the second job needs a different kind of loop."

"Two jobs, two tools," Wren says. "Let's compare them."

REPEAT UNTIL

A REPEAT UNTIL loop runs its body until a condition becomes true.

The condition is checked before each pass. If it is already true at the start, the body never runs at all.

If the condition can never become true, the loop never ends. That is an infinite loop.

humidity ← INPUT()
REPEAT UNTIL (humidity ≥ 60)
{
  DISPLAY("Keep misting the herbs")
  humidity ← INPUT()
}

Look at the last line of the body. It reads the sensor again, so humidity can change each pass.

Without that line, humidity would never change, and a dry start would mean an infinite loop.

LoopUse it whenBeacon job
REPEAT n TIMESYou know how many passes you needA photo every hour for 24 hours
REPEAT UNTILYou stop when a condition becomes trueMist until the air is damp enough
WHICH LOOP FITS THE JOB?
  • Read the question.
  • Tap your answer.
Water each of the 8 bean pots once.
Run the heater until the temperature reaches 20.
Show the word Hello on the screen 3 times.
Keep asking for a reading until the sensor sends a number from 0 to 100.

Similar loops, different results

Algorithms can be written in different ways and still do the same task.

But algorithms that look similar can also give different results. Compare these two loops.

Loop A                         Loop B
level ← 50                     level ← 50
REPEAT UNTIL (level > 70)      REPEAT UNTIL (level ≥ 70)
{                              {
  level ← level + 10             level ← level + 10
}                              }
TRACE BOTH LOOPS
  • Read the question.
  • Tap your answer.
What is level when Loop A ends?
What is level when Loop B ends?
How many passes does Loop A run?

Justify your choice

Good developers explain why they chose a control structure, not just which one.

Readability is how clear a program is to other programmers. Documentation can improve it.

One loop of eight lines is easier to read and fix than forty-eight copied lines.

StatementTrue or false?
If a REPEAT UNTIL condition is true at the start, the body runs once anyway.?
A REPEAT UNTIL loop whose condition never changes can become an infinite loop.?
Two loops that look almost the same can end with different values.?
A loop is usually easier to read than many copied lines.?
WHY THIS EXERCISEKnowing how each loop behaves is what lets you justify the one you choose.
Comet must mist the herbs until the air is damp enough. Which loop should she use? Type its name.
WHY THIS EXERCISEChoosing the control structure that matches the job makes the program correct and easier to read.
HOW A REPEAT UNTIL LOOP RUNS
  • ?Check the condition.
  • ?If the condition is true, skip the body and move on.
  • ?If the condition is false, run the body once.
  • ?Go back and check the condition again.
WHY THIS EXERCISEThe condition is checked before each pass, which is why the body may never run.
SPOT THE INFINITE LOOP
  • Read the question.
  • Tap your answer.
A loop starts with count at 1, adds 2 each pass, and stops when count equals 8. Will it stop?
A misting loop forgets to read the sensor inside its body. The air starts dry. What happens?
On paper, sketch the misting loop as a flow chart. Show the condition check as a diamond with yes and no arrows.
Try it
Find a set of instructions at home, like a recipe card or a game rule.
Mark each step that repeats. Is it a set number of times, or until something happens?

Sharp thinking. Tomorrow is Impact Friday: what happens when a loop reaches thousands of people?

← Wednesday