Eduqas GCSE Computer Science · 4, 5, 8 Programming and data organisation
Mini-Lesson
Programming and data organisation
5 missions. Guess, learn, then check. Your place is saved.
Mission 1How does a game turn 135 seconds into 2:15 on its timer?
Mission 2How can a few lines of code print all 100 times-table answers?
Mission 3How can one name hold all 9 squares of a noughts and crosses board?
Mission 4Two subroutines both use a variable called total. Why no clash?
Mission 5How does an app show only the last 4 digits of your card number?
The specification points and sources are listed under the lesson: see Sources.
Mission 1
How does a game turn 135 seconds into 2:15 on its timer?
Make a guess: How does a game turn 135 seconds into 2:15 on its timer?
Programming · data and operators
Variables, constants, input and output
A variable is a named memory location whose value can change while the program runs.
Assignment puts a value into a variable; the first assignment is initialisation. Use meaningful identifiers like lives, not x.
Eduqas pseudo-code
lives is integer
playerName is string
set lives = 3
output "Enter your name"
input playerName
output playerName
set lives = lives - 1
output lives
Programming · data and operators
Data types
A data type tells the computer what kind of data a variable holds and which operations make sense.
Data type
Holds
Examples
Integer
a whole number
42, -7, 0
Real
a number that can have a fractional part
3.5, -0.25, 12.0
Boolean
one of two values
True, False
Character
one single character
'A', '7', '@'
String
a sequence of characters
'Hello', '07700 900123'
Quick check
?A program stores a customer's phone number, such as 07700 900123. Which data type is most suitable?
Programming · data and operators
Arithmetic operators
+, - and * work as in maths. Real division/ can give a fractional answer.
Integer division gives only the whole-number part. The remainder (modulus) is what is left over.
Operation
200 seconds ÷ 60
Result
Real division
200 / 60
3.333…
Integer division
200 DIV 60
3
Remainder
200 MOD 60
20
Eduqas pseudo-code
set total = 200
set minutes = total DIV 60 /** 3 */
set seconds = total MOD 60 /** 20 */
Calculate
?A shop packs eggs in boxes of 6. It has 47 eggs. What value does the program store in spare?
Eduqas pseudo-code
set boxes = 47 DIV 6
set spare = 47 MOD 6
Programming · data and operators
Casting
Casting converts a value from one data type to another, e.g. the string '15' to the integer 15.
Keyboard input usually arrives as a string, so cast it before doing arithmetic.
Cast numbers to strings to join them into a message.
Eduqas pseudo-code
input ageText
set age = val(ageText)
output age + 1
Quick check
?A user types 15 for their age. The program reads it as a string and needs to output the age next year. What must happen first?
Mission check
?How does a game turn 135 seconds into 2:15 on its timer?
The timer keeps the time as one integer number of seconds. Integer division by 60 (DIV) gives the whole minutes, 2, and the remainder (MOD) gives the seconds left over, 15. Real division would give 2.25, which is not 2 minutes 25 seconds.
Mission 2
How can a few lines of code print all 100 times-table answers?
Make a guess: How can a few lines of code print all 100 times-table answers?
Programming · control flow
Sequence and selection
Sequence: statements run one after another, in order.
Selection: a condition that is true or false decides which block of code runs.
Compare with <, >, <=, >=, equal to = and not equal to <>.
Join conditions with AND, OR, NOT, XOR.
Eduqas pseudo-code
set mark = 72
if mark >= 70
output "Distinction"
else
if mark >= 40
output "Pass"
else
output "Fail"
end if
end if
Quick check
?What is output when this code runs?
Eduqas pseudo-code
set temp = 18
set wind = 35
if (temp > 15) AND NOT(wind > 30)
output "Picnic"
else
output "Stay in"
end if
Programming · control flow
Count-controlled loops
Iteration repeats a block of code.
A count-controlled loop repeats a fixed number of times. A counter variable goes up by 1 each time unless a step is given.
Eduqas pseudo-code
for count = 1 to 5
output count * 3
next count
All output 3, 6, 9, 12, 15.
Programming · control flow
Condition-controlled loops
A condition-controlled loop repeats until a condition changes, so you need not know the number of passes.
A WHILE loop tests the condition at the start: it may run zero times.
repeat … until tests at the end, so it always runs at least once.
Eduqas pseudo-code
set total = 1
while total < 50
set total = total * 2
repeat
output total
All output 64.
Your answer
?What is output when this code runs?
Eduqas pseudo-code
set n = 3
while n < 40
set n = n * 2
repeat
output n
Programming · control flow
Nesting
Nesting puts one construct inside another: an IF inside a loop, a loop inside an IF, or a loop inside a loop.
With nested loops, the inner loop runs in full for every pass of the outer loop.
Total passes of the inner block = outer passes × inner passes.
Eduqas pseudo-code
for row = 1 to 3
for col = 1 to 3
output row * col
next col
next row
Output: 1, 2, 3, 2, 4, 6, 3, 6, 9 (9 values).
Calculate
?How many values does this code output?
Eduqas pseudo-code
for i = 1 to 4
for j = 1 to 6
output i + j
next j
next i
Programming · control flow
Counts and rogue values
A count records how many times something has happened; it can stop a loop when it reaches a set value.
A rogue value is a value outside the normal data (e.g. −1 for a mark) that tells a loop to stop.
Check for the rogue value before processing, so it is never counted or added.
Eduqas pseudo-code
set count = 0
set total = 0
input mark
while mark <> -1
set count = count + 1
set total = total + mark
input mark
repeat
output count
output total
Inputs 12, 7, 9, −1 give a count of 3 and a total of 28.
Mission check
?How can a few lines of code print all 100 times-table answers?
The program uses nested count-controlled loops. The outer loop counts the rows 1 to 10, and for every row the inner loop counts the columns 1 to 10 and outputs row × column. That gives 10 × 10 = 100 answers from a handful of lines.
Mission 3
How can one name hold all 9 squares of a noughts and crosses board?
Make a guess: How can one name hold all 9 squares of a noughts and crosses board?
Programming · data structures
Arrays
An array is a data structure: it stores many values under one identifier. Each value is an element.
All elements have the same data type.
An index picks out one element. The first element has index 0.
Eduqas pseudo-code
set scores[0] = 12
set scores[1] = 7
set scores[2] = 9
set scores[3] = 15
set scores[4] = 4
output scores[1] /** 7 */
set scores[3] = 20 /** 15 becomes 20 */
Programming · data structures
Looping through an array
Use a variable as the index in a count-controlled loop to visit every element in turn.
The counter runs from 0 to length − 1.
Eduqas pseudo-code
/** temps holds 18, 21, 17, 22, 19 */
set total = 0
for i = 0 to 4
set total = total + temps[i]
next i
output total
All output 97.
Your answer
?What is output when this code runs?
Eduqas pseudo-code
for i = 0 to 4
set nums[i] = i * 3
next i
output nums[2] + nums[4]
Programming · data structures
2D arrays
A two-dimensional (2D) array is like a table: one identifier, with rows and columns.
Each element needs two indexes: board[row, col] (row first here).
A noughts and crosses board is a 3 × 3 2D array of characters.
Eduqas pseudo-code
/** board is a 3 x 3 array of characters */
set board[1,1] = "X" /** centre square */
set board[2,0] = "O" /** bottom-left square */
Quick check
?This table shows a 2D array marks with 3 rows and 4 columns (row first). What is marks[1, 2]?
5
8
2
9
7
1
6
3
4
10
11
0
Programming · data structures
Records
A record groups related data about one item under one name.
Each item of data in a record is a field. Fields can have different data types.
Compare: an array's elements all share one data type, but a record mixes types.
A file is a collection of records of the same design.
Field
Data type
Example
name
String
'Biscuit'
species
String
'dog'
age
Integer
4
weight
Real
12.5
vaccinated
Boolean
True
Programming · data structures
File design
Design each record first: field names, data types and field lengths.
Fixed-length records give every field a set size, so every record is the same length.
Choose a key field whose value is unique for every record, e.g. petID.
File size = record size × number of records, e.g. 40 bytes × 500 records = 20,000 bytes.
Field
Data type
Length (bytes)
Example
petID (key)
String
4
P017
name
String
20
Biscuit
species
String
15
dog
vaccinated
Boolean
1
TRUE
Record total
40
Quick check
?A club stores each member's name, age and whether they have paid. Which structure best holds one member's details?
Mission check
?How can one name hold all 9 squares of a noughts and crosses board?
Store the board as a 2D array: one identifier, 3 rows × 3 columns, so 9 elements of the same data type. Each square is reached with two indexes, row then column, counting from 0.
Mission 4
Two subroutines both use a variable called total. Why no clash?
Make a guess: Two subroutines both use a variable called total. Why no clash?
Programming · subroutines
Subroutines
A subroutine is a named, out-of-line block of code. It runs when you call it by its name, then control returns to the line after the call.
A procedure carries out a task. A function carries out a task and returns a value.
Eduqas pseudo-code
Declare Welcome
output "Welcome to the quiz"
End Subroutine
call Welcome
call Welcome
Programming · subroutines
Parameters and return values
A parameter passes data into a subroutine when it is called. Values are matched to parameters in order.
A function sends its result back with return. The returned value replaces the call, so store it or use it straight away.
Eduqas pseudo-code
Declare Area(width, height)
set result = width * height
return result
End Subroutine
set roomArea = Area(4, 5)
output roomArea /** 20 */
A local variable is declared inside a subroutine: it only exists while that subroutine runs and is only usable inside it.
A global variable is declared in the main program and can be used anywhere.
Good practice: nothing outside can change a local variable, and its name can be reused elsewhere.
Eduqas pseudo-code
Declare AddBonus(score)
set newScore = score + bonus /** newScore is local */
return newScore
End Subroutine
set bonus = 5 /** bonus is global */
output AddBonus(10) /** 15 */
output newScore /** error: newScore does not exist here */
Quick check
?A subroutine calcTax declares a local variable tax and returns its value. After calling calcTax, the main program tries to output tax. What happens?
Programming · subroutines
Why use subroutines?
Reuse: write code once and call it many times, so there is less to write and check.
Decomposition: each subroutine solves one part and can be written and tested on its own.
Maintenance: fix or improve the code in one place.
Give subroutines well-defined interfaces and self-documenting identifiers.
Quick check
?Which task should be written as a function rather than a procedure?
Mission check
?Two subroutines both use a variable called total. Why no clash?
Each total is a local variable. It is created when its subroutine runs, can only be used inside that subroutine, and disappears when the subroutine ends. So the two variables never meet, which is why using local variables is good practice.
Mission 5
How does an app show only the last 4 digits of your card number?
Make a guess: How does an app show only the last 4 digits of your card number?
Programming · strings, files and random numbers
Strings have positions
A string is a sequence of characters. Each character has a position, its index.
A question tells you whether the first character is position 0 or 1, so check.
The length of a string is how many characters it holds, spaces included.
A card number is stored as a string, not an integer: it is never used in arithmetic.
Eduqas pseudo-code
set card = "4929123456789012"
output len(card)
/** outputs 16 */
Programming · strings, files and random numbers
Taking a substring
A substring is part of a string, picked out by position.
left(string, x) and right(string, x) take x characters from the left or right end; mid(string, x, y) takes characters from the middle.
right counts back from the end, so the code works for any card length.
Eduqas pseudo-code
set card = "4929123456789012"
set last4 = right(card, 4)
output "****", last4
Your answer
?Trace this code. What is output?
Eduqas pseudo-code
set word = "computer"
output right(left(word, 6), 3)
Programming · strings, files and random numbers
More string tools
len(string) gives the length; mid(string, x, y) takes characters from the middle.
trim(string) removes spaces from the ends; val(string) and int(string) turn digit strings into numbers.
char(number) gives the character for a character code; instring finds one string inside another.
Programming · strings, files and random numbers
Reading and writing files
Variables are lost when a program ends. A file in secondary storage keeps data, such as high scores, for next time.
Open the file, read or write it, then close it so the data is saved and the file is freed.
Lines can be read into a variable or into an array (a list), and written from one.
Python
file = open("scores.csv", "a")
file.write("Zara,42\n")
file.close()
file = open("scores.csv", "r")
for line in file:
fields = line.strip("\n").split(",")
print(fields[0] + " scored " + fields[1])
file.close()
Quick check
?A game keeps its high-score table in a variable. Every time the game closes, the scores vanish. What fixes this?
Mission check
?How does an app show only the last 4 digits of your card number?
The card number is stored as a string. The program finds its length, works out that the last four characters start at length − 4 when counting from 0 (length − 3 when counting from 1), and takes that substring to show after the stars.
Recap
Big ideas
A variable is a named memory location whose value can change as the program runs.
Choose the data type that fits the data: integer, real, Boolean, character or string, and cast between types when needed (input text to an integer).
Integer division gives the whole-number part; the remainder (modulus) gives what is left over.
Sequence runs statements in order; selection uses a true/false condition to choose a branch.
Count-controlled loops repeat a fixed number of times; condition-controlled loops repeat until a condition changes, tested at the start or the end.
In nested loops the inner loop runs fully for every pass of the outer loop.
An array holds many values of one data type under one identifier; an index picks out an element (the first is index 0).
A 2D array is a table of rows and columns, so each element needs two indexes.
A record groups fields of different data types about one item.
A subroutine is a named block of code called by its name; a function returns a value and a procedure does not.
Parameters pass data in, in order; return passes a result back to the call.
Local variables exist only inside their subroutine while it runs, so the same name can be reused safely; global variables can be used anywhere.
A string is a sequence of characters, each with an index; the length is how many characters it has. Check whether positions start at 0 or 1.
A substring picks out part of a string by position.
Files keep data after a program ends: open, read or write, close.
🏆
Mini-lesson complete!
⭐⭐⭐
You've worked through Programming and data organisation for Eduqas GCSE Computer Science. 🎉
Mission 1How does a game turn 135 seconds into 2:15 on its timer?
Mission 2How can a few lines of code print all 100 times-table answers?
Mission 3How can one name hold all 9 squares of a noughts and crosses board?
Mission 4Two subroutines both use a variable called total. Why no clash?
Mission 5How does an app show only the last 4 digits of your card number?