You are on page 1of 9

OCR Exam Reference Language

OCR Exam Reference Language


The following code is a breakdown of OCR’s official Exam Reference Language. Although it has been
formally defined by OCR, it is still considered pseudocode. By this we mean it is not written to match
one language but has many similarities with Python.

All examination questions will be written using this language for clarity and consistency, so you
should get familiar with it and attempt to use this standard whenever possible.

Logical operators
AND OR NOT

Example
while x<=5 AND flag==false

Comparison operators
== Equal to
!= Not equal to
< Less than
<= Less than or equal to
> Greater than
>= Greater than or equal to

Arithmetic operators
+ Addition e.g. x=6+5 gives 11
- Subtraction e.g. x=6-5 gives 1
* Multiplication e.g. x=12*2 gives 24
/ Division e.g. x=12/2 gives 6
MOD Modulus e.g. 12MOD5 gives 2
DIV Quotient e.g. 17DIV5 gives 3
^ Exponentiation e.g. 3^4 gives 81

This resource has been reproduced from the J277 – OCR GCSE Specification Version 1 © OCR 2019. Page 1
It has been adapted with additional explanations and code examples for you to print out to give to
Craig’n’Dave students as a handy reference guide to use throughout the course.
OCR Exam Reference Language

Comments
Concept Keyword(s) / Symbols
Comment //

print(“Hello World”) //This is a comment

Taking input from the user/keyboard


Concept Keyword(s) / Symbols
Input input(…)

variable=input(prompt to user)

Example
name=input(“Please enter your name”)

Outputting to the screen


Concept Keyword(s) / Symbols
Output print(…)

print(string)
print(variable)

Example
print(“hello”)
print(myAge)

This resource has been reproduced from the J277 – OCR GCSE Specification Version 1 © OCR 2019. Page 2
It has been adapted with additional explanations and code examples for you to print out to give to
Craig’n’Dave students as a handy reference guide to use throughout the course.
OCR Exam Reference Language

Variables, constants and assignments


Concept Keyword(s) / Symbols
Assignment =
Constants const
Global variales global

Variables and constants are assigned using the = operator.

x=3
name=”Bob”

Variables and constants are declared the first time a value is assigned. They assume the data type of
the value they are given.

Variables and constants that are declared inside a function or procedure are local to that subroutine.
Variables in the main program can be made global with the keyword global.

global userid = 123

Variables in the main program can be made constant with the keyword const

const vat = 20

Casting
Concept Keyword(s) / Symbols
Converting to str()
another data type int()
float()
real()
bool()

Variables can be typecast using the following functions.

str(3) returns “3”


int(“3”) returns 3
float(“3.14”) returns 3.14
real(“4.52”) returns 4.52
bool(“True”) returns TRUE

This resource has been reproduced from the J277 – OCR GCSE Specification Version 1 © OCR 2019. Page 3
It has been adapted with additional explanations and code examples for you to print out to give to
Craig’n’Dave students as a handy reference guide to use throughout the course.
OCR Exam Reference Language

Iteration: count-controlled
FOR loop
Concept Keyword(s) / Symbols
FOR loop for… to …

next …

for i = 0 to 7
print(“Hello”)
next i

Will print hello 8 times (0-7 inclusive).

FOR loop with step


Concept Keyword(s) / Symbols
FOR loop for… to … step…

next …

for i = 2 to 10 step 2
print(i)
next i

This will print the even numbers from 2 to 10 inclusive.

Iteration: condition-controlled
WHILE loop
Concept Keyword(s) / Symbols
WHILE loop while…

endwhile…

while answer!=”computer”
answer=input(“What is the password?”)
endwhile

DO WHILE loop
Concept Keyword(s) / Symbols
DO WHILE loop do…

until…

do
answer=input(“What is the password?”)
until answer==“computer”

This resource has been reproduced from the J277 – OCR GCSE Specification Version 1 © OCR 2019. Page 4
It has been adapted with additional explanations and code examples for you to print out to give to
Craig’n’Dave students as a handy reference guide to use throughout the course.
OCR Exam Reference Language

Selection
Selection will be carried out with if/then/else and switch/case.

IF-THEN-ELSE
Concept Keyword(s) / Symbols
IF-THEN-ELSE if… then
elseif … then
else
endif

if entry==“a” then
print(“You selected A”)
elseif entry==“b” then
print(“You selected B”)
else
print(“Unrecognised selection”)
endif

CASE SELECT or SWITCH


Concept Keyword(s) / Symbols
CASE SELECT or switch…:
SWITCH case…:
case…:
default:
endswitch

switch entry:
case “A”:
print(“You selected A”)
case “B”:
print(“You selected B”)
default:
print(“Unrecognised selection”)
endswitch

This resource has been reproduced from the J277 – OCR GCSE Specification Version 1 © OCR 2019. Page 5
It has been adapted with additional explanations and code examples for you to print out to give to
Craig’n’Dave students as a handy reference guide to use throughout the course.
OCR Exam Reference Language

String handling
To get the length of a string:
Concept Keyword(s) / Symbols
String length .length

stringname.length

To get a substring:
Concept Keyword(s) / Symbols
Substrings .substring(x, i)

stringname.subString(startingPosition, numberOfCharacters)
NB The string will start with the 0 th character.

Converting cases:
Concept Keyword(s) / Symbols
Uppercase .upper
Lowercase .lower

stringname.upper
stringname.lower

Ascii conversion:
Concept Keyword(s) / Symbols
ASCII Converstion ASC(…)
CHR(…)

ASC(character)
CHR(asciinumber)

Example
someText=“Computer Science”
print(someText.length)
print(someText.substring(3,3))

Will display
16
Put

This resource has been reproduced from the J277 – OCR GCSE Specification Version 1 © OCR 2019. Page 6
It has been adapted with additional explanations and code examples for you to print out to give to
Craig’n’Dave students as a handy reference guide to use throughout the course.
OCR Exam Reference Language

Subprograms
Concept Keyword(s) / Symbols
Procedure procedure name(…)
endprocedure
Calling a procedure(parameters)
procedure
Function function name(…)

return …
endfunction
Calling a function function(parameters)

function triple(number)
return number*3
endfunction

Called from main program


y=triple(7)
procedure greeting(name)
print(“hello”+name)
endprocedure

Called from main program


greeting(“Hamish”)

Arrays and lists


Concept Keyword(s) / Symbols
Declaration array names[…]
array gameboard[…,…]
Assignment names[…] = …
gameboard[…,…] = …

Arrays will be 0 based and declared with the keyword array.

array names[5]
names[0]=”Ahmad”
names[1]=”Ben”
names[2]=”Catherine”
names[3]=”Dana”
names[4]=”Elijah”
print(names[3])

Example of 2D array:
array board[8,8]
board[0,0]=“rook”

This resource has been reproduced from the J277 – OCR GCSE Specification Version 1 © OCR 2019. Page 7
It has been adapted with additional explanations and code examples for you to print out to give to
Craig’n’Dave students as a handy reference guide to use throughout the course.
OCR Exam Reference Language

Reading to and writing from files


Concept Keyword(s) / Symbols
Open open(…)
Close .close()
Read line .readLine()
Write line .writeLine(…)
End of file .endOfFile()
Create a new file newFile()

To open a file to read or write to it open is used. We then use writeLine to write a line to the file and
readLine to return a line of text from the file.
The following program makes x the first line of sample.txt

myFile = open(“sample.txt”)
x = myFile.readLine()
myFile.close()

endOfFile() is used to determine the end of the file. The following program will print out the
contents of sample.txt

myFile = open(“sample.txt”)
while NOT myFile.endOfFile()
print(myFile.readLine())
endwhile
myFile.close()

In the program below, hello world is made the contents of sample.txt (any previous contents are
overwritten).

myFile = open(“sample.txt”)
myFile.writeLine(“Hello World”)
myFile.close()

To create a new file called “myNewFile.txt”

newFile = (“myNewFile.txt”)

The file would then need to be opened using the above command for Open.

Random numbers
Concept Keyword(s) / Symbols
Random number Random(…,…)

myVariable = random(1,6)

Creates a random integer between 1 and 6 inclusive.

myVariable = random(-5.0,5.0)

Creates a random real between -5.0 and 5.0 inclusive.

This resource has been reproduced from the J277 – OCR GCSE Specification Version 1 © OCR 2019. Page 8
It has been adapted with additional explanations and code examples for you to print out to give to
Craig’n’Dave students as a handy reference guide to use throughout the course.
OCR Exam Reference Language

This resource has been reproduced from the J277 – OCR GCSE Specification Version 1 © OCR 2019. Page 9
It has been adapted with additional explanations and code examples for you to print out to give to
Craig’n’Dave students as a handy reference guide to use throughout the course.

You might also like