Pseudocode and the exam reference language

Reading and writing your board's pseudocode, and translating it into Python.

F5.3AlgorithmsGCSE15 min

Do this lesson in the simulator

Pseudocode is a way of writing an algorithm that looks like code but is not tied to any one programming language. It is quicker to write than a flowchart and closer to a program, so it is how most algorithms are shared on paper. Exam boards each have their own style, and in written exams you read and write theirs. This lesson is about moving between pseudocode and Python.

The same algorithm, three ways

The robot steps towards the wall and reports whether it took an even or odd number of steps. Switch between the exam boards in the box below to see it in each board's pseudocode, and compare with Python.

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

steps = 0
while distance() > 30:
    forward(50, distance=10)
    steps = steps + 1
if steps % 2 == 0:
    print("even number of steps: " + str(steps))
else:
    print("odd number of steps: " + str(steps))

Run this in the simulator

forward(10) in the pseudocode is not a real command: pseudocode lets you invent a clear name for a step and leave the details to whoever writes the program. That is the point of it.

Translating line by line

Most lines translate one for one. The differences to watch:

Idea OCR Exam Reference Language AQA pseudo-code Python
assign x = 3 x ← 3 x = 3
equal to == = ==
not equal != !=
remainder MOD MOD %
whole-number divide DIV DIV //
output print(x) OUTPUT x print(x)
input x = input("prompt") x ← USERINPUT x = input("prompt")
end of a block endif, endwhile, next i ENDIF, ENDWHILE, ENDFOR indentation
count-controlled loop for i = 1 to 5 (includes 5) FOR i ← 1 TO 5 (includes 5) for i in range(1, 6)
a procedure procedure name(x) ... endprocedure SUBROUTINE name(x) ... ENDSUBROUTINE def name(x):

The count-controlled loop is where most translation mistakes happen: both pseudocodes include the end number, and Python's range does not.

Writing your own pseudocode

When an exam asks for pseudocode, it wants a clear algorithm, not perfect syntax. Examiners generally accept any sensible pseudocode, provided:

  • each step is clear and unambiguous,
  • the structure (sequence, selection, iteration) is shown with indentation and closing words,
  • variables are named and given values before they are used.

Writing the pseudocode first, then the Python underneath it, is also a good way to plan a program before touching the keyboard.

Python as pseudocode

Python reads so much like pseudocode that people sometimes call it executable pseudocode. That makes translation easy in one direction, but it also makes it tempting to use Python-only features in an exam answer, such as range with its excluded end, +=, or list slicing, where the board's own form would be clearer to the examiner.

Task: steps from pseudocode

Translate the algorithm at the top of this lesson into Python, from the pseudocode for your board. The robot should step towards the wall in 10 cm steps while it is more than 30 cm away, then print whether it took an even or odd number of steps, in exactly the form even number of steps: 4.

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

steps = 0

Challenges

  1. Write pseudocode for the password guard from lesson F5.2, in your board's style.
  2. Translate this OCR loop into Python, and check it prints the same numbers: for i = 2 to 10 step 2 then print(i) then next i.
  3. Find three things in a Python program of yours that would need changing to be good pseudocode for your board.