← Back to course
1/6
Week 04 · Loops

Monday

Twenty-four checks a day
// Doing a job many times, and knowing when to stop
⏱ about 20 min

Monday: Twenty-Four Checks a Day

The greenhouse team has a new request for Beacon. They want a water check every hour, all day and all night.

Comet grins and starts writing. Ten minutes later, her page is covered in the same two lines, copied again and again.

"Twenty-four copies," she says, shaking her hand. "Done. Mostly."

Wren scans the page. "What do you notice about lines three, five and seven?"

Comet looks closer. Line seven says waterLevl. She made a typo on copy number four.

Nova hovers over the page, her cyan lights blinking. "Would you like a hint?" she asks.

"What if you wrote the check once, and told Beacon how many times to do it?"

Wren smiles. "Then there would be only one place to fix a mistake."

This week's driving question

How can a short program do a job a thousand times, and know when to stop?

This week you will write loops in pseudocode, trace them by hand and fix one that never stops.

Three building blocks

Every algorithm can be built from three building blocks: sequencing, selection and iteration.

Sequencing means doing each step in the order it is given. You practiced it in week 2.

Selection decides which parts run, based on a true or false condition. That was week 3, with IF and ELSE.

Iteration is a repeating portion of an algorithm. It repeats a set number of times, or until a condition is met.

In everyday talk, programmers call a repeating portion a loop.

Building blockWhat it doesBeacon example
sequencingRuns steps in the order givenRead the sensor, then show the reading
selectionChooses which steps runIF the water is low, show an alert
iterationRepeats stepsCheck the water 24 times a day
NAME THE BUILDING BLOCK
  • Read the question.
  • Tap your answer.
Beacon reads the light sensor, then stores the value, then shows it.
Beacon shows an alert only when the temperature is too low.
Beacon checks the water level again and again until the tank is full.
Beacon takes one photo of each plant, one after another, for twelve plants.

Comet's copy problem

Here are the first two of Comet's twenty-four copies. In our Beacon pseudocode, INPUT() stands for reading a sensor.

waterLevel ← INPUT()
DISPLAY(waterLevel)
waterLevel ← INPUT()
DISPLAY(waterLevel)
... 22 more copies ...
About Comet's copiesTrue or false?
Forty-eight copied lines are easy to check for typos.?
If the team wants a check every half hour, Comet must copy even more lines.?
Iteration could let Comet write the check only once.?
Copying lines by hand is a kind of selection.?
WHY THIS EXERCISERepeated code is slow to write and hard to fix, which is exactly the problem iteration solves.
Which of the three building blocks repeats part of an algorithm? Type one word.
WHY THIS EXERCISEIteration is this week's big idea, and it lets a short program do a long job.
Try it
Look around your home for a job that repeats, like stirring a pot or folding towels.
Write the job as one step plus a rule for how many times to repeat it.

Loops all around you

Loops are not only in programs. Many everyday instructions repeat a step.

Some repeat a set number of times. Others repeat until something happens.

SET NUMBER, OR UNTIL SOMETHING HAPPENS?
  • Read the question.
  • Tap your answer.
Stir the paint 20 times.
Knock on the door until someone answers.
Water the seedlings until the soil is damp.
Do 10 jumping jacks.
PLAN THE HOURLY CHECK BEFORE YOU WRITE IT
  • ?Write the repeating steps once, inside a loop.
  • ?Trace the loop to check that it does what you expect.
  • ?Decide what repeats: read the water sensor and check the level.
  • ?Decide how many times it repeats: 24 times a day.
WHY THIS EXERCISEPlanning what repeats and how often comes before writing any loop.
On paper, sketch a flow chart for the hourly check: one box for the check, and an arrow looping back to it.

Strong start, developer. Tomorrow you will write your first REPEAT loop for Beacon.