Error: a step placed inside or outside the loop

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Error: a step placed inside or outside the loop

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. total ← 0 REPEAT 5 TIMES total ← total + 2 SHOW total The goal is to show only the final total. What is the problem?
A SHOW is outside the loop, so it prints only once
B SHOW is inside the loop, so it prints on every pass
2. Which line shows a step that runs only ONCE after the loop finishes?
A SHOW total (indented under the loop)
B SHOW total (not indented, below the loop)
3. count ← 0 REPEAT 4 TIMES SHOW "Hi" count ← count + 1 The goal is to count each pass. What is the bug?
A The update is outside the loop, so count only goes up once
B The update is inside the loop, so count goes up too often
4. True or false: indentation helps you see which steps are inside a loop.
A True
B False
5. A student wants a welcome message printed once, but writes it indented inside a loop. What happens?
A The message repeats on every pass of the loop
B The message prints once before the loop starts

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1REPEAT 5 TIMES SET total TO total + 2 SHOW total ASHOW is indented inside the loop, so it prints total on every pass (5 lines) instead of once at the end.
2SET count TO 0 WHILE count < 3 SHOW count SET count TO count + 1 BFOR each mark IN marks SET total TO total + mark SHOW total
3To print the total only once, put SHOW after the loop, lined up with REPEAT and not indented. CThe update SET n TO n + 1 is outside the loop, so n never changes and the loop runs forever. Fix: indent it so it is inside the loop.
4A step that must happen on every pass, like adding to a total, must be indented inside the loop. DThe update is outside the loop because it is not indented, so it never runs inside the loop and the loop never stops.
5SET total TO 0 REPEAT 4 TIMES SET total TO total + 5 SHOW total EREPEAT 5 TIMES SET total TO total + 2 SHOW total
6SET n TO 1 REPEAT UNTIL n > 3 SHOW n SET n TO n + 1 FSHOW is inside the loop, so it prints 5, 10, 15, 20 instead of only the final total of 20. Fix: un-indent SHOW so it is after the loop.

Definitions

loop placement error

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
REPEAT 5 TIMES total ← total + 2 ____ total
2.
REPEAT 4 TIMES _____ ← count + 1 SHOW count
3.
______ 3 TIMES SHOW "Go!" SHOW "Done"
4.
REPEAT _____ count = 3 count ← count + 1 SHOW count
5.
REPEAT 3 TIMES total ← total + 5 SHOW _____

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A REPEAT 4 TIMES
B SET sides TO sides + 1
C TURN RIGHT 90
D SHOW sides
Question 2
A REPEAT 3 TIMES
B SET score TO score + 2
C SHOW score
Question 3
A SET count TO 0
B WHILE count < 3
C SET count TO count + 1
D SHOW count
Question 4
A SHOW "Counting..."
B REPEAT 3 TIMES
C ADD 1 TO count
D SHOW count
Question 5
A REPEAT 3 TIMES
B MOVE 10 STEPS
C SHOW "Step taken"

Answer Key

Error: a step placed inside or outside the loop
Section A: Quick Questions
Q1
B — SHOW is inside the loop, so it prints on every pass
Q2
B — SHOW total (not indented, below the loop)
Q3
A — The update is outside the loop, so count only goes up once
Q4
A — True
Q5
A — The message repeats on every pass of the loop
Section B: Match the Pairs
Q1
REPEAT 5 TIMES SET total TO total + 2 SHOW total → SHOW is indented inside the loop, so it prints total on every pass (5 lines) instead of once at the end.
Q2
SET count TO 0 WHILE count < 3 SHOW count SET count TO count + 1 → The update is outside the loop because it is not indented, so it never runs inside the loop and the loop never stops.
Q3
To print the total only once, put SHOW after the loop, lined up with REPEAT and not indented. → REPEAT 5 TIMES SET total TO total + 2 SHOW total
Q4
A step that must happen on every pass, like adding to a total, must be indented inside the loop. → FOR each mark IN marks SET total TO total + mark SHOW total
Q5
SET total TO 0 REPEAT 4 TIMES SET total TO total + 5 SHOW total → SHOW is inside the loop, so it prints 5, 10, 15, 20 instead of only the final total of 20. Fix: un-indent SHOW so it is after the loop.
Q6
SET n TO 1 REPEAT UNTIL n > 3 SHOW n SET n TO n + 1 → The update SET n TO n + 1 is outside the loop, so n never changes and the loop runs forever. Fix: indent it so it is inside the loop.
Section C: Definitions
Q1
loop placement error — A bug where a step sits in the wrong place around a loop. A SHOW inside repeats every pass, and an update outside runs only once. Indentation shows which steps are inside the loop.
Section D: Fill in the Blanks
Q1
REPEAT 5 TIMES total ← total + 2 SHOW total
Q2
REPEAT 4 TIMES count ← count + 1 SHOW count
Q3
REPEAT 3 TIMES SHOW "Go!" SHOW "Done"
Q4
REPEAT UNTIL count = 3 count ← count + 1 SHOW count
Q5
REPEAT 3 TIMES total ← total + 5 SHOW total
Section E: Spot the Error
Q1
B — The update is not indented, so it sits outside the loop and runs only once. It should be ' ADD 1 TO sides', indented inside the loop, so sides grows on every pass.
Q2
B — The score update is at the left margin, so it is outside the loop and runs only once. It should be ' ADD 2 TO score', indented inside the loop, so it repeats on every pass.
Q3
C — The update is not indented, so it is outside the loop. count never changes inside the WHILE loop, so the loop never stops. It should be ' ADD 1 TO count', indented inside the loop.
Q4
A — This message is indented as if it belonged to a loop. A one-off opening message should sit at the left margin, before the loop, so it is shown once. It should be 'SHOW "Counting starts"' with no indent.
Q5
C — The indent puts SHOW inside the loop, so it prints on every pass. The arrival message should appear once, after the loop. It should be 'SHOW "Arrived"' with no indent.