← Back to course
2/6
Week 04 · Loops

Tuesday

REPEAT n TIMES
// Doing a job many times, and knowing when to stop
⏱ about 20 min

Tuesday: REPEAT n TIMES

Comet tears up her twenty-four copies and starts fresh. This time the page has only a few lines.

"REPEAT 24 TIMES," she reads aloud. "Then the check goes inside the braces."

Wren leans in. "How will you know it really runs twenty-four times, and not twenty-three?"

Comet opens her mouth, then closes it again. She is not sure.

Nova projects a small grid in the air. "I found a pattern," she says.

"When you are not sure what a loop does, follow it one pass at a time and write down every value."

Wren grabs a pencil. "A trace table," he says. "Let's investigate."

Repeating a set number of times

A REPEAT n TIMES loop runs the statements inside its braces n times. Those statements are the loop body.

Here is Beacon's hourly water check, written once instead of twenty-four times.

REPEAT 24 TIMES
{
  waterLevel ← INPUT()
  IF (waterLevel < 30)
  {
    DISPLAY("The tomatoes need water")
  }
}

Notice that the loop body holds an IF statement. Iteration and selection can work together.

Each pass reads a new value, so waterLevel gets the most recent reading each hour.

READ THE HOURLY CHECK
  • Read the question.
  • Tap your answer.
How many times does the loop body run in one day?
Which statement is inside the loop body?
Hour 5 reads 25. What does Beacon show that hour?
Hour 6 reads 45. What does Beacon show that hour?

Hand tracing with a trace table

Hand tracing means following a program by hand and writing down each value as it changes.

It is one of the most useful ways to find and correct errors.

A trace table has a column for each variable and a row for each step. Trace this short loop.

total ← 0
REPEAT 3 TIMES
{
  total ← total + 5
}
DISPLAY(total)
StepWhat happenstotal
Starttotal gets 00
Pass 1total gets 0 + 55
Pass 2total gets 5 + 510
Pass 3total gets 10 + 5?
FINISH THE TRACE
  • Read the question.
  • Tap your answer.
What is total after pass 3?
What does the program display?
If the loop said REPEAT 4 TIMES instead, what would it display?
PUT THE STEPS OF A HAND TRACE IN ORDER
  • ?Follow the loop body one pass at a time.
  • ?Write each starting value in the first row.
  • ?Draw a column for each variable.
  • ?Read the final values and compare them with what you expected.
  • ?Write the new value of each variable after every pass.
WHY THIS EXERCISEA steady trace order means you never skip a pass, which is where loop mistakes hide.
The statements inside a loop's braces are the loop ____.
Following a program by hand and writing each value is hand ____.

Change the count, change the result

plantsChecked ← 0
REPEAT 6 TIMES
{
  plantsChecked ← plantsChecked + 1
}
DISPLAY(plantsChecked)
TRACE A COUNTER
  • Read the question.
  • Tap your answer.
What does this program display?
What if the first line were plantsChecked gets 10 instead?
What if the DISPLAY line moved inside the braces?
StatementTrue or false?
A statement after the closing brace runs once, after the loop ends.?
A statement inside the loop body runs once for every pass.?
REPEAT 6 TIMES runs its body seven times.?
A trace table needs a column for each variable.?
WHY THIS EXERCISEKnowing what is inside the body is the key to predicting what a loop does.
Try it
Write a REPEAT 5 TIMES loop on paper that adds 3 to a variable called score, starting from 0.
Trace it in a table, then check: the final value should be 15.

Nice tracing. Tomorrow in Beacon Lab you will hunt down a loop that never stops.

← Monday