Pseudocode conventions are the agreed way of writing an algorithm: ← for assignment, capital letters for keywords, indentation for blocks. They appear in any exam question that asks you to read, complete or write an algorithm.
This lesson is the foundation of pseudocode for the applicable syllabus. Check the final lesson of the module for how to confirm which notation applies to your exam year.
What are the main conventions?
Cambridge pseudocode uses a small set of patterns. Learn them as a toolkit.
| Idea | Pseudocode |
|---|---|
| Declare a variable | DECLARE Total : INTEGER |
| Assign a value | Total ← 0 |
| Input and output | INPUT Mark, OUTPUT Total |
| Selection | IF ... THEN ... ELSE ... ENDIF |
| Count-controlled loop | FOR Count ← 1 TO 5 ... NEXT Count |
| Pre-condition loop | WHILE ... DO ... ENDWHILE |
| Post-condition loop | REPEAT ... UNTIL ... |
| Comparison | =, <>, <, >, <=, >= |
Three habits make your answers readable. Write keywords in capitals, indent everything inside a structure, and close every structure with its ending keyword.
Worked example
Write pseudocode that reads three marks, then outputs their average. Test with 12, 15 and 9.
DECLARE Mark1, Mark2, Mark3 : INTEGER
DECLARE Average : REAL
INPUT Mark1
INPUT Mark2
INPUT Mark3
Average ← (Mark1 + Mark2 + Mark3) / 3
OUTPUT Average
Trace with inputs 12, 15, 9:
| Line | Mark1 | Mark2 | Mark3 | Average |
|---|---|---|---|---|
| after the INPUT lines | 12 | 15 | 9 | |
| Average ← … | 12 | 15 | 9 | 12 |
The sum is 12 + 15 + 9 = 36, and 36 ÷ 3 = 12. The output is 12. Check again: 12 × 3 = 36, so the average is right.
The brackets matter. Without them, the line would divide only Mark3 by 3 and add the rest, giving 12 + 15 + 3 = 30.
The mistake to watch for
A common slip is to carry over a programming language’s symbols.
Mistaken line:
Average = (Mark1 + Mark2 + Mark3) / 3In the convention,
=is a comparison. This line asks “are these equal?”, rather than storing the result.
The correction is Average ← (Mark1 + Mark2 + Mark3) / 3. Python and many other languages do use = for storing, which is why the habit transfers so easily. When an exam asks for pseudocode, switch back to ← deliberately.
Check yourself
1. Write one line of pseudocode that stores 20 in a variable called Limit.
Show answer
Limit ← 20
2. What is wrong with this loop?
FOR Count ← 1 TO 3
OUTPUT Count
Show answer
It has no closing keyword. Add NEXT Count after the indented line. With it, the loop outputs 1, 2 and 3.
3. Using the three-mark example, what would the average be for 10, 10 and 4?
Show answer
The sum is 24, and 24 ÷ 3 = 8. The output is 8.
Where this leads next
Next, practise reading a line correctly in tracing assignment and comparison distinctly. The restricted pseudocode trace trainer is useful for checking a trace once you have done it by hand.
Style habits are hard to spot in your own writing. Teachers on our online one-to-one Computer Science tuition can read your answers as an examiner would and point out exactly which habit to fix.