OCR Reference Language Guide Cheat Sheet
OCR Reference Language Guide Cheat Sheet
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 //
variable=input(prompt to user)
Example
name=input(“Please enter your name”)
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
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.
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()
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
next …
for i = 2 to 10 step 2
print(i)
next i
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
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
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
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()
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)
myVariable = random(-5.0,5.0)
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.