Counting in Cambridge pseudocode

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

Counting in Cambridge pseudocode

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which line correctly adds one to a counter called Count?
A Count ← Count + 1
B Count + 1 ← Count
2. Which line should come BEFORE the loop starts so a counter begins correctly?
A Count ← 0
B Count ← Count + 1
3. Which declaration is correct for a counter that holds whole numbers?
A DECLARE Count : STRING
B DECLARE Count : INTEGER
4. Where does Count ← Count + 1 go to count the number of times a loop runs?
A Inside the loop, so it runs on every pass
B After the loop, so it runs once at the end
5. A FOR loop runs 5 times and Count starts at 0. Count ← Count + 1 is the only line inside the loop. What is Count at the end?
A 6
B 5

Match the Pairs

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

Left columnRight column
1DECLARE Count : INTEGER AREPEAT INPUT Num IF Num <> -1 THEN Count ← Count + 1 UNTIL Num = -1
2Count ← Count + 1 BCount ← 0
3The counter must be set to zero before the loop starts so counting begins from nothing CCreates an integer variable called Count to hold the number of times something happens
4A REPEAT loop that counts how many numbers are entered before the sentinel -1 is typed DCount is reset to 0 inside the loop, so it is wiped on every pass and ends as 1. Count ← 0 must go before the loop.
5FOR i ← 1 TO 10 Count ← 0 Count ← Count + 1 NEXT i EAdds 1 to the current value of Count each time this line runs inside the loop
6Count ← 0 WHILE Num <> -1 DO INPUT Num Count ← Count ENDWHILE FCount ← Count never changes the counter. It must be Count ← Count + 1 so that 1 is added on each pass.

Definitions

Count ← Count + 1

Fill in the Blanks

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

1.
DECLARE Count : _______ Count ← 0
2.
Count ← _ FOR Index ← 1 TO 5 Count ← Count + 1
3.
FOR Index ← 1 TO 10 Count ← Count _ 1 NEXT Index
4.
WHILE Num <> -1 DO Count ← Count + 1 ________
5.
REPEAT Count ← _____ + 1 UNTIL Num = -1

Spot the Error

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

Question 1
A DECLARE Count : INTEGER
B Count ← 0
C FOR i = 1 TO 10
D Count ← Count + 1
E NEXT i
Question 2
A Count ← 0
B INPUT Num
C WHILE Num <> -1 THEN
D Count ← Count + 1
E INPUT Num
F ENDWHILE
Question 3
A Count ← 0
B REPEAT
C Count ← Count - 1
D INPUT Guess
E UNTIL Guess = Secret
Question 4
A DECLARE Count : INTEGER
B Count ← 0
C FOR i ← 1 TO 8
D Count ← Count + i
E NEXT i
F OUTPUT Count
Question 5
A DECLARE Count : INTEGER
B Count ← 1
C FOR i ← 1 TO 5
D INPUT Mark
E Count ← Count + 1
F NEXT i

Answer Key

Counting in Cambridge pseudocode
Section A: Quick Questions
Q1
A — Count ← Count + 1
Q2
A — Count ← 0
Q3
B — DECLARE Count : INTEGER
Q4
A — Inside the loop, so it runs on every pass
Q5
B — 5
Section B: Match the Pairs
Q1
DECLARE Count : INTEGER → Creates an integer variable called Count to hold the number of times something happens
Q2
Count ← Count + 1 → Adds 1 to the current value of Count each time this line runs inside the loop
Q3
The counter must be set to zero before the loop starts so counting begins from nothing → Count ← 0
Q4
A REPEAT loop that counts how many numbers are entered before the sentinel -1 is typed → REPEAT INPUT Num IF Num <> -1 THEN Count ← Count + 1 UNTIL Num = -1
Q5
FOR i ← 1 TO 10 Count ← 0 Count ← Count + 1 NEXT i → Count is reset to 0 inside the loop, so it is wiped on every pass and ends as 1. Count ← 0 must go before the loop.
Q6
Count ← 0 WHILE Num <> -1 DO INPUT Num Count ← Count ENDWHILE → Count ← Count never changes the counter. It must be Count ← Count + 1 so that 1 is added on each pass.
Section C: Definitions
Q1
Count ← Count + 1 — Adds 1 to the counter variable Count each time it runs inside a loop. Set Count ← 0 before the loop starts, so Count ends up holding how many times the loop ran.
Section D: Fill in the Blanks
Q1
DECLARE Count : INTEGER Count ← 0
Q2
Count ← 0 FOR Index ← 1 TO 5 Count ← Count + 1
Q3
FOR Index ← 1 TO 10 Count ← Count + 1 NEXT Index
Q4
WHILE Num <> -1 DO Count ← Count + 1 ENDWHILE
Q5
REPEAT Count ← Count + 1 UNTIL Num = -1
Section E: Spot the Error
Q1
C — A FOR loop sets its counter with the assignment arrow, not =. The line should be FOR i ← 1 TO 10.
Q2
C — A WHILE loop uses DO, not THEN (THEN belongs to IF). The line should be WHILE Num <> -1 DO.
Q3
C — Counting adds 1 each pass. Subtracting 1 makes Count go down instead of up. It should be Count ← Count + 1.
Q4
D — Counting always adds exactly 1, not the loop variable i. Adding i would build a total instead. It should be Count ← Count + 1.
Q5
B — The counter must start at 0 before the loop. Starting at 1 makes the final count one too high. It should be Count ← 0.