← QCE Digital Solutions Unit 3

Iterating through records in pseudocode

A loop that visits every record in an array once, so you can read, test or change each record's fields, such as counting or totalling the records that match.

Understanding Iterating through records in pseudocode in QCE Digital Solutions Unit 3

What it does: The loop visits every record, the if tests the house field, and the counter goes up only when the record matches. The index i selects one record at a time from the array, and .name reads that record's name field. An accumulator must add to its previous value. Writing total <- item.price overwrites it, so only the last price is kept. The loop variable takes each record in turn, one per pass. You then use p.fieldName to reach its fields. An index loop needs a number as the end value, such as the array length. An array is not a number, so it cannot be the limit.

Common mistakes: The loop header is missing the keyword 'in'. It should be 'for each s in students' so the loop visits every record in the students array. This overwrites total on every pass, so it only holds the last price. An accumulator must add to itself: total <- total + o.price.

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 194–250) and word search (Modules 194–337).

Take this worksheet further — free with Cloodit

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

Learning
Duo
Which loop visits every record in an array of records exactly once?
if student in studentsfor each student in students
Matches
for each student in students total ← total + points
↓
The points field is read without naming the record. It should be student.points so the loop reads the field from the current record.
Applying
Meanings
Term
for each record in records
Meaning
A loop that visits every record in an array once, so you can read, test or change each record's fields, such as counting or totalling the records that match.
Practice
count ← 0
Testing
Gaps
for student in students
Check Answer
Spots
1count <- 0
2for each s students

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

The 'Gila Monster' badge — Patient Coder, 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. Which loop visits every record in an array of records exactly once?
A. if student in students
B. for each student in students ✓
A for each loop repeats its body once per record. An if statement only makes a single decision and does not repeat.
2. Which line correctly reads the mark field of the current record inside the loop?
A. total <- total + student.mark ✓
B. total <- total + students.mark
Inside the loop, student is the current record, so student.mark reads its field. students is the whole array and has no single mark.
3. You want to count how many records have a mark of 50 or more. Which is correct?
A. for each s in students if s.mark >= 50 then count <- count + 1 ✓
B. for each s in students count <- s.mark >= 50
Test each record with an if, then add 1 to a counter when it matches. Assigning the comparison result to count does not keep a running tally.
4. Where should a counter be set to 0 when counting matching records?
A. Inside the loop, on every pass
B. Before the loop starts ✓
If the counter is reset inside the loop, it goes back to 0 for every record and the final count is wrong. Set it once before the loop.
5. To total the price field of every record in an array called items, which is correct?
A. for each item in items total <- total + item.price ✓
B. for each item in items total <- item.price
An accumulator must add to its previous value. Writing total <- item.price overwrites it, so only the last price is kept.

Match the Pairs

for each student in students → Visits every record in the students array once, giving the loop variable one record at a time
total ← total + order.price → Reads the price field of the current record and adds it to a running total
Count only the records that match a condition by testing a field inside the loop → for each student in students if student.house = "Red" then count ← count + 1 end if next student
Loop over an array of records by index and read a field from the current record → for i from 1 to length(products) print products[i].name next i
for each student in students total ← total + points → The points field is read without naming the record. It should be student.points so the loop reads the field from the current record.
for each order in orders count ← 0 if order.quantity > 2 then count ← count + 1 → The counter is reset to 0 inside the loop, so it restarts on every record. It should be set to 0 once before the loop starts.

Key Term

for each record in records
A loop that visits every record in an array once, so you can read, test or change each record's fields, such as counting or totalling the records that match.

Fill in the Blanks

1. for each student in students print student.name
Hint: Type the keyword after for that makes the loop visit every record in the array.
2. for each student in students print student.name
Hint: Type the name of the array of records that the loop works through.
3. for each student in students print student.mark
Hint: Type the loop variable that holds the current record, so its mark field can be read.
4. for each s in students if s.mark >= 50 then count ← count + 1
Hint: Type the comparison operator that tests whether a record's mark is at or above the pass mark.
5. total ← 0 for each s in students total ← total + s.mark
Hint: Type the arithmetic operator that adds each record's mark to the running total.

Spot the Error

Question 1
A count <- 0
B for each s students
C if s.yearLevel = 12 then
D count <- count + 1
E end if
F end for
Line B — The loop header is missing the keyword 'in'. It should be 'for each s in students' so the loop visits every record in the students array.
Question 2
A total <- 0
B for each o in orders
C total <- o.price
D end for
E print total
Line C — This overwrites total on every pass, so it only holds the last price. An accumulator must add to itself: total <- total + o.price.
Question 3
A count <- 0
B for each p in players
C if p.score >= 50 then
D count <- count + p.score
E end if
F end for
Line D — A counter goes up by 1 for each matching record. Adding p.score makes it a total of scores. It should be count <- count + 1.
Question 4
A total <- 0
B for each s in sales
C if s.region = "QLD" then
D total <- total + amount
E end if
F end for
Line D — A field must be read through the loop variable. 'amount' on its own is not defined. It should be s.amount.
Question 5
A for i from 1 to length(cars)-1
B print cars[i].make
C end for
Line A — Stopping at length(cars)-1 means the last record is never visited. The loop should run to length(cars) so every record is read once.

Student with a class code? Join your class →