← QCE Digital Solutions Unit 2

Pseudocode: presenting query results

Loops through the returned records, printing each as a formatted line under a heading, then prints a summary such as a count or total. If there are none, print a no results message.

Understanding Pseudocode: presenting query results in QCE Digital Solutions Unit 2

What it does: Checking whether the count is 0 lets the program tell the user nothing matched rather than showing a blank screen. The summary is printed once after all the lines, using the counter that was increased inside the loop. Formatting joins the chosen fields with separators so each line is easy to read. Printing the whole record does not control the layout. Adding to total each pass accumulates the sum. Assigning only record.mark overwrites the total each time. The no results message should show only when the count is 0. Using > 0 would show it when there are results. A heading row is printed one time above the records so the student can tell what each field shows.

Common mistakes: The heading line must be a fixed text string. This line uses r.name and r.mark before the loop has created r, so it would fail. It should be print "Name Mark". A for each loop header does not use the word then, because then belongs to an if statement. It should be for each r in results.

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 234–285) and word search (Modules 234–409).

Take this worksheet further — free with Cloodit

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

Learning
Duo
A query returns 5 student records. Where should the heading be printed?
Before the loop, so it prints onceInside the loop, so it prints for every record
Matches
for each record in results\n print "Name | Mark"\n print record.name + " | " + record.mark
↓
The heading is printed inside the loop, so it repeats above every record. It should be printed once before the loop.
Applying
Meanings
Term
for each record in results
Meaning
Loops through the returned records, printing each as a formatted line under a heading, then prints a summary such as a count or total. If there are none, print a no results message.
Practice
if results is empty then
Testing
Gaps
"Name | Points"
Check Answer
Spots
1print "Top students"
2print r.name, r.mark

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

The 'Arctic Hare' badge — Speed Indexer, 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 query returns 5 student records. Where should the heading be printed?
A. Before the loop, so it prints once ✓
B. Inside the loop, so it prints for every record
The heading goes before the loop. Inside the loop it would repeat once for every record.
2. Which loop prints each returned record as one line?
A. print results.name for each record in results
B. for each record in results print record.name ✓
The print must be inside the loop so it runs once per record. Printing outside the loop does not output each record.
3. Where should a count of the records be printed?
A. After the loop finishes ✓
B. At the start of each loop pass
A summary such as a count or total goes after the loop, once every record has been processed.
4. Which line builds a formatted output line for one record?
A. print record
B. print record.name + " - " + record.mark ✓
Formatting joins the chosen fields with separators so each line is easy to read. Printing the whole record does not control the layout.
5. A query returns zero records. What should the program do?
A. Print a no results message ✓
B. Print the heading and a total of nothing with no explanation
If nothing is returned, tell the user with a message such as "No results found" instead of showing a confusing empty report.

Match the Pairs

print "Name | Mark" → Prints a heading row once, before the loop, so each column of the results is labelled.
for each record in results → Repeats the loop body once for every record the query returned.
When the query returns no records, the program prints a no results message instead of an empty list. → if count = 0 then print "No results found"
After the loop, a summary line reports how many records were displayed. → print "Records found: " + count
for each record in results\n print "Name | Mark"\n print record.name + " | " + record.mark → The heading is printed inside the loop, so it repeats above every record. It should be printed once before the loop.
print record.name + record.mark → The fields have no separator, so the output runs together, such as Ava88. It needs a separator between the fields.

Key Term

for each record in results
Loops through the returned records, printing each as a formatted line under a heading, then prints a summary such as a count or total. If there are none, print a no results message.

Fill in the Blanks

1. print "Name | Points" for each record in results print record.name + " | " + record.points
Hint: Type the keyword that outputs the heading row before the loop starts.
2. print "Name | Points" for each record in results print record.name + " | " + record.points
Hint: Type the name of the variable holding the records returned by the query.
3. for each record in results print record.name + " - " + record.points end for
Hint: Type the operator that joins text and field values together into one formatted line.
4. count ← count + 1 end for print "Total records: " + count
Hint: Type the variable that holds the running number of records, used in the summary line.
5. if count = 0 then print "No results found" end if
Hint: Type the number of records that means the query returned nothing.

Spot the Error

Question 1
A print "Top students"
B print r.name, r.mark
C for each r in results
D print r.name, r.mark
E end for
Line B — The heading line must be a fixed text string. This line uses r.name and r.mark before the loop has created r, so it would fail. It should be print "Name Mark".
Question 2
A print "Name Points"
B for each r in results then
C print r.name, r.points
D end for
Line B — A for each loop header does not use the word then, because then belongs to an if statement. It should be for each r in results.
Question 3
A print "Name Points"
B for each r in results
C print r.name, r.points
D end if
Line D — The loop was opened with for each, so it must be closed with end for. It should be end for, not end if.
Question 4
A print ID, Name, Mark
B for each r in results
C print r.id, r.name, r.mark
D end for
Line A — The heading is text, so it needs quotation marks. Without them ID, Name and Mark are treated as undefined variables. It should be print "ID Name Mark".
Question 5
A count ← length(results)
B if count = 0 then
C print No results found
D end if
Line C — The no results message is a text string and must be in quotation marks. It should be print "No results found".

Student with a class code? Join your class →