Counting only some things with IF inside a loop

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

Counting only some things with IF inside a loop

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. A loop checks each card in a pile. Which line counts only the red cards?
A IF card = red THEN count ← count + 1
B count ← count + 1 (with no IF)
2. count is 0. The next card is blue. The algorithm says: IF card = red THEN count ← count + 1. What is count afterwards?
A 1
B 0
3. A pile has 5 cards: red, blue, red, red, blue. The loop counts only red cards. What does count end up as?
A 3
B 5
4. Where should the IF go to count only the correct quiz answers?
A Inside the loop, so it checks each answer
B After the loop, so it checks only once
5. A quiz loop has this line: IF answer is correct THEN score ← score + 1. A student gets an answer wrong. What happens to score?
A score goes down by 1
B score stays the same

Match the Pairs

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

Left columnRight column
1IF card = red THEN count ← count + 1 AAdds 1 to the score only when the quiz answer is correct, and leaves the score alone if it is wrong
2IF answer = correct THEN score ← score + 1 BThe count goes up on every turn of the loop, not only for red cards. The add-1 step must come after THEN so it only happens when the card is red.
3When a card is black, the condition is false, so the counter stays the same CAdds 1 to the counter only when the card being checked is red
4Start the counter at 0, then check every card in the pile and count only the red ones DIF card = red THEN count ← count + 1
5REPEAT 10 TIMES count ← count + 1 IF card = red THEN say count EThe counter is reset to 0 instead of adding 1. It should be count ← count + 1 so the red cards are added up.
6count ← 0 REPEAT 10 TIMES IF card = red THEN count ← 0 Fcount ← 0 REPEAT 10 TIMES IF card = red THEN count ← count + 1

Definitions

IF card = red THEN count ← count + 1

Fill in the Blanks

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

1.
FOR EACH card IN pile IF card = red ____ count ← count + 1
2.
REPEAT 10 TIMES __ answer = right THEN score ← score + 1
3.
FOR EACH card IN pile IF card = red THEN count ← count _ 1
4.
count ← _ FOR EACH card IN pile IF card = red THEN count ← count + 1
5.
score ← 0 FOR EACH answer IN quiz IF answer = right THEN score ← _____ + 1

Spot the Error

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

Question 1
A count ← 1
B FOR EACH card IN pile
C IF card = red THEN
D count ← count + 1
E OUTPUT count
Question 2
A score ← 0
B FOR EACH answer IN score
C IF answer = correct THEN
D score ← score + 1
E OUTPUT score
Question 3
A count ← 0
B FOR EACH card IN pile
C IF card = red THEN
D count ← count - 1
E OUTPUT count
Question 4
A redCount ← 0
B FOR EACH card IN pile
C IF card = blue THEN
D redCount ← redCount + 1
E OUTPUT redCount
Question 5
A count ← 0
B REPEAT UNTIL card = red
C take the next card
D IF card = red THEN
E count ← count + 1
F OUTPUT count

Answer Key

Counting only some things with IF inside a loop
Section A: Quick Questions
Q1
A — IF card = red THEN count ← count + 1
Q2
B — 0
Q3
A — 3
Q4
A — Inside the loop, so it checks each answer
Q5
B — score stays the same
Section B: Match the Pairs
Q1
IF card = red THEN count ← count + 1 → Adds 1 to the counter only when the card being checked is red
Q2
IF answer = correct THEN score ← score + 1 → Adds 1 to the score only when the quiz answer is correct, and leaves the score alone if it is wrong
Q3
When a card is black, the condition is false, so the counter stays the same → IF card = red THEN count ← count + 1
Q4
Start the counter at 0, then check every card in the pile and count only the red ones → count ← 0 REPEAT 10 TIMES IF card = red THEN count ← count + 1
Q5
REPEAT 10 TIMES count ← count + 1 IF card = red THEN say count → The count goes up on every turn of the loop, not only for red cards. The add-1 step must come after THEN so it only happens when the card is red.
Q6
count ← 0 REPEAT 10 TIMES IF card = red THEN count ← 0 → The counter is reset to 0 instead of adding 1. It should be count ← count + 1 so the red cards are added up.
Section C: Definitions
Q1
IF card = red THEN count ← count + 1 — Inside a loop, the IF checks each item. Only when the item meets the condition does the counter go up by 1. If the condition is false, the counter stays the same.
Section D: Fill in the Blanks
Q1
FOR EACH card IN pile IF card = red THEN count ← count + 1
Q2
REPEAT 10 TIMES IF answer = right THEN score ← score + 1
Q3
FOR EACH card IN pile IF card = red THEN count ← count + 1
Q4
count ← 0 FOR EACH card IN pile IF card = red THEN count ← count + 1
Q5
score ← 0 FOR EACH answer IN quiz IF answer = right THEN score ← score + 1
Section E: Spot the Error
Q1
A — The counter must start at 0 before any cards are checked. Starting at 1 means the final count is always one too high. It should be count ← 0.
Q2
B — The loop needs to go through every answer in the quiz, not through the score. It should be FOR EACH answer IN quiz.
Q3
D — When a card is red, the counter should go UP by 1, not down. It should be count ← count + 1.
Q4
C — We are counting red cards, so the IF must check for red. Checking for blue would count the wrong cards. It should be IF card = red THEN.
Q5
B — The loop would stop at the first red card, so the other cards would never be checked. It should repeat 8 times so every card gets checked: REPEAT 8 TIMES.