Counting items that meet a compound condition

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

Counting items that meet a compound condition

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which line counts only marks from 50 to 100 inclusive?
A IF mark >= 50 OR mark <= 100 THEN count ← count + 1
B IF mark >= 50 AND mark <= 100 THEN count ← count + 1
2. The loop must count entries where day is Saturday or Sunday. Which condition is correct?
A IF day = "Saturday" OR day = "Sunday" THEN
B IF day = "Saturday" AND day = "Sunday" THEN
3. Where should the line count ← count + 1 go to count qualifying items?
A Inside the IF, so it only runs when the condition is true
B After ENDIF, so it runs for every item
4. What must happen to count before the loop starts?
A count ← 1
B count ← 0
5. Which condition counts items that are NOT blank?
A IF NOT (item = "") THEN count ← count + 1
B IF item = "" 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
1IF mark >= 50 AND mark <= 100 THEN count ← count + 1 AAdds 1 to count for each entry that falls on a weekend day, because only one of the two tests needs to be true.
2IF day = "Saturday" OR day = "Sunday" THEN count ← count + 1 Bcount ← 0 FOR i ← 1 TO 10 IF mark >= 50 AND mark <= 100 THEN count ← count + 1 NEXT i
3Count only the items that are NOT blank by putting a NOT test inside the loop's IF. CAdds 1 to count only when the mark is in the range 50 to 100, so both parts of the test must be true.
4Set the counter to 0 before the loop starts so it begins with nothing counted. DOR makes the test true for every mark, because any number is at least 50 or at most 100. It should be AND so only marks from 50 to 100 are counted.
5IF mark >= 50 OR mark <= 100 THEN count ← count + 1 EIF NOT (item = "") THEN count ← count + 1
6FOR i ← 1 TO 10 count ← 0 IF day = "Saturday" OR day = "Sunday" THEN count ← count + 1 NEXT i Fcount is reset to 0 inside the loop on every pass, so the total is wiped each time. It must be set to 0 once before the loop.

Definitions

IF mark >= 50 AND mark <= 100 THEN count ← count + 1

Fill in the Blanks

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

1.
IF mark >= 50 ___ mark <= 100 THEN count ← count + 1 ENDIF
2.
IF day = "Saturday" __ day = "Sunday" THEN count ← count + 1 ENDIF
3.
IF ___ (item = "") THEN count ← count + 1 ENDIF
4.
IF mark >= 50 AND mark <= 100 THEN count ← count + _ ENDIF
5.
IF day = "Saturday" OR ___ = "Sunday" THEN count ← count + 1 ENDIF

Spot the Error

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

Question 1
A count ← 1
B FOR EACH m IN marks
C IF m >= 50 AND m <= 100 THEN
D count ← count + 1
E ENDIF
F NEXT m
Question 2
A count ← 0
B FOR EACH day IN days
C IF day = 6 OR day = 7 THEN
D count ← count - 1
E ENDIF
F NEXT day
Question 3
A count ← 0
B FOR EACH n IN names
C IF NOT (name = "") THEN
D count ← count + 1
E ENDIF
F NEXT name
Question 4
A count ← 0
B FOR EACH day IN days
C IF day = 6 AND day = 7 THEN
D count ← count + 1
E ENDIF
F NEXT day
Question 5
A count ← 0
B FOR EACH m TO marks
C IF m >= 50 AND m <= 100 THEN
D count ← count + 1
E ENDIF
F NEXT m

Answer Key

Counting items that meet a compound condition
Section A: Quick Questions
Q1
B — IF mark >= 50 AND mark <= 100 THEN count ← count + 1
Q2
A — IF day = "Saturday" OR day = "Sunday" THEN
Q3
A — Inside the IF, so it only runs when the condition is true
Q4
B — count ← 0
Q5
A — IF NOT (item = "") THEN count ← count + 1
Section B: Match the Pairs
Q1
IF mark >= 50 AND mark <= 100 THEN count ← count + 1 → Adds 1 to count only when the mark is in the range 50 to 100, so both parts of the test must be true.
Q2
IF day = "Saturday" OR day = "Sunday" THEN count ← count + 1 → Adds 1 to count for each entry that falls on a weekend day, because only one of the two tests needs to be true.
Q3
Count only the items that are NOT blank by putting a NOT test inside the loop's IF. → IF NOT (item = "") THEN count ← count + 1
Q4
Set the counter to 0 before the loop starts so it begins with nothing counted. → count ← 0 FOR i ← 1 TO 10 IF mark >= 50 AND mark <= 100 THEN count ← count + 1 NEXT i
Q5
IF mark >= 50 OR mark <= 100 THEN count ← count + 1 → OR makes the test true for every mark, because any number is at least 50 or at most 100. It should be AND so only marks from 50 to 100 are counted.
Q6
FOR i ← 1 TO 10 count ← 0 IF day = "Saturday" OR day = "Sunday" THEN count ← count + 1 NEXT i → count is reset to 0 inside the loop on every pass, so the total is wiped each time. It must be set to 0 once before the loop.
Section C: Definitions
Q1
IF mark >= 50 AND mark <= 100 THEN count ← count + 1 — Inside a loop, an IF with AND or OR tests each item and adds 1 to count only when the whole combined condition is true, so count ends as the number of items that qualify.
Section D: Fill in the Blanks
Q1
IF mark >= 50 AND mark <= 100 THEN count ← count + 1 ENDIF
Q2
IF day = "Saturday" OR day = "Sunday" THEN count ← count + 1 ENDIF
Q3
IF NOT (item = "") THEN count ← count + 1 ENDIF
Q4
IF mark >= 50 AND mark <= 100 THEN count ← count + 1 ENDIF
Q5
IF day = "Saturday" OR day = "Sunday" THEN count ← count + 1 ENDIF
Section E: Spot the Error
Q1
A — The counter must start at 0 before the loop. Starting at 1 makes the final count one too high. It should be count ← 0.
Q2
D — Each Saturday (6) or Sunday (7) entry should add 1 to count, not take 1 away. The line should be count ← count + 1.
Q3
B — The loop variable is n, but the IF tests name, which is never set. The loop should be FOR EACH name IN names.
Q4
C — A day cannot be both 6 and 7, so AND is never true and count stays 0. To count Saturday OR Sunday entries, use OR.
Q5
B — A FOR EACH loop goes through a list using the keyword IN, not TO. It should be FOR EACH m IN marks.