Exam Reference Language
What Exam Reference Language is
Exam Reference Language (ERL) is OCR's own way of writing code. Every piece of code in the exam papers is printed in ERL, so you must be able to read it. When you write code, you can use ERL or Python.
ERL looks a lot like Python, but its keywords are different: blocks end with words such as endif and endwhile, rather than by indentation alone.
This sub-topic puts every construct from the specification's ERL section in one place, each beside its Python equivalent.
Operators that differ:
| Meaning | ERL | Python |
|---|---|---|
| Remainder (modulo) | MOD | % |
| Whole-number division (quotient) | DIV | // |
| Power (exponent) | ^ | ** |
| And, or, not | AND, OR, NOT | and, or, not |
The others are the same in both: ==, !=, <, <=, >, >=, +, -, * and /. So 17 MOD 5 is 2, 17 DIV 5 is 3, and 2 ^ 3 is 8.
Key terms
Comments, variables, input and output
// Comments start with two slashes
const vat = 0.2
name = input("What is your name?")
price = 50
print("Hello " + name)
print(price * (1 + vat))
- Assignment uses
=in both. - A constant is made with
constin ERL. Python has no constants, so programmers write the name in capitals to show it shouldn't change. input()shows its message and returns what the user types, as a string;print()outputs.- A global variable, made with
globalin ERL, is shown under sub programs below.
Key terms
Casting
text = str(345)
whole = int("3")
decimal = float("4.52")
also_decimal = real("4.52")
answer = bool("True")
ERL has real() as well as float(): they do the same. Python only has float().
Common mistake
In Python, bool() of any text that isn't empty is True, so bool("False") is True. Compare the text instead: answer == "True".
Count-controlled loops
for i = 0 to 4
print(i)
next i
for i = 2 to 10 step 2
print(i)
next i
for i = 3 to 1 step -1
print(i)
next i
The first loop prints 0 to 4, the second the even numbers from 2 to 10, and the third counts down 3, 2, 1. In ERL, next i marks the end of the loop.
Remember
ERL's for i = 0 to 4 includes 4. Python's range(0, 5) stops before 5, so its end number is always one past the last value.
Condition-controlled loops
A while loop checks its condition before each pass, so it may never run. A do … until loop runs once before checking, and stops when its condition becomes true.
answer = ""
while answer != "yes"
answer = input("Ready?")
endwhile
do
answer = input("Ready?")
until answer == "yes"
Python has no do … until. The same effect comes from running the loop's body once before a while loop, with the opposite condition.
Selection
mark = int(input("Mark?"))
if mark >= 70 then
print("Distinction")
elseif mark >= 50 then
print("Pass")
else
print("Try again")
endif
day = input("Day?")
switch day:
case "Sat":
print("Weekend")
case "Sun":
print("Weekend")
default:
print("School day")
endswitch
ERL writes then after each condition and ends with endif; Python uses a colon and indentation, and elif for elseif. ERL's switch checks one variable against several values, with default for anything else. In Python, the usual equivalent is if, elif and else.
String handling
word = "Programming"
print(word.length)
print(word.substring(3, 4))
print(word.left(3))
print(word.right(4))
print(word.upper)
print(word.lower)
print("Pro" + "gram")
print(ASC("B"))
print(CHR(100))
These print 11, gram, Pro, ming, PROGRAMMING, programming, Program, 66 and d. Both count characters from 0. ERL's substring(3, 4) means start at 3 and take 4 characters; Python's slice [3:7] means start at 3 and stop before 7.
File handling
newFile("notes.txt")
file = open("notes.txt")
file.writeLine("First line")
file.writeLine("Second line")
file.close()
file = open("notes.txt")
while NOT file.endOfFile()
print(file.readLine())
endwhile
file.close()
- ERL makes a file with
newFile(), andwriteLine()always adds to the end of it. In Python,open()takes a mode:"w"makes a new file (or empties an old one),"a"adds to the end, and"r"reads. - Python's
write()doesn't start a new line, so add\n. - ERL checks
endOfFile(); in Python,readline()returns an empty string at the end. - Always close a file when you've finished with it.
Arrays
array scores[5]
array colours = ["Red", "Green", "Blue"]
scores[0] = 18
colours[2] = "Yellow"
print(colours[2])
array board[3, 3]
board[1, 2] = "X"
print(board[1, 2])
Both count from 0, so array scores[5] has positions 0 to 4. ERL arrays have a fixed length and hold one data type. Python uses lists, which can grow and mix types, but in the exam treat them like ERL arrays.
A 2D array is written board[1, 2] in ERL and board[1][2] in Python: row 1, column 2.
Sub programs
procedure greet(name)
print("Hello " + name)
endprocedure
function cube(number)
return number ^ 3
endfunction
greet("Sam")
answer = cube(2)
print(answer)
global score = 0
procedure add_point()
score = score + 1
endprocedure
add_point()
print(score)
A procedure does a job; a function returns a value, which the program should store or use. Python writes both with def.
A global variable can be used anywhere in the program. In Python, a variable made outside every function is global, but a function must say global before it can change one.
Key terms
Random numbers
roll = random(1, 6)
print(roll)
amount = random(-1.0, 10.0)
print(amount)
random(1, 6) gives a whole number from 1 to 6, including both. With real numbers, such as random(-1.0, 10.0), it gives a real number in that range. Python needs import random first, then uses random.randint() for whole numbers and random.uniform() for real ones.