Pseudocode and Trace Tables
The exact style of pseudocode Cambridge uses in exams, and how to trace an algorithm by hand.
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.
//This is not Python — the exam has its own style
Assignment uses a left arrow, keywords are capitalised, and every block is explicitly closed. You must be able to both READ and WRITE it.
DECLARE Count : INTEGER
DECLARE Name : STRING
Count ← 0
OUTPUT "What is your name?"
INPUT Name
IF Count > 10 THEN
OUTPUT "Too many"
ELSE
OUTPUT "Fine"
ENDIF
CASE OF Grade
'A' : OUTPUT "Excellent"
'B' : OUTPUT "Good"
OTHERWISE OUTPUT "See me"
ENDCASE
FOR i ← 1 TO 10
OUTPUT i
NEXT i
WHILE Count < 5 DO
Count ← Count + 1
ENDWHILE
REPEAT
Count ← Count - 1
UNTIL Count = 0//The five standard routines
Almost every Paper 2 question is built from these. Learn the shape of each.
TOTAL Total ← 0 (before the loop!)
Total ← Total + Number
COUNT Count ← 0
Count ← Count + 1
MAXIMUM Max ← first value (NOT 0)
IF Number > Max THEN Max ← Number
MINIMUM Min ← first value
IF Number < Min THEN Min ← Number
AVERAGE Average ← Total / Count//Trace tables — work mechanically
One column per variable, plus one for OUTPUT. Add a row every time any variable changes. Do not try to guess the answer: the marks are for the working, and guessing is how people go wrong.
X ← 10
WHILE X > 6 DO
X ← X - 2
OUTPUT X
ENDWHILE
X | OUTPUT
10 |
8 | 8
6 | 6 <- loop now stops, 6 is not > 6CHECK 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?