Appendix 5d (OCR A-Level Computer Science): Revision Notes
Languages and Boolean Logic Guide
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 declared inside a function or procedure are local to that subroutine.
Variables in the main programme can be made global with the keyword global.
global userid = 123
Casting
Variables can be typecast using the int, str, and float functions.
str(3) returns "3"
int("3") returns 3
float("3.14") returns 3.14
Outputting to Screen
print(string)
Example:
print("hello")
Taking Input from User
variable = input(prompt to user)
Example:
name = input("Please enter your name")
Iteration
Count Controlled
for i = 0 to 7
print("Hello")
next i
Will print "Hello" 8 times (0–7 inclusive).
Condition Controlled
While Loop:
while answer != "computer"
answer = input("What is the password?")
endwhile
Do Until Loop:
do
answer = input("What is the password?")
until answer == "computer"
Logical Operators
AND, OR, NOT
Example:
while x <= 5 AND flag == false
Comparison Operators
| Symbol | Meaning |
|---|---|
| == | Equal to |
| != | Not equal to |
| < | Less than |
| <= | Less than or equal to |
| > | Greater than |
| >= | Greater than or equal to |
Arithmetic Operators
| Operator | Function | Example |
|---|---|---|
| + | Addition | x = 6 + 5 → 11 |
| - | Subtraction | x = 6 • 5 → 1 |
| * | Multiplication | x = 12 * 2 → 24 |
| / | Division | x = 12 / 2 → 6 |
| MOD | Modulus | 12 MOD 5 → 2 |
| DIV | Quotient | 17 DIV 5 → 3 |
| ^ | Exponentiation | 3 ^ 4 → 81 |
Selection
If/Else
if entry == "a" then
print("You selected A")
elseif entry == "b" then
print("You selected B")
else
print("Unrecognised selection")
endif
Switch/Case
switch entry:
case "A":
print("You selected A")
case "B":
print("You selected B")
default:
print("Unrecognised selection")
endswitch
String Handling
Length of a String:
stringname.length
Substring:
stringname.substring(startingPosition, numberOfCharacters)
(Strings start at the 0th character.)
Example:
someText = "Computer Science"
print(someText.length)
print(someText.substring(3, 3))
Subroutines
Function Example
function triple(number)
return number * 3
endfunction
// Called from main programme
y = triple(7)
Procedure Example
procedure greeting(name)
print("Hello " + name)
endprocedure
// Called from main programme
greeting("Hamish")
Values are passed by value unless stated otherwise. Use byVal and byRef for clarity if required.
Arrays
Arrays are 0-based and declared using the array keyword.
array names[5]
names[0] = "Ahmad"
names[1] = "Ben"
names[2] = "Catherine"
names[3] = "Dana"
names[4] = "Elijah"
print(names[3])
2D Array Example:
array board[8, 8]
board[0, 0] = "rook"
Reading and Writing Files
Reading
myFile = openRead("sample.txt")
x = myFile.readLine()
myFile.close()
Writing
myFile = openWrite("sample.txt")
myFile.writeLine("Hello World")
myFile.close()
Comments
Comments are denoted by //.
print("Hello World") // This is a comment