Using a variable as a counter in a loop

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

Using a variable as a counter in a loop

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. A student wants to count how many times a loop repeats. Which line goes inside the loop?
A counter ← counter + 1
B counter ← 0
2. Where should you set a counter to 0?
A Before the loop starts
B Inside the loop at the end
3. A counter starts at 0. The loop repeats 4 times and adds 1 each time. What is the counter after the loop?
A 5
B 4
4. What does a counter variable do in a loop?
A It keeps track of how many times something has happened
B It makes the loop run faster
5. Which loop stops when the counter reaches 10?
A REPEAT counter TIMES UNTIL 0
B REPEAT UNTIL counter = 10

Match the Pairs

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

Left columnRight column
1counter ← 0 AAdds 1 to the counter each time the loop repeats, so it counts how many times something happened
2counter ← counter + 1 BSets the counter to 0 before the loop starts so counting begins from zero
3Stop the loop when the counter reaches 5 CREPEAT UNTIL counter = 5
4Start counting from zero before the loop begins DThe counter is set to 0 inside the loop, so it never goes up and the loop never stops. It should be counter ← counter + 1.
5REPEAT UNTIL counter = 4 counter ← 0 EThe counter was never set to 0 before the loop. Add counter ← 0 before the loop so it starts from a known number.
6REPEAT UNTIL counter = 3 counter ← counter + 1 Fcounter ← 0 REPEAT UNTIL counter = 3 counter ← counter + 1

Definitions

counter ← counter + 1

Fill in the Blanks

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

1.
counter ← _ REPEAT UNTIL counter = 5 counter ← counter + 1
2.
claps ← 0 REPEAT UNTIL claps = 10 claps ← claps _ 1
3.
counter ← 0 REPEAT UNTIL counter _ 5 counter ← counter + 1
4.
jumps ← 0 REPEAT _____ jumps = 8 jumps ← jumps + 1
5.
counter ← 0 REPEAT UNTIL counter = 5 counter ← counter + _

Spot the Error

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

Question 1
A counter ← 0
B REPEAT UNTIL counter = 0
C SAY 'Hello'
D counter ← counter + 1
E END REPEAT
Question 2
A counter ← 0
B REPEAT UNTIL counter = 3
C counter ← counter - 1
D SAY counter
E END REPEAT
Question 3
A counter ← 0
B REPEAT 10 TIMES
C counter ← 1
D END REPEAT
E SAY counter
Question 4
A WHEN green flag clicked
B counter ← 5
C REPEAT 5 TIMES
D counter ← counter + 1
E SAY counter
Question 5
A counter ← 0
B REPEAT UNTIL counter = 4
C SAY 'Jump!'
D counter = counter + 1
E END REPEAT

Answer Key

Using a variable as a counter in a loop
Section A: Quick Questions
Q1
A — counter ← counter + 1
Q2
A — Before the loop starts
Q3
B — 4
Q4
A — It keeps track of how many times something has happened
Q5
B — REPEAT UNTIL counter = 10
Section B: Match the Pairs
Q1
counter ← 0 → Sets the counter to 0 before the loop starts so counting begins from zero
Q2
counter ← counter + 1 → Adds 1 to the counter each time the loop repeats, so it counts how many times something happened
Q3
Stop the loop when the counter reaches 5 → REPEAT UNTIL counter = 5
Q4
Start counting from zero before the loop begins → counter ← 0 REPEAT UNTIL counter = 3 counter ← counter + 1
Q5
REPEAT UNTIL counter = 4 counter ← 0 → The counter is set to 0 inside the loop, so it never goes up and the loop never stops. It should be counter ← counter + 1.
Q6
REPEAT UNTIL counter = 3 counter ← counter + 1 → The counter was never set to 0 before the loop. Add counter ← 0 before the loop so it starts from a known number.
Section C: Definitions
Q1
counter ← counter + 1 — A counter is a variable that goes up by 1 each time a loop repeats, so it counts how many times something happened. Set it to 0 before the loop starts.
Section D: Fill in the Blanks
Q1
counter ← 0 REPEAT UNTIL counter = 5 counter ← counter + 1
Q2
claps ← 0 REPEAT UNTIL claps = 10 claps ← claps + 1
Q3
counter ← 0 REPEAT UNTIL counter = 5 counter ← counter + 1
Q4
jumps ← 0 REPEAT UNTIL jumps = 8 jumps ← jumps + 1
Q5
counter ← 0 REPEAT UNTIL counter = 5 counter ← counter + 1
Section E: Spot the Error
Q1
B — The counter starts at 0, so 'counter = 0' is already true and the loop never runs. The loop should stop when the counter reaches the target number, so it should be REPEAT UNTIL counter = 5.
Q2
C — Subtracting 1 makes the counter go down (-1, -2, ...), so it never reaches 3 and the loop never stops. A counter goes up by 1 each time, so it should be counter ← counter + 1.
Q3
C — Setting the counter to 1 inside the loop wipes out the count every time, so it stays at 1. It should add 1 to the old value: counter ← counter + 1.
Q4
B — A counter must be reset to 0 before the loop starts. Starting at 5 would make the final count 10, not 5. It should be counter ← 0.
Q5
D — The = sign compares two values; it does not change the variable. The counter never goes up, so the loop never ends. Use the assignment arrow: counter ← counter + 1.