← QCE Digital Solutions Unit 2

Pseudocode: processing a file of records for insertion

A loop that reads each line, splits it into fields, trims and validates them, then builds an INSERT statement for each good row and counts accepted and rejected rows.

Understanding Pseudocode: processing a file of records for insertion in QCE Digital Solutions Unit 2

What it does: Fields from a file are text with possible extra spaces, so trim() cleans them and toInteger() converts the value so it can be range checked. The program creates the INSERT statement by joining fixed SQL text with the cleaned values, with quotes around text values. The loop takes each line from the file one at a time, so the file comes after 'in' and the line is the loop variable. Clean the data first. Splitting, trimming and converting the fields means validation checks the real values, not stray spaces. Bad rows must not reach the database. Build the INSERT inside the 'if valid' branch only.

Common mistakes: A counter must start at 0. Starting rejected at 1 means the final count is always one too high. It should be rejected ← 0. line is the whole text of the row. The separate values are in the fields array after split(), so it should be trim(fields[0]).

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
Which loop header correctly reads every line of a file of student records in pseudocode?
for each file in linefor each line in file
Matches
for each line in file fields <- split(line, ",") mark <- fields[2] if mark >= 0 AND mark <= 100 then
↓
The field is still text and was not trimmed or converted to a number, so the range check compares text with numbers and may fail.
Applying
Meanings
Term
for each line in file
Meaning
A loop that reads each line, splits it into fields, trims and validates them, then builds an INSERT statement for each good row and counts accepted and rejected rows.
Practice
accepted ← 0
Testing
Gaps
fields ← (line, ",")
Check Answer
Spots
1accepted ← 0
2rejected ← 1

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

The 'Firefly' badge — Night 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 header correctly reads every line of a file of student records in pseudocode?
A. for each file in line
B. for each line in file ✓
The loop takes each line from the file one at a time, so the file comes after 'in' and the line is the loop variable.
2. A line reads "7, Ava ,Red". What should happen before the fields are validated?
A. Split the line, then trim each field and convert the types ✓
B. Build the INSERT statement straight away
Clean the data first. Splitting, trimming and converting the fields means validation checks the real values, not stray spaces.
3. Which line correctly turns the text field fields[3] into a whole number?
A. points <- fields[3] + "number"
B. points <- INTEGER(TRIM(fields[3])) ✓
Trim the spaces off the text, then convert it to an integer so numeric checks such as range checks work.
4. When should the program build an INSERT statement for a row?
A. Only when the row passes validation ✓
B. For every line, even if a field is invalid
Bad rows must not reach the database. Build the INSERT inside the 'if valid' branch only.
5. Which pseudocode correctly builds the SQL text for a valid row?
A. sql <- "INSERT INTO students VALUES (" + id + ", '" + name + "')" ✓
B. sql <- INSERT INTO students VALUES id, name
The SQL is built as a string by joining text and variables. Text values go inside single quotes in the SQL.

Match the Pairs

fields <- split(line, ",") → Breaks one line of the file into a list of separate field values at each comma.
accepted <- accepted + 1 → Counts one more row that passed validation and had an INSERT statement built.
Trim spaces off a field and convert it to a number before checking it → mark <- toInteger(trim(fields[2]))
Build the SQL text for a good row by joining the cleaned field values into an INSERT statement → sql <- "INSERT INTO students VALUES (" + id + ", '" + name + "')"
for each line in file fields <- split(line, ",") mark <- fields[2] if mark >= 0 AND mark <= 100 then → The field is still text and was not trimmed or converted to a number, so the range check compares text with numbers and may fail.
if valid then accepted <- accepted + 1 else accepted <- accepted + 1 → The else branch adds to accepted instead of rejected, so rejected rows are counted as accepted and the rejected total never changes.

Key Term

for each line in file
A loop that reads each line, splits it into fields, trims and validates them, then builds an INSERT statement for each good row and counts accepted and rejected rows.

Fill in the Blanks

1. for each line in file fields ← split(line, ",")
Hint: Type the function name that breaks a line into a list of fields.
2. name ← trim(fields[0]) age ← integer(fields[1])
Hint: Type the function name that removes extra spaces from the ends of a text field.
3. age ← integer(trim(fields[1]))
Hint: Type the function name that converts the cleaned text into a whole number.
4. sql ← "INSERT INTO students VALUES ('" sql ← sql + name + "')"
Hint: Type the operator that joins pieces of text together to build the SQL statement.
5. if valid then accepted ← accepted + 1 else rejected ← rejected + 1
Hint: Type the counter variable that tracks rows that failed validation.

Spot the Error

Question 1
A accepted ← 0
B rejected ← 1
C for each line in file
D fields ← split(line, ",")
E accepted ← accepted + 1
Line B — A counter must start at 0. Starting rejected at 1 means the final count is always one too high. It should be rejected ← 0.
Question 2
A for each line in file
B fields ← split(line, ",")
C name ← trim(line[0])
D mark ← toInteger(fields[1])
Line C — line is the whole text of the row. The separate values are in the fields array after split(), so it should be trim(fields[0]).
Question 3
A for each line in file
B fields ← split(line, ",")
C age ← toString(fields[1])
D if age < 0 OR age > 120 then
E rejected ← rejected + 1
Line C — The age must be converted to a number so it can be range checked. toString() keeps it as text, so it should be toInteger(fields[1]).
Question 4
A for each line in fields
B fields ← split(line, ",")
C id ← toInteger(fields[0])
D print id
Line A — The loop must go through every line in the file. The fields array does not exist until a line has been split inside the loop, so it should be for each line in file.
Question 5
A accepted ← 0
B for each line in file
C fields ← split(line, ",")
D if length(fields) ← 3 then
E accepted ← accepted + 1
Line D — The ← symbol assigns a value. A condition needs the comparison operator =, so it should be if length(fields) = 3 then.

Student with a class code? Join your class →