← ACARA Digital Technologies Years 5–6

Counting only some things with IF inside a loop

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.

Understanding Counting only some things with IF inside a loop in ACARA Digital Technologies Years 5–6

What it does: A black card does not meet card = red, so the step after THEN is skipped and count does not change. The counter starts at 0 before the loop. The IF inside the loop adds 1 only for red cards. The condition is false because the card is blue. The counter stays the same. There are 3 red cards. The two blue cards do not meet the condition, so they are not counted. The IF must be inside the loop. Then every answer gets checked one at a time. The loop repeats for every item, but the IF decides whether the counter goes up.

Common mistakes: 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. The loop needs to go through every answer in the quiz, not through the score. It should be FOR EACH answer IN quiz.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 106–119) and word search (Modules 106–132).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
A loop checks each card in a pile. Which line counts only the red cards?
IF card = red THEN count ← count + 1count ← count + 1 (with no IF)
Matches
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.
Applying
Meanings
Term
IF card = red THEN count ← count + 1
Meaning
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.
Practice
count ← 0
Testing
Gaps
IF card = red
Check Answer
Spots
1count ← 1
2FOR EACH card IN pile

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Pufferfish' badge — Error Handler, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

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)
Without the IF, the counter would go up for every card. The IF makes it count only red cards.
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 ✓
The condition is false because the card is blue. The counter stays the same.
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
There are 3 red cards. The two blue cards do not meet the condition, so they are not counted.
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
The IF must be inside the loop. Then every answer gets checked one at a time.
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 ✓
When the condition is false, nothing in the THEN part runs. The score does not change.

Match the Pairs

IF card = red THEN count ← count + 1 → Adds 1 to the counter only when the card being checked is red
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
When a card is black, the condition is false, so the counter stays the same → IF card = red THEN count ← count + 1
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
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.
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.

Key Term

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.

Fill in the Blanks

1. FOR EACH card IN pile IF card = red THEN count ← count + 1
Hint: Type the keyword that comes after the condition and starts the action.
2. REPEAT 10 TIMES IF answer = right THEN score ← score + 1
Hint: Type the keyword that checks the condition for each item in the loop.
3. FOR EACH card IN pile IF card = red THEN count ← count + 1
Hint: Type the operator that makes the counter go up.
4. count ← 0 FOR EACH card IN pile IF card = red THEN count ← count + 1
Hint: Type the number the counter should start at before any cards are checked.
5. score ← 0 FOR EACH answer IN quiz IF answer = right THEN score ← score + 1
Hint: Type the variable name that is being added to, so the old total is kept.

Spot the Error

Question 1
A count ← 1
B FOR EACH card IN pile
C IF card = red THEN
D count ← count + 1
E OUTPUT count
Line 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.
Question 2
A score ← 0
B FOR EACH answer IN score
C IF answer = correct THEN
D score ← score + 1
E OUTPUT score
Line B — The loop needs to go through every answer in the quiz, not through the score. It should be FOR EACH answer IN quiz.
Question 3
A count ← 0
B FOR EACH card IN pile
C IF card = red THEN
D count ← count - 1
E OUTPUT count
Line D — When a card is red, the counter should go UP by 1, not down. It should be count ← count + 1.
Question 4
A redCount ← 0
B FOR EACH card IN pile
C IF card = blue THEN
D redCount ← redCount + 1
E OUTPUT redCount
Line 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.
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
Line 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.

Student with a class code? Join your class →