Mission 1 · Spec 2.2.1
Variables, constants and operators
Storing values, taking input and giving output, and the arithmetic, comparison and Boolean operators in OCR Exam Reference Language.
- Starter 5 min
- Learn 10 min
- Lab 15 min
- Quiz 10 min
- Exam 10 min
Sharing sweets
23 sweets are shared between 5 people. How many each, and how many are left over?
Reveal
4 each, 3 left over. In code: 23 DIV 5 gives 4 and 23 MOD 5 gives 3.
Key ideas
Variables and constants
A variable is a named memory location whose value can change. A constant's value can't change while the program runs.
score = 0
const VAT = 0.2
Input, output, assignment
name = input("Enter your name")
print("Hello " + name)
Arithmetic
+ - * /
MOD remainder: 7 MOD 2 = 1
DIV quotient: 7 DIV 2 = 3
^ exponent: 2 ^ 3 = 8
Comparison and Boolean
== != < <= > >=
AND OR NOT
In OCR ERL, = assigns a value and == compares two values.
Expression blaster
Exam-style questions
1. Give the result of: (a) 19 MOD 4 (b) 19 DIV 4 (c) 3 ^ 2
Mark scheme
- (a) 3 (1)
- (b) 4 (1)
- (c) 9 (1)
2. Explain the difference between a variable and a constant.
[2 marks]Mark scheme
- A variable's value can change while the program runs (1)
- A constant's value cannot change while the program runs (1)
TUTOR NOTES
- Misconception: using = to compare in ERL conditions.
- Language link: in Python, DIV is
//, MOD is%and exponent is**.
Mission 2 · Spec 2.2.1
Sequence, selection and iteration
The three programming constructs: if and switch/case, count-controlled and condition-controlled loops.
- Starter 5 min
- Learn 10 min
- Lab 20 min
- Quiz 10 min
- Exam 10 min
Laps or until the whistle?
"Run 5 laps" or "run until I blow the whistle". What's the difference in how you know when to stop?
Reveal
5 laps is count-controlled (a for loop). Until the whistle is condition-controlled (a while or do … until loop).
Key ideas
Selection: if
if mark >= 70 then
print("Merit")
elseif mark >= 50 then
print("Pass")
else
print("Fail")
endif
Selection: switch/case
switch day:
case "Sat":
print("Weekend")
default:
print("Weekday")
endswitch
Count-controlled
for i = 1 to 5
print(i)
next i
Condition-controlled
while guess != 7
guess = input("Guess")
endwhile
do
guess = input("Guess")
until guess == 7
Loop lab
Exam-style questions
1. Write an algorithm that asks for a password and repeats until the user enters "letmein", then prints "Access granted".
[3 marks]Mark scheme
- Takes the password as input (1)
- Loop that repeats while / until the password is "letmein" (1)
- Outputs "Access granted" after the loop (1)
2. State the difference between a count-controlled and a condition-controlled loop.
[2 marks]Mark scheme
- Count-controlled repeats a set number of times (1)
- Condition-controlled repeats until / while a condition is met (1)
TUTOR NOTES
- Misconception:
for i = 1 to 5runs 4 times. It runs 5 times. - ERL: a do … until loop always runs at least once.
Mission 3 · Spec 2.2.2
Data types and casting
Integer, real, Boolean, character and string, and converting between them.
- Starter 5 min
- Learn 10 min
- Lab 15 min
- Quiz 10 min
- Exam 10 min
"5" + "5"
What does "5" + "5" give? What about 5 + 5?
Reveal
"55" and 10. Strings are joined (concatenated); integers are added. The data type changes what the operator does.
Key ideas
| Type | Stores | Example |
|---|---|---|
| Integer | Whole numbers | 42 |
| Real | Numbers with a decimal part | 3.14 |
| Boolean | True or False | True |
| Character | A single character | "A" |
| String | A sequence of characters | "Hello" |
Casting converts a value from one data type to another. input() returns a string, so it must be cast before arithmetic: age = int(input("Age")).
Type sorter
Casting match
Exam-style questions
1. State the most suitable data type for: (a) a student's height in metres (b) whether homework is complete (c) a student's surname.
[3 marks]Mark scheme
- (a) Real (1)
- (b) Boolean (1)
- (c) String (1)
2. Explain why casting is needed in age = int(input("Enter age")).
Mark scheme
- input() returns a string (1)
- The value must be converted to an integer so it can be used in calculations / comparisons (1)
TUTOR NOTES
- Misconception: phone numbers should be integers.
- Language link: the same casting functions exist in Python.
Mission 4 · Spec 2.2.3
Strings and files
String manipulation in ERL, and reading from and writing to text files.
- Starter 5 min
- Learn 10 min
- Lab 20 min
- Quiz 10 min
- Exam 10 min
Username generator
A school makes usernames from the first 3 letters of a surname plus the first letter of the first name, e.g. Smith, Anna → "smia". How would a program do this?
Reveal
surname.left(3) + firstname.left(1), then .lower. String manipulation lets programs pull text apart and join it back together.
Key ideas
String manipulation
s.length
s.substring(start, length)
s.left(n) · s.right(n)
s.upper · s.lower
ASC("A") → 65 · CHR(65) → "A"
s + t (concatenation)
Characters are numbered from 0.
File handling
myFile = open("scores.txt")
while NOT myFile.endOfFile()
print(myFile.readLine())
endwhile
myFile.close()
newFile("log.txt")
f = open("log.txt")
f.writeLine("Hello")
f.close()
Always close a file when you've finished with it.
String workbench
File handling in order
Put the steps for reading every line of a file in order.Exam-style questions
1. word = "Computing". State the output of: (a) word.length (b) word.left(4) (c) word.substring(3, 3)
Mark scheme
- (a) 9 (1)
- (b) Comp (1)
- (c) put (1)
2. Write an algorithm that writes the names "Ali" and "Beth" to a new file called names.txt.
[3 marks]Mark scheme
- Creates / opens the file, e.g.
newFile("names.txt")andopen(1) - Writes both names using
writeLine(1) - Closes the file (1)
TUTOR NOTES
- Misconception: substring's second parameter is the end position. In ERL it's the number of characters.
- Check: "Computing" positions 3, 4, 5 are p, u, t.
Mission 5 · Spec 2.2.3
Arrays, records and SQL
1D and 2D arrays, records to store data, and SQL to search a database.
- Starter 5 min
- Learn 10 min
- Lab 25 min
- Quiz 10 min
- Exam 10 min
30 variables?
Would you store 30 test scores in 30 separate variables? What problems would it cause?
Reveal
You couldn't loop through them. An array stores them all under one name: scores[0] to scores[29].
Key ideas
1D array
array names[3]
names[0] = "Ali"
print(names[0])
Fixed size; items of the same type; indexed from 0.
2D array
array scores[3,4]
scores[1,2] = 75
Think of it as a table: [row, column].
Records
Store related data about one item, where fields can have different data types, e.g. a student's name, age and form. In a database, each row of a table is a record.
SQL
SELECT Surname, Form
FROM Students
WHERE Age >= 15
SELECT * FROM Students
WHERE Surname LIKE "S%"
* selects every field; % is a wildcard in LIKE.
2D array explorer
SQL playground
A real database runs in your browser.Exam-style questions
1. Write an SQL statement to show the FirstName and Age of all students in form 10A.
[3 marks]Mark scheme
SELECT FirstName, Age(1)FROM Students(1)WHERE Form = "10A"(1)
2. Write an algorithm that outputs every value in a 1D array temps of 7 values.
Mark scheme
- A loop from 0 to 6 (1)
- Outputs
temps[i]each time (1)
TUTOR NOTES
- ERL: 2D arrays use a comma:
scores[1,2]. - Misconception: SQL text values without quotes.
Mission 6 · Spec 2.2.3
Sub programs and random numbers
Functions and procedures, parameters, return values, local and global variables, and generating random numbers.
- Starter 5 min
- Learn 10 min
- Lab 20 min
- Quiz 10 min
- Exam 10 min
Recipe steps
A recipe book says "make the white sauce (see page 12)" in five recipes. Why not print the method five times?
Reveal
Write it once, reuse it. If it changes, only one place needs updating. That's what a sub program does.
Key ideas
Function
A sub program that returns a value.
function area(w, h)
return w * h
endfunction
Procedure
A sub program that does not return a value.
procedure greet(name)
print("Hi " + name)
endprocedure
Local vs global
Local variables exist only inside the sub program. Global variables can be used anywhere in the program.
Random numbers
roll = random(1, 6)
Returns a random integer from 1 to 6 inclusive.
Why use sub programs? Code is reused, programs are shorter, easier to test and debug, and different programmers can work on different parts.
Inside a sub program call
Random number generator
Exam-style questions
1. Write a function isEven that takes a number as a parameter and returns True if it is even, otherwise False.
Mark scheme
- Function defined with a parameter (1)
- Uses
MOD 2 == 0(1) - Returns True / False correctly (1)
2. Give two benefits of using sub programs.
[2 marks]Mark scheme
- Any two: reuse of code; shorter programs; easier to test / debug; easier to maintain; work can be shared between programmers (1 each)
TUTOR NOTES
- Misconception: print and return do the same thing.
- Extension: write a function that rolls two dice and returns the total.