As and A Level: Computer Science
As and A Level: Computer Science
H046/H446
COMPUTER
SCIENCE
Pseudocode Guide
August 2015
We will inform centres about any changes to the specification. We will also
publish changes on our website. The latest version of our specification will
always be the one on our website (www.ocr.org.uk) and this may differ from
printed versions.
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.
CONTENTS
INTRODUCTION PAGE 4
VARIABLES PAGE 4
CASTING PAGE 4
OUTPUTTING TO SCREEN PAGE 5
ITERATION – COUNT CONTROLLED PAGE 5
ITERATION – CONDITION CONTROLLED PAGE 5
LOGICAL OPERATORS PAGE 6
STRING HANDLING PAGE 7
SUBROUTINES PAGE 8
ARRAYS PAGE 9
READING TO AND WRITING FROM FILES PAGE 10
COMMENTS PAGE 11
OBJECT-ORIENTED PAGE 11
METHODS AND ATTRIBUTES: PAGE 11
CONSTRUCTORS AND INHERITANCE PAGE 12
3
AS AND A LEVEL COMPUTER SCIENCE PSEUDOCODE GUIDE
Introduction
The following guide shows the format pseudocode will appear in the examined components. It is provided to enable teachers to provide learners with 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
4
AS AND A LEVEL COMPUTER SCIENCE PSEUDOCODE GUIDE
Outputting to Screen
print(string)
Example:
print(“hello”)
variable=input(prompt to user)
Example:
print(“Hello”)
next i
endwhile
do
until answer==”computer”
5
AS AND A LEVEL COMPUTER SCIENCE PSEUDOCODE GUIDE
Logical Operators
AND OR NOT
eg
if/else
if entry==”a” then
else
print(“Unrecognised selection”)
endif
6
AS AND A LEVEL COMPUTER SCIENCE PSEUDOCODE GUIDE
switch/case
switch entry:
case “A”:
case “B”:1
default:
print(“Unrecognised selection”)
endswitch
String Handling
To get the length of a string:
stringname.length
To get a substring:
stringname.subString(startingPosition, numberOfCharacters)
someText=”Computer Science”
print(someText.length)
print(someText.substring(3,3))
Will display:
16
put
7
AS AND A LEVEL COMPUTER SCIENCE PSEUDOCODE GUIDE
Subroutines
function triple(number)
return number*3
endfunction
procedure greeting(name)
print(“hello”+name)
endprocedure
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
8
AS AND A LEVEL 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”
9
AS AND A LEVEL 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 //
10
AS AND A LEVEL COMPUTER SCIENCE PSEUDOCODE GUIDE
Object-Oriented
Object oriented code will match the pseudocode listed above with the following extensions:
level is relevant to the question it will always be explicit in the code denoted by the keywords.
private attempts = 3
attempts=number
endprocedure
return attempts
endfunction
Methods will always be instance methods, learners aren’t expected to be aware of static methods.
player.setAttempts(5)
print(player.getAttempts())
11
AS AND A LEVEL COMPUTER SCIENCE PSEUDOCODE GUIDE
class Pet
private name
name=givenName
endprocedure
endclass
Inheritance is denoted by the inherits keyword, superclass methods will be called with the keyword super. i.e. super.methodName(parameters) in the case of the constructor this would be
super.new()
private breed
super.new(givenName)
breed=givenBreed
endprocedure
endclass
eg
12
AS AND A LEVEL COMPUTER SCIENCE PSEUDOCODE GUIDE
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.
Whether you already offer OCR qualifications, are new to OCR, or are considering switching from your current provider/awarding organisation, you can request more information by completing the
Expression of Interest form which can be found here:
www.ocr.org.uk/expression-of-interest
OCR acknowledges the use of the following content:Square down and Square up: alexwhite/Shutterstock.com
Please get in touch if you want to discuss the accessibility of resources we offer to support delivery of our qualifications: [email protected]
13
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 2016 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.