Counting in pseudocode

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

Counting in pseudocode

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which line should come BEFORE the loop when counting how many marks are 50 or more?
A Count ← 0
B Count ← 1
2. Which statement adds one to the count?
A Count ← Count + 1
B Count = Count + 1
3. Where does Count ← Count + 1 go if you only want to count the marks that are 50 or more?
A On its own line in the loop, outside any IF
B Inside an IF that checks Mark >= 50, inside the loop
4. A loop runs 20 times. Count ← Count + 1 is inside the loop but NOT inside an IF. What does Count hold at the end?
A 20 (the number of passes)
B The number of items that meet the condition
5. A loop runs 10 times and 4 items meet the condition. The increase is inside the IF. What is Count at the end?
A 10
B 4

Match the Pairs

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

Left columnRight column
1Count ← 0 ACount ← Count + 1 is outside the IF, so it counts every pass instead of only the items that meet the condition.
2Count ← Count + 1 BCount ← 0 is inside the loop, so the counter is reset to zero on every pass and the total is lost.
3To count only the marks that are 50 or more, put the counting statement inside an IF within the loop CAdds 1 to the counter each time it runs, so it counts one more matching item
4To count how many times the loop passes (every item, not just matches), add 1 in the loop with no IF DIF Mark >= 50 THEN Count ← Count + 1 ENDIF
5FOR Index ← 1 TO 10 Count ← 0 IF Mark[Index] > 50 THEN Count ← Count + 1 ENDIF NEXT Index EFOR Index ← 1 TO 10 Passes ← Passes + 1 NEXT Index
6Count ← 0 FOR Index ← 1 TO 10 Count ← Count + 1 IF Mark[Index] > 50 THEN OUTPUT Mark[Index] ENDIF NEXT Index FSets the counter to zero before the loop starts, so the count begins from nothing

Definitions

Count ← Count + 1

Fill in the Blanks

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

1.
Count ← _ FOR Index ← 1 TO 10 INPUT Mark
2.
FOR Index ← 1 TO 10 __ Mark >= 50 THEN Count ← Count + 1
3.
IF Mark >= 50 THEN Count ← Count + _ ENDIF
4.
IF Age >= 18 THEN Count ← _____ + 1 ENDIF
5.
IF Score > 80 THEN Count ← Count + 1 _____

Spot the Error

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

Question 1
A Count ← 0
B FOR Index ← 1 TO 10
C IF Mark[Index] >= 50
D Count ← Count + 1
E ENDIF
F NEXT Index
Question 2
A Count ← 1
B FOR Index ← 1 TO 30
C IF Pass[Index] = TRUE THEN
D Count ← Count + 1
E ENDIF
F NEXT Index
Question 3
A Count ← 0
B FOR Index = 1 TO 10
C IF Score[Index] > 90 THEN
D Count ← Count + 1
E ENDIF
F NEXT Index
Question 4
A Count ← 0
B FOR Index ← 1 TO 10
C IF Speed[Index] > 70 THEN
D Count ← 1
E ENDIF
F NEXT Index
Question 5
A Count ← 0
B FOR Index ← 1 TO 10
C IF Mark[Index] ← 100 THEN
D Count ← Count + 1
E ENDIF
F NEXT Index

Answer Key

Counting in pseudocode
Section A: Quick Questions
Q1
A — Count ← 0
Q2
A — Count ← Count + 1
Q3
B — Inside an IF that checks Mark >= 50, inside the loop
Q4
A — 20 (the number of passes)
Q5
B — 4
Section B: Match the Pairs
Q1
Count ← 0 → Sets the counter to zero before the loop starts, so the count begins from nothing
Q2
Count ← Count + 1 → Adds 1 to the counter each time it runs, so it counts one more matching item
Q3
To count only the marks that are 50 or more, put the counting statement inside an IF within the loop → IF Mark >= 50 THEN Count ← Count + 1 ENDIF
Q4
To count how many times the loop passes (every item, not just matches), add 1 in the loop with no IF → FOR Index ← 1 TO 10 Passes ← Passes + 1 NEXT Index
Q5
FOR Index ← 1 TO 10 Count ← 0 IF Mark[Index] > 50 THEN Count ← Count + 1 ENDIF NEXT Index → Count ← 0 is inside the loop, so the counter is reset to zero on every pass and the total is lost.
Q6
Count ← 0 FOR Index ← 1 TO 10 Count ← Count + 1 IF Mark[Index] > 50 THEN OUTPUT Mark[Index] ENDIF NEXT Index → Count ← Count + 1 is outside the IF, so it counts every pass instead of only the items that meet the condition.
Section C: Definitions
Q1
Count ← Count + 1 — Adds 1 to Count each time an item meets a condition. Put it inside an IF in the loop, and set Count ← 0 before the loop starts so the count begins at zero.
Section D: Fill in the Blanks
Q1
Count ← 0 FOR Index ← 1 TO 10 INPUT Mark
Q2
FOR Index ← 1 TO 10 IF Mark >= 50 THEN Count ← Count + 1
Q3
IF Mark >= 50 THEN Count ← Count + 1 ENDIF
Q4
IF Age >= 18 THEN Count ← Count + 1 ENDIF
Q5
IF Score > 80 THEN Count ← Count + 1 ENDIF
Section E: Spot the Error
Q1
C — The IF statement is missing THEN. It should be IF Mark[Index] >= 50 THEN so the count is only increased when the condition is true.
Q2
A — The counter must be set to 0 before the loop starts. Starting at 1 makes every final count one too high. It should be Count ← 0.
Q3
B — A FOR loop sets its counter with the assignment arrow, not =. It should be FOR Index ← 1 TO 10.
Q4
D — Count ← 1 just resets the count to 1 each time instead of adding to it. It should be Count ← Count + 1 so the count goes up by one for every match.
Q5
C — The arrow ← is for assignment. A condition must compare values using =. It should be IF Mark[Index] = 100 THEN.