Counters and accumulators in pseudocode

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

Counters and accumulators in pseudocode

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which line sets up a counter correctly BEFORE a loop starts?
A count <- 0
B count <- count + 1
2. Which line adds 1 to a counter each time the loop repeats?
A count <- count + 1
B count <- 1
3. An accumulator keeps a running total of marks. Which line does this inside the loop?
A total <- mark
B total <- total + mark
4. True or false: you should set total <- 0 inside the loop so it starts fresh on every repeat.
A False: set total <- 0 before the loop
B True: set total <- 0 inside the loop
5. You want to count how many scores are above 50. Which code does this?
A count <- count + 1 then if score > 50
B if score > 50 then count <- count + 1

Match the Pairs

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

Left columnRight column
1count <- count + 1 AAn accumulator: adds the value of score to a running total each time the loop repeats
2total <- total + score BThe accumulator adds 1 instead of the value being collected, so it only counts repeats. It should be total <- total + x.
3Set the counter to 0 before the loop so it starts from nothing Cfor i from 1 to 5 if mark >= 50 then passes <- passes + 1 end if end for
4Count only the matches: add 1 only when a condition is true Dcount <- 0 for i from 1 to 10 count <- count + 1 end for
5for i from 1 to 10 count <- 0 count <- count + 1 end for EA counter: adds 1 to count each time the loop repeats
6total <- 0 for i from 1 to 5 total <- total + 1 end for Fcount is set to 0 inside the loop, so it resets every repeat and always ends at 1. Move count <- 0 to before the loop.

Definitions

count <- count + 1

Fill in the Blanks

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

1.
count <- _ for i from 1 to 10 count <- count + 1
2.
total <- 0 for i from 1 to 5 total <- total + _
3.
for i from 1 to 5 input score __ score > 50 then count <- count + 1
4.
total <- 0 for i from 1 to 4 total <- total _ i
5.
count <- 0 while count < 5 count <- _____ + 1

Spot the Error

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

Question 1
A input n
B total <- n
C for i from 1 to n
D total <- total + i
E print total
Question 2
A count <- 0
B for i from 1 to 8
C input age
D count <- 1
E print count
Question 3
A total <- 0
B for i from 1 to 5
C input price
D total <- total + 1
E print total
Question 4
A n <- 10
B counter <- 0
C for i from 1 to n
D count <- count + 1
E print count
Question 5
A count <- 3
B for i from 1 to 6
C input mark
D if mark >= 50 then
E count <- count + 1
F print count

Answer Key

Counters and accumulators in pseudocode
Section A: Quick Questions
Q1
A — count <- 0
Q2
A — count <- count + 1
Q3
B — total <- total + mark
Q4
A — False: set total <- 0 before the loop
Q5
B — if score > 50 then count <- count + 1
Section B: Match the Pairs
Q1
count <- count + 1 → A counter: adds 1 to count each time the loop repeats
Q2
total <- total + score → An accumulator: adds the value of score to a running total each time the loop repeats
Q3
Set the counter to 0 before the loop so it starts from nothing → count <- 0 for i from 1 to 10 count <- count + 1 end for
Q4
Count only the matches: add 1 only when a condition is true → for i from 1 to 5 if mark >= 50 then passes <- passes + 1 end if end for
Q5
for i from 1 to 10 count <- 0 count <- count + 1 end for → count is set to 0 inside the loop, so it resets every repeat and always ends at 1. Move count <- 0 to before the loop.
Q6
total <- 0 for i from 1 to 5 total <- total + 1 end for → The accumulator adds 1 instead of the value being collected, so it only counts repeats. It should be total <- total + x.
Section C: Definitions
Q1
count <- count + 1 — A counter. It adds 1 to a variable each time a loop repeats. Set it to 0 before the loop. An accumulator works the same way: total <- total + x adds a value each time.
Section D: Fill in the Blanks
Q1
count <- 0 for i from 1 to 10 count <- count + 1
Q2
total <- 0 for i from 1 to 5 total <- total + i
Q3
for i from 1 to 5 input score if score > 50 then count <- count + 1
Q4
total <- 0 for i from 1 to 4 total <- total + i
Q5
count <- 0 while count < 5 count <- count + 1
Section E: Spot the Error
Q1
B — The running total must be set to 0 before the loop. Starting it at n adds an extra n to the answer. It should be total <- 0.
Q2
D — This line replaces count with 1 each time instead of adding 1, so count never gets above 1. It should be count <- count + 1.
Q3
D — An accumulator adds the value from each repeat, not just 1. Adding 1 only counts the prices. It should be total <- total + price.
Q4
B — The variable set to 0 is called counter, but the loop and print use count. The names must match. It should be count <- 0.
Q5
A — A counter must start at 0 before the loop. Starting at 3 makes the final count 3 too high. It should be count <- 0.