Counting only items that meet a condition

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

Counting only items that meet a condition

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which loop body counts how many marks are 50 or more?
A Count ← Count + 1 ENDIF IF Mark[i] >= 50 THEN
B IF Mark[i] >= 50 THEN Count ← Count + 1 ENDIF
2. A student wants to count only the items that meet a condition. Where does Count ← Count + 1 go?
A Inside the IF, so it only runs when the condition is true
B Outside the IF, so it runs on every loop
3. Which line counts the marks, and which line totals them? Pick the line that COUNTS marks of 50 or more.
A Count ← Count + Mark[i]
B Count ← Count + 1
4. Which IF counts how many names in Name[] are "Ali"?
A IF Name[i] = "Ali" THEN Count ← Count + 1
B IF Name[i] ← "Ali" THEN Count ← Count + 1
5. Which IF counts students who scored 50 or more in BOTH Maths and Science?
A IF Maths[i] >= 50 AND Science[i] >= 50 THEN
B IF Maths[i] >= 50 OR Science[i] >= 50 THEN

Match the Pairs

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

Left columnRight column
1IF Mark[i] >= 50 THEN Count ← Count + 1 AIF Name[i] = "Ali" THEN Count ← Count + 1
2IF Colour[i] = "Red" AND Size[i] > 10 THEN Count ← Count + 1 BCounts only the items that are red and also bigger than 10, because AND needs both conditions to be true
3To count how many names are "Ali", add 1 to Count only when the current name matches the string CThis adds the mark's value to Count instead of 1, so it is making a total, not counting passes.
4A counter goes up by 1 for each matching item, but a total goes up by the item's value DThe counter is updated outside the IF, so every mark is counted, not just those that are 50 or more.
5FOR i ← 1 TO 10 Count ← Count + 1 IF Mark[i] >= 50 THEN ENDIF NEXT i ETotal ← Total + Mark[i]
6IF Mark[i] >= 50 THEN Count ← Count + Mark[i] FAdds 1 to Count only when the current mark is 50 or more, so Count tallies the passing marks

Definitions

IF Mark[i] >= 50 THEN Count ← Count + 1

Fill in the Blanks

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

1.
IF Mark[i] __ 50 THEN Count ← Count + 1
2.
IF Name[i] _ "Ali" THEN Count ← Count + 1
3.
IF Mark[i] >= 50 ___ Mark[i] < 70 THEN Count ← Count + 1 ENDIF
4.
IF Mark[i] >= 50 THEN Total ← _____ + Mark[i] ENDIF
5.
IF Mark[i] >= 50 THEN Count ← Count + _ ENDIF

Spot the Error

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

Question 1
A Count ← 0
B FOR i ← 1 TO 30
C IF Mark[i] > 50 THEN
D Count ← Count + 1
E ENDIF
F NEXT i
Question 2
A Count ← 0
B FOR i = 1 TO 20
C IF Answer[i] = "Yes" THEN
D Count ← Count + 1
E ENDIF
F NEXT i
Question 3
A Count ← 0
B FOR i ← 1 TO 25
C IF Score[i] >= 40 THEN
D Count ← Count + Score[i]
E ENDIF
F NEXT i
Question 4
A Count ← 0
B FOR i ← 1 UNTIL 7
C IF Temp[i] > 30 THEN
D Count ← Count + 1
E ENDIF
F NEXT i
Question 5
A Count ← 0
B FOR i ← 1 TO 20
C IF Colour[i] ← "red" THEN
D Count ← Count + 1
E ENDIF
F NEXT i

Answer Key

Counting only items that meet a condition
Section A: Quick Questions
Q1
B — IF Mark[i] >= 50 THEN Count ← Count + 1 ENDIF
Q2
A — Inside the IF, so it only runs when the condition is true
Q3
B — Count ← Count + 1
Q4
A — IF Name[i] = "Ali" THEN Count ← Count + 1
Q5
A — IF Maths[i] >= 50 AND Science[i] >= 50 THEN
Section B: Match the Pairs
Q1
IF Mark[i] >= 50 THEN Count ← Count + 1 → Adds 1 to Count only when the current mark is 50 or more, so Count tallies the passing marks
Q2
IF Colour[i] = "Red" AND Size[i] > 10 THEN Count ← Count + 1 → Counts only the items that are red and also bigger than 10, because AND needs both conditions to be true
Q3
To count how many names are "Ali", add 1 to Count only when the current name matches the string → IF Name[i] = "Ali" THEN Count ← Count + 1
Q4
A counter goes up by 1 for each matching item, but a total goes up by the item's value → Total ← Total + Mark[i]
Q5
FOR i ← 1 TO 10 Count ← Count + 1 IF Mark[i] >= 50 THEN ENDIF NEXT i → The counter is updated outside the IF, so every mark is counted, not just those that are 50 or more.
Q6
IF Mark[i] >= 50 THEN Count ← Count + Mark[i] → This adds the mark's value to Count instead of 1, so it is making a total, not counting passes.
Section C: Definitions
Q1
IF Mark[i] >= 50 THEN Count ← Count + 1 — An IF inside a loop adds 1 to Count only when an item meets the condition, so Count tallies the matching items. A total adds the item's value instead.
Section D: Fill in the Blanks
Q1
IF Mark[i] >= 50 THEN Count ← Count + 1
Q2
IF Name[i] = "Ali" THEN Count ← Count + 1
Q3
IF Mark[i] >= 50 AND Mark[i] < 70 THEN Count ← Count + 1 ENDIF
Q4
IF Mark[i] >= 50 THEN Total ← Total + Mark[i] ENDIF
Q5
IF Mark[i] >= 50 THEN Count ← Count + 1 ENDIF
Section E: Spot the Error
Q1
C — A mark of exactly 50 should be counted, so the condition must be >= 50. Using > 50 leaves out students who scored exactly 50.
Q2
B — A FOR loop sets its start value with the assignment arrow, so it must be FOR i ← 1 TO 20. Writing = is a comparison and is not valid in a FOR header.
Q3
D — This adds the score itself, so it builds a total instead of a count. A counter adds 1 for each matching item: Count ← Count + 1.
Q4
B — A FOR loop uses the keyword TO for its end value, so the line should be FOR i ← 1 TO 7. UNTIL belongs to REPEAT...UNTIL loops.
Q5
C — The IF must compare the item with the string, so it needs = and not the assignment arrow. It should be IF Colour[i] = "red" THEN.