OCR GCSE Computer Science: Pseudocode Reference Language Cheat Sheet
OCR GCSE Computer Science: Pseudocode Reference Language Cheat Sheet
OCR GCSE Computer Science (J277) uses a prescribed pseudocode syntax called the Exam Reference Language (ERL). In the exam, you will be asked to read, trace, and write code using this syntax. While it looks similar to Python, there are important differences that can cost you marks if you get them wrong.
This guide covers every part of the OCR pseudocode syntax you need to know, compares each construct with its Python equivalent, highlights the traps that catch students out, and gives you exercises to practise.
Variables and Data Types
In OCR pseudocode, you do not need to declare variable types. You simply assign a value, and the data type is inferred from the value assigned.
OCR Pseudocode:
name = "Alice"
age = 16
score = 85.5
passed = true
Python equivalent:
name = "Alice"
age = 16
score = 85.5
passed = True
The key difference: OCR pseudocode uses lowercase true and false for Boolean values, while Python uses capitalised True and False.
Common Data Types
| Data Type | Example in OCR Pseudocode | Description |
|---|---|---|
| Integer | count = 10 | Whole numbers |
| Real (float) | price = 4.99 | Decimal numbers |
| Boolean | found = false | true or false |
| Character | letter = 'A' | A single character |
| String | word = "Hello" | A sequence of characters |
Casting (Type Conversion)
OCR pseudocode uses casting functions to convert between types:
str(42) // converts integer 42 to string "42"
int("42") // converts string "42" to integer 42
float("3.14") // converts string "3.14" to real 3.14
bool("true") // converts string to Boolean
Python equivalent:
str(42)
int("42")
float("3.14")
# Python does not have a direct bool("true") equivalent
Input and Output
OCR Pseudocode:
name = input("Enter your name: ")
print("Hello " + name)
Python equivalent:
name = input("Enter your name: ")
print("Hello " + name)
These are identical in both languages. Note that input() in OCR pseudocode always returns a string, just as it does in Python. If you need a number, you must cast it:
age = int(input("Enter your age: "))
Arithmetic and Comparison Operators
Arithmetic Operators
| Operator | Meaning | OCR Example | Python Equivalent |
|---|---|---|---|
+ | Addition | x = 5 + 3 | x = 5 + 3 |
- | Subtraction | x = 10 - 4 | x = 10 - 4 |
* | Multiplication | x = 6 * 7 | x = 6 * 7 |
/ | Division | x = 20 / 4 | x = 20 / 4 |
MOD | Modulus (remainder) | x = 10 MOD 3 | x = 10 % 3 |
DIV | Integer division | x = 10 DIV 3 | x = 10 // 3 |
^ | Exponentiation | x = 2 ^ 3 | x = 2 ** 3 |
Pay close attention to MOD and DIV -- OCR uses words rather than symbols. 10 MOD 3 gives 1 (the remainder). 10 DIV 3 gives 3 (integer division, discarding the remainder).
Comparison Operators
| Operator | Meaning |
|---|---|
== | Equal to |
!= | Not equal to |
< | Less than |
> | Greater than |
<= | Less than or equal to |
>= | Greater than or equal to |
Critical distinction: = is used for assignment (giving a variable a value). == is used for comparison (checking if two values are equal). Confusing these is one of the most common mistakes in the exam.
score = 50 // assignment: score is now 50
if score == 50 // comparison: is score equal to 50?
Boolean Operators
OCR pseudocode uses AND, OR, and NOT:
if age >= 18 AND hasTicket == true then
print("Entry allowed")
endif
Python equivalent:
if age >= 18 and hasTicket == True:
print("Entry allowed")
Selection (If / Elseif / Else / Switch)
If Statements
OCR Pseudocode:
if score >= 50 then
print("Pass")
elseif score >= 40 then
print("Near miss")
else
print("Fail")
endif
Python equivalent:
if score >= 50:
print("Pass")
elif score >= 40:
print("Near miss")
else:
print("Fail")
Key differences from Python:
- OCR uses
thenafter the condition - OCR uses
elseif(one word), Python useselif - OCR requires
endifto close the block -- Python uses indentation alone - Forgetting
endifis one of the most common mark-losing mistakes in the exam
Switch / Case Statements
OCR Pseudocode:
switch day:
case "Monday":
print("Start of the week")
case "Friday":
print("Nearly the weekend")
default:
print("Midweek")
endswitch
Python equivalent:
Python 3.10+ has match/case, but for GCSE purposes, the equivalent is typically an if/elif/else chain:
if day == "Monday":
print("Start of the week")
elif day == "Friday":
print("Nearly the weekend")
else:
print("Midweek")
Remember to close with endswitch.
Iteration (Loops)
OCR pseudocode has three types of loop. You must know when to use each one.
FOR Loop (Count-Controlled)
Use when you know in advance how many times the loop should run.
OCR Pseudocode:
for i = 0 to 4
print(i)
next i
This prints: 0, 1, 2, 3, 4 (five values -- the range is inclusive of both ends).
Python equivalent:
for i in range(5):
print(i)
Important difference: OCR's for i = 0 to 4 includes 4. Python's range(5) goes from 0 to 4 as well, but if you wrote range(4) thinking it matched 0 to 4, you would only get 0, 1, 2, 3. The OCR range is inclusive on both ends.
You can also use a step value:
for i = 0 to 10 step 2
print(i)
next i
This prints: 0, 2, 4, 6, 8, 10.
WHILE Loop (Condition-Controlled, Pre-Check)
Use when the loop should keep running as long as a condition is true. The condition is checked before each iteration, so the loop body may never execute if the condition is false from the start.
OCR Pseudocode:
while answer != "yes"
answer = input("Continue? ")
endwhile
Python equivalent:
while answer != "yes":
answer = input("Continue? ")
Close with endwhile in OCR pseudocode.
DO...UNTIL Loop (Condition-Controlled, Post-Check)
Use when the loop body must execute at least once. The condition is checked after each iteration.
OCR Pseudocode:
do
password = input("Enter password: ")
until password == "secret123"
Python equivalent (no direct equivalent):
while True:
password = input("Enter password: ")
if password == "secret123":
break
Python does not have a native do...until loop, which is why OCR students sometimes forget this structure exists. In the exam, if a question requires a loop that must run at least once, a do...until loop is usually the cleanest answer.
Arrays
OCR Pseudocode:
array names[5]
names[0] = "Alice"
names[1] = "Bob"
names[2] = "Charlie"
print(names[0])
Python equivalent:
names = [""] * 5
names[0] = "Alice"
names[1] = "Bob"
names[2] = "Charlie"
print(names[0])
Key points:
- OCR arrays are zero-indexed (the first element is at index 0)
- You declare an array with
array variableName[size] - Accessing an index outside the array bounds would cause an error
Two-Dimensional Arrays
array grid[3,3]
grid[0,0] = "X"
grid[1,1] = "O"
Python equivalent:
grid = [["" for _ in range(3)] for _ in range(3)]
grid[0][0] = "X"
grid[1][1] = "O"
In OCR pseudocode, 2D array elements are accessed with grid[row,col] (comma-separated within one set of brackets), whereas Python uses grid[row][col] (two sets of brackets).
String Operations
OCR pseudocode includes built-in string functions:
| Function | Purpose | Example | Result |
|---|---|---|---|
stringName.length | Returns the length of the string | "Hello".length | 5 |
stringName.substring(start, length) | Extracts part of a string | "Hello".substring(1,3) | "ell" |
stringName.upper | Converts to uppercase | "hello".upper | "HELLO" |
stringName.lower | Converts to lowercase | "HELLO".lower | "hello" |
ASC(character) | Returns ASCII value | ASC("A") | 65 |
CHR(integer) | Returns character from ASCII value | CHR(65) | "A" |
Python equivalents:
len("Hello") # 5
"Hello"[1:4] # "ell"
"hello".upper() # "HELLO"
"HELLO".lower() # "hello"
ord("A") # 65
chr(65) # "A"
Note the differences: OCR uses .length (a property), Python uses len() (a function). OCR's .substring(1,3) takes a start position and a length, while Python's slice [1:4] takes a start and end position. This catches many students out.
File Handling
OCR pseudocode includes file operations:
Reading from a file:
myFile = openRead("data.txt")
line1 = myFile.readLine()
myFile.close()
Writing to a file:
myFile = openWrite("output.txt")
myFile.writeLine("Hello, World!")
myFile.close()
Python equivalents:
# Reading
myFile = open("data.txt", "r")
line1 = myFile.readline()
myFile.close()
# Writing
myFile = open("output.txt", "w")
myFile.write("Hello, World!\n")
myFile.close()
You can also check for the end of a file using endOfFile():
myFile = openRead("data.txt")
while NOT myFile.endOfFile()
print(myFile.readLine())
endwhile
myFile.close()
Always remember to close files after use.
Subroutines: Functions and Procedures
OCR distinguishes between functions (which return a value) and procedures (which do not).
Functions
function calculateArea(length, width)
area = length * width
return area
endfunction
// Calling the function:
result = calculateArea(5, 3)
print(result)
Procedures
procedure displayMessage(name)
print("Welcome, " + name + "!")
endprocedure
// Calling the procedure:
displayMessage("Alice")
Python equivalent:
def calculate_area(length, width):
area = length * width
return area
def display_message(name):
print("Welcome, " + name + "!")
Python uses def for both functions and procedures. OCR separates them with function/endfunction and procedure/endprocedure. In the exam, if the question asks you to write a function, make sure you include a return statement. If it asks for a procedure, do not return a value.
Scope: Local and Global Variables
Variables declared inside a subroutine are local -- they only exist within that subroutine. Variables declared outside any subroutine are global -- they can be accessed from anywhere in the program.
global totalScore = 0
procedure addScore(points)
totalScore = totalScore + points
endprocedure
In general, using local variables and passing values through parameters is better practice than relying on global variables. Examiners often ask why -- the answer is that local variables reduce the risk of unintended side effects and make code easier to debug and maintain.
Common Exam Traps
These are the mistakes that examiners report seeing most frequently. Avoid them and you will pick up marks that many students lose.
-
Forgetting
endif,endwhile,endfunction,endprocedure, orendswitch. Every opening keyword must have its matching closing keyword. Get into the habit of writing the closing keyword immediately after the opening one, then filling in the body. -
Using
=instead of==in conditions.=is assignment.==is comparison. Writingif x = 5 thenwhen you meanif x == 5 thenis technically incorrect in OCR pseudocode. -
Array indexing errors. OCR arrays are zero-indexed. An array declared as
array items[5]has valid indices 0, 1, 2, 3, 4. Accessingitems[5]would be out of bounds. -
Confusing
.substring(start, length)with Python slicing."Hello".substring(1, 3)returns"ell"(starting at index 1, taking 3 characters). Python's"Hello"[1:3]returns"el"(from index 1 up to but not including index 3). Different behaviour, same-looking numbers. -
Forgetting
thenafterif. In OCR pseudocode, everyifcondition must be followed bythen. Python does not usethen, so students who code primarily in Python often omit it. -
Writing Python syntax in a pseudocode question. If the question says "using pseudocode" or "using OCR Exam Reference Language," you must use OCR syntax. Writing
elifinstead ofelseif, orrange()instead offor i = 0 to n, will lose marks.
Practice Exercises
Test yourself by tracing through these pseudocode snippets. Work out what each one outputs before checking the answer.
Exercise 1: Trace the Output
x = 5
y = 3
while x > 0
x = x - y
y = y + 1
print(x)
endwhile
Think about it: What values do x and y take on each iteration? When does the loop end? What gets printed?
Iteration 1: x = 5 - 3 = 2, y = 3 + 1 = 4, prints 2 Iteration 2: x = 2 - 4 = -2, y = 4 + 1 = 5, prints -2 Loop ends because x (-2) is not greater than 0.
Output: 2, -2
Exercise 2: What Does This Function Return?
function mystery(word)
result = ""
for i = word.length - 1 to 0 step -1
result = result + word.substring(i, 1)
next i
return result
endfunction
print(mystery("code"))
Think about it: What does the loop do on each iteration? What is the final value of result?
The function reverses the string. Starting from the last character and working backwards, it appends each character to result.
- i = 3: result = "" + "e" = "e"
- i = 2: result = "e" + "d" = "ed"
- i = 1: result = "ed" + "o" = "edo"
- i = 0: result = "edo" + "c" = "edoc"
Output: edoc
Exercise 3: Find the Bug
array scores[5]
scores[0] = 72
scores[1] = 85
scores[2] = 63
scores[3] = 91
scores[4] = 78
total = 0
for i = 1 to 5
total = total + scores[i]
next i
average = total / 5
print(average)
Think about it: There are two bugs. Can you spot them both?
Bug 1: The loop starts at i = 1 instead of i = 0, so it skips scores[0] (which is 72).
Bug 2: The loop goes up to i = 5, but the highest valid index is 4. scores[5] is out of bounds and would cause an error.
Fix: Change for i = 1 to 5 to for i = 0 to 4.
Tips for Writing Pseudocode in the Exam
- Write the structure first, then fill in the detail. If you need a loop, write the
for/nextorwhile/endwhilepair first, then add the body. - Use meaningful variable names.
totalScoreis better thanx. It makes your code easier to follow and shows the examiner you understand good programming practice. - Indent your code. Proper indentation shows the structure of your program and makes it easier for the examiner to follow your logic. It also demonstrates your understanding of maintainability.
- Add comments if they help clarity. You can use
//for comments in OCR pseudocode. A short comment explaining a tricky section can help the examiner give you the benefit of the doubt. - Check your closing keywords. Before moving on from a question, scan your code and match every
ifwith anendif, everywhilewith anendwhile, and everyfunction/procedurewith its closing keyword.
Practise with LearningBro
Reading about pseudocode syntax is a good start, but the real skill comes from writing it repeatedly under exam conditions. You can practise OCR pseudocode questions, algorithm tracing, and programming challenges on LearningBro's OCR GCSE Computer Science courses. Working through questions topic by topic is the fastest way to build fluency with the Exam Reference Language so that it feels natural by the time you sit the exam.