Pseudocode in the exam

Reading and writing exam pseudocode and flowcharts, and translating both ways.

F13.3Exam preparationGCSE15 min

Do this lesson in the simulator

Exam algorithms are not written in Python. Each board has its own way of writing code on paper, and you have to read it and, often, write it. The good news: it is the same ideas you already use, in slightly different clothes. This lesson translates both ways.

The same algorithm, three ways

Python, as you have been writing it:

total = 0
for i in range(1, 6):
    total = total + i
print(total)

Exam-style pseudocode, as a paper will show it:

total = 0
FOR i = 1 TO 5
    total = total + i
NEXT i
OUTPUT total

The differences are worth learning because they are where marks go:

Python Exam pseudocode
print(x) OUTPUT x
x = input() x = USERINPUT or INPUT x
for i in range(1, 6): FOR i = 1 TO 5NEXT i (the end value is included)
while x < 5: WHILE x < 5ENDWHILE
if / elif / else IFELSE IFELSEENDIF
indentation ends a block a word ends it: NEXT, ENDWHILE, ENDIF, ENDFUNCTION
def f(x):return FUNCTION f(x)RETURNENDFUNCTION
len(s), s.upper() LEN(s), s.upper varies by board

The trap is FOR i = 1 TO 5, which runs five times with i finishing at 5, while range(1, 5) stops at 4.

Reading it

You will be given pseudocode and asked what it outputs, what is wrong with it, or to change it. Read it exactly as written: do not assume it means what you would have written.

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

# FOR i = 1 TO 3
#     OUTPUT i * i
# NEXT i
for i in range(1, 4):          # 1 TO 3 includes 3, so range stops at 4
    print(i * i)

Run this in the simulator

Writing it

If the question says "write an algorithm", you may normally answer in pseudocode, in your board's reference language, or in a real language, as long as it is clear and consistent. Whatever you choose:

  • use meaningful names: total, not t;
  • indent the inside of loops and ifs, even on paper;
  • close what you open, or indent clearly enough that the examiner can see where a block ends;
  • write the check before the loop body if it is a while loop;
  • do not worry about a missing colon: examiners mark the logic, not your typing.

Flowcharts

The other way a paper shows an algorithm:

Shape Means
rounded box start or stop
rectangle a process: a calculation or an assignment
parallelogram input or output
diamond a decision, with a labelled arrow out of each side

A loop is an arrow that goes back to an earlier box. Follow the arrows exactly, and write down the variables as you go, as in lesson F13.2.

Task: pseudocode into Python

Turn the pseudocode in the comment into a working program. It asks for a number of steps, drives that many 10 cm steps while counting the total distance, and prints the total. input() is answered for you with 4.

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

# steps = USERINPUT
# total = 0
# FOR i = 1 TO steps
#     MOVE FORWARD 10
#     total = total + 10
#     OUTPUT "step " + i + ": " + total
# NEXT i
# OUTPUT "total: " + total

Challenges

  1. Rewrite the same algorithm as a WHILE loop in pseudocode, then in Python.
  2. Write the pseudocode for a program that finds the largest of three numbers.
  3. Draw the flowchart for the task above.