Pseudocode Cheat Sheet Guide
Pseudocode Cheat Sheet Guide
REFORM
Copyright
OCR retains the copyright on all its publications, including the specifications.
However, registered centres for OCR are permitted to copy material from this
specification booklet for their own internal use.
This resource is an exemplar of the types of materials that will be provided to assist in the teaching of the new
qualifications being developed for first teaching in 2016. It can be used to teach existing qualifications but may be
updated in the future to reflect changes in the new qualifications. Please check the OCR website for updates and
additional resources being released. We would welcome your feedback so please get in touch.
GCSE (9–1) COMPUTER SCIENCE PSEUDOCODE GUIDE
CONTENTS
INTRODUCTION 4
VARIABLES 4
CASTING 4
OUTPUTTING TO SCREEN 4
ITERATION – COUNT CONTROLLED 5
ITERATION – CONDITION CONTROLLED 5
LOGICAL OPERATORS 6
SELECTION 7
STRING HANDLING 8
SUBROUTINES 9
ARRAYS 10
READING TO AND WRITING FROM FILES 11
COMMENTS 11
3
GCSE (9–1) COMPUTER SCIENCE PSEUDOCODE GUIDE
INTRODUCTION
The following guide shows the format pseudocode will appear in the examined components. It is provided to allow you to give learners
familiarity before the exam. Learners are not expected to memorise the syntax of this pseudocode and when asked may provide answers in
any style of pseudocode they choose providing its meaning could be reasonably inferred by a competent programmer.
Variables
Variables are assigned using the = operator.
x=3
name=”Bob”
A variable is declared the first time a value is assigned. It assumes the data type of the value it is given.
Variables in the main program can be made global with the keyword global.
Casting
Variables can be typecast using the int str and float functions
int(“3”) returns 3
Outputting to Screen
print(string)
Example:
print(“hello”)
variable=input(prompt to user)
Example:
4
GCSE (9–1) COMPUTER SCIENCE PSEUDOCODE GUIDE
print(“Hello”)
next i
endwhile
do
until answer==”computer”
5
GCSE (9–1) COMPUTER SCIENCE PSEUDOCODE GUIDE
Logical Operators
AND OR NOT
eg
AND OR NOT
A B A ^B A B A B A ¬A
^
T T T T T T T F
T F F T F T F T
F T F F T T
F F F F F F
6
GCSE (9–1) COMPUTER SCIENCE PSEUDOCODE GUIDE
Selection
Selection will be carried out with if/else and switch/case:
if/else
if entry==”a” then
else
print(“Unrecognised selection”)
endif
switch/case
switch entry:
case “A”:
case “B”:
default:
print(“Unrecognised selection”)
endswitch
7
GCSE (9–1) COMPUTER SCIENCE PSEUDOCODE GUIDE
String Handling
To get the length of a string:
stringname.length
To get a substring:
stringname.subString(startingPosition, numberOfCharacters)
Example:
someText=”Computer Science”
print(someText.length)
print(someText.substring(3,3))
Will display:
16
put
8
GCSE (9–1) COMPUTER SCIENCE PSEUDOCODE GUIDE
Subroutines
function triple(number)
return number*3
endfunction
y=triple(7)
procedure greeting(name)
print(“hello”+name)
endprocedure
greeting(“Hamish”)
Unless stated values passed to subroutines can be assumed to be passed by value. If this is relevant to the question byVal
and byRef will be used. In the case below x is passed by value and y is passed by reference.
endprocedure
9
GCSE (9–1) COMPUTER SCIENCE PSEUDOCODE GUIDE
Arrays
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”
10
GCSE (9–1) COMPUTER SCIENCE PSEUDOCODE GUIDE
myFile = openRead(“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 = openRead(“sample.txt”)
print(myFile.readLine())
endwhile
myFile.close()
To open a file to write to openWrite is used and writeLine to add a line of text to the file. In the program below hello world is
made the contents of sample.txt (any previous contents are overwritten).
myFile = openWrite(“sample.txt”)
myFile.writeLine(“Hello World”)
myFile.close()
Comments
Comments are denoted by //
11
We’d like to know your view on the resources we produce. By clicking on the ‘Like’ or ‘Dislike’ button you can help us to ensure
that our resources work for you. When the email template pops up please add additional comments if you wish and then just
click ‘Send’. Thank you.
If you do not currently offer this OCR qualification but would like to do so, please complete the Expression of Interest Form which
can be found here: www.ocr.org.uk/expression-of-interest
Please get in touch if you want to discuss the accessibility of resources we offer to support delivery of our qualifications: [email protected]
ocr.org.uk/gcsereform
OCR customer contact centre
General qualifications
Telephone 01223 553998
Facsimile 01223 552627
Email [email protected]
For staff training purposes and as part of our quality assurance programme your call may be recorded or monitored.
© OCR 2015 Oxford Cambridge and RSA Examinations is a Company Limited by Guarantee. Registered in England.
Registered office 1 Hills Road, Cambridge CB1 2EU. Registered company number 3484466. OCR is an exempt charity.