Pseudocode and Trace Tables
The exam's own pseudocode layout, the standard methods (totalling, counting, maximum, minimum and average), trace tables, and finding and fixing errors.
WATCH IT EXPLAINED
EXPLORE IT IN 3D
The algorithm machine
A machine on the workbench runs the exam's pseudocode: boxes of input ride in, counters roll, a printer tapes out every OUTPUT and the trace table fills itself in on the board. Load it, find what's wrong, and make it give the right answers.
The exam's own notation
Cambridge prints pseudocode in one fixed style, and your answers should follow it.
| Rule | Example |
|---|---|
| Keywords in capitals | IF, OUTPUT |
| Assignment is an arrow | Price ← ChildPrice |
| Close every block | ENDIF, ENDWHILE, NEXT Count, UNTIL, ENDCASE |
| Names: letters and digits only, no spaces, a capital to start each word | TotalMarks |
A comment starts with // | // Works out the price |
Indenting: everything inside a block goes four spaces in. THEN and ELSE go on their own lines, only two spaces in.
Watch out:Be exact in a condition. Write Age < 12, never "Age is less than 12".
// Works out the price of a cinema ticket DECLARE Age : INTEGER DECLARE Price : INTEGER CONSTANT FullPrice ← 2000 CONSTANT ChildPrice ← 1000 OUTPUT "Enter your age" INPUT Age IF Age < 12 THEN Price ← ChildPrice ELSE Price ← FullPrice ENDIF OUTPUT "Price: ", Price
How to fill in a trace table
In a trace table you act as the computer, and write down every value, line by line.
A trace table records a dry run: you act as the computer and work through the algorithm one line at a time.
- Give each variable a column. Add an OUTPUT column.
- Write a value each time a variable is given one, even the same value again.
- Move down to a new row when the column you need already has a value in this row.
- Each time a loop condition is tested, work it out with the values you have now.
Remember:A user prompt is an output too, so it goes in the OUTPUT column. The loop test is where the loop stops.
X ← 10 WHILE X > 6 DO X ← X - 2 OUTPUT X ENDWHILE
Totalling
Set the total to 0 before the loop, and add each value to it inside the loop.
Totalling means adding up values as you go, like a shop till.
- Before the loop:
Total ← 0 - Inside the loop:
Total ← Total + Price - After the loop: output the total.
Watch out:If Total ← 0 is inside the loop, the total is wiped every time round. Only the last value survives.
Exam:Mark schemes give one mark for setting the total to 0 before the loop. They give another for outputting the total after the loop.
Counting
A counter starts at 0 and goes up by one each time a value passes the test.
Counting is like totalling, with one difference. A counter also starts at 0 before the loop, but it goes up by one each time: Count ← Count + 1.
Usually that line is inside an IF. Then only the values that pass the test are counted.
Read the condition exactly as the question states it:
| The question says | Write | Is 0 counted? |
|---|---|---|
| "below zero" | < 0 | no: 0 is not below zero |
| "zero or below" | <= 0 | yes |
Maximum and minimum
Start Max and Min with a value that cannot win by mistake, then compare every value with them.
There are two safe ways to start:
- Start the maximum and the minimum with the first value read. Then compare every later value with them.
- Start them with values outside anything possible. For marks from 0 to 100, use
Max ← -1andMin ← 101.
Watch out:Never start the minimum at 0 when no value can be below 0. Nothing is ever smaller, so Min stays 0 for ever.
Starting a maximum at 0 is only safe when no value can be negative.
Average
Average = total ÷ how many values, worked out once, after the loop.
An average is the total divided by how many values were added.
- Work it out once, after the loop. Never inside it.
- Don't know how many values will come? Keep reading until a value that means stop, such as -1. Count the values as they arrive.
- Check that the count is not 0 before you divide.
Example:ROUND(Total / Count, 1) gives the average to one decimal place.
Trace it, then say what it is for
After you trace an algorithm, read the OUTPUT column and say in one sentence what it does.
A question can ask for a trace table, then the algorithm's purpose (what it is for). Question 7 of 0478/22 in October/November 2024 did this.
- Complete the trace table.
- Read the OUTPUT column.
- Say in one sentence what the algorithm does for its user.
Watch out:In a REPEAT loop the test is at the end. So a value input inside the body is used once before the loop can stop.
Purpose: it takes the cost of each
purchase away from a balance of 10000,
refuses any purchase that costs more
than is left, and outputs the money left
when 0 is entered.Finding and fixing errors
To fix an error, give the line number and write the whole corrected line.
Error questions give numbered lines. They ask for the line number of each error and a correction.
Check in this order:
- the starting values
- the loop's start, end and step
- every comparison operator
- which variable is used
- block endings, such as ENDWHILE where ENDIF is needed
- an OUTPUT inside the loop that belongs after it
Exam:Give the line number. Then write out the corrected line in full, so the examiner sees exactly what changes.
Writing an algorithm from a description
Build every algorithm in the same order: inputs and outputs, variables, loop, processing, outputs.
- List the inputs and outputs.
- Declare the variables.
- Set totals and counters before the loop.
- Choose the loop: FOR when you know how many times; WHILE or REPEAT when you do not.
- Put a prompt before every INPUT, and a validation loop after it.
- Put the processing inside the loop.
- Put the outputs after the loop, each with a message.
Exam:The long scenario question gives marks for techniques that cover every requirement. It also gives marks for clear names, comments and a logical order.
Check your algorithm like an examiner
Before you move on, check your answer the way the examiner will, one mark point at a time.
Read your algorithm again and tick each point:
- Every variable has a value before it is used.
- Every loop can end.
- Every condition is exact:
<or<=, AND or OR. - There is a prompt before every INPUT.
- There is a message with every OUTPUT.
- Every block is closed: ENDIF, ENDWHILE, NEXT, UNTIL.
RUN THE PSEUDOCODE
Write the pseudocode your syllabus uses and press Run. Assignment is the arrow — type <- and it works the same as ←. Both syllabuses are accepted, so OUTPUT and PRINT both work; where a word differs from the one the exam prints, the runner says so.
Press Run to see what it doesTotal ← 0
FOR Count ← 1 TO N
Total ← Total + Count
NEXT Count
OUTPUT TotalSTARTN = 5
| Count | Total | OUTPUT |
|---|---|---|
One row per pass through the loop. Leave a cell blank if it does not change on that pass.
KEY TERMS
CHECK YOURSELF
1.In Cambridge pseudocode, which symbol assigns a value?
2.Where must a total be set to 0?
3.Trace: X ← 8, then WHILE X > 4 DO X ← X - 2, OUTPUT X. What is printed?
4.Which loop is guaranteed to run at least once?
5.Marks 62, 45, 88, 71 and 39 are checked with Min ← 0 set before the loop. What is Min at the end?
6.In a trace table, where does a user prompt such as "Enter mark 1" go?
7.A question says there is an error in line 07. What should your answer give?