Iterating through records in pseudocode

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

Iterating through records in pseudocode

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which loop visits every record in an array of records exactly once?
A if student in students
B for each student in students
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
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
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
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

Match the Pairs

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

Left columnRight column
1for each student in students AThe 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.
2total ← total + order.price BVisits every record in the students array once, giving the loop variable one record at a time
3Count only the records that match a condition by testing a field inside the loop Cfor each student in students if student.house = "Red" then count ← count + 1 end if next student
4Loop over an array of records by index and read a field from the current record DThe points field is read without naming the record. It should be student.points so the loop reads the field from the current record.
5for each student in students total ← total + points EReads the price field of the current record and adds it to a running total
6for each order in orders count ← 0 if order.quantity > 2 then count ← count + 1 Ffor i from 1 to length(products) print products[i].name next i

Definitions

for each record in records

Fill in the Blanks

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

1.
for ____ student in students print student.name
2.
for each student in ________ print student.name
3.
for each student in students print _______.mark
4.
for each s in students if s.mark __ 50 then count ← count + 1
5.
total ← 0 for each s in students total ← total _ s.mark

Spot the Error

Shade in the letter of the line that contains an 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
Question 2
A total <- 0
B for each o in orders
C total <- o.price
D end for
E print total
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
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
Question 5
A for i from 1 to length(cars)-1
B print cars[i].make
C end for

Answer Key

Iterating through records in pseudocode
Section A: Quick Questions
Q1
B — for each student in students
Q2
A — total <- total + student.mark
Q3
A — for each s in students if s.mark >= 50 then count <- count + 1
Q4
B — Before the loop starts
Q5
A — for each item in items total <- total + item.price
Section B: Match the Pairs
Q1
for each student in students → Visits every record in the students array once, giving the loop variable one record at a time
Q2
total ← total + order.price → Reads the price field of the current record and adds it to a running total
Q3
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
Q4
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
Q5
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.
Q6
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.
Section C: Definitions
Q1
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.
Section D: Fill in the Blanks
Q1
for each student in students print student.name
Q2
for each student in students print student.name
Q3
for each student in students print student.mark
Q4
for each s in students if s.mark >= 50 then count ← count + 1
Q5
total ← 0 for each s in students total ← total + s.mark
Section E: Spot the Error
Q1
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.
Q2
C — This overwrites total on every pass, so it only holds the last price. An accumulator must add to itself: total <- total + o.price.
Q3
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.
Q4
D — A field must be read through the loop variable. 'amount' on its own is not defined. It should be s.amount.
Q5
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.