← GCSE Computer Science · AQA · GCSE Computer Science revision
⭐ 0
AQA GCSE Computer Science (8525) · 3.2 Programming
Mini-Lesson

Programming

7 missions. Guess, learn, then check. Your place is saved.

  1. Mission 1How does a game turn 135 seconds into 2:15 on its timer?
  2. Mission 2How can a few lines of code print all 100 times-table answers?
  3. Mission 3How can one name hold all 9 squares of a noughts and crosses board?
  4. Mission 4Two subroutines both use a variable called total. Why no clash?
  5. Mission 5How does an app show only the last 4 digits of your card number?
  6. Mission 6You typed 41, not 14, for your age. Why did the form accept it?
  7. Mission 7It passed every test. So why did it fail for an 18-year-old?
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

Variables and constantslives32assignmentvariableMAX_LIVES3constantvariable: value can changeconstant: value fixed while running
  • A variable is a named memory location whose value can change while the program runs.
  • A constant is a named value that cannot change while the program runs. Naming it means one place to update it, and clearer code.
  • Assignment puts a value into a variable; the first assignment is initialisation. Use meaningful identifiers like lives, not x.
AQA pseudo-code
CONSTANT MAX_LIVES ← 3
lives ← MAX_LIVES
name ← USERINPUT
OUTPUT 'Good luck, ', name
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 typeHoldsExamples
Integera whole number42, -7, 0
Reala number that can have a fractional part3.5, -0.25, 12.0
Booleanone of two valuesTrue, False
Characterone single character'A', '7', '@'
Stringa sequence of characters'Hello', '07700 900123'
  • Some languages call a real a float; AQA exams use the names in the table.
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

Integer division and remainder200 seconds60606020integer division = 3200 DIV 60remainder = 20200 MOD 60
  • +, - 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.
Operation200 seconds ÷ 60Result
Real division200 / 603.333…
Integer division200 DIV 603
Remainder200 MOD 6020
AQA pseudo-code
total ← 200
minutes ← total DIV 60    # 3
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?
AQA pseudo-code
boxes ← 47 DIV 6
spare ← 47 MOD 6
Programming · data and operators

Casting

Casting'15'stringint()15integer+ 1 → 16
  • 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.
  • AQA calls these string conversion operations: STRING_TO_INT, STRING_TO_REAL, INT_TO_STRING, REAL_TO_STRING.
AQA pseudo-code
ageText ← USERINPUT
age ← STRING_TO_INT(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?
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

Selection flowchartYesNoYesNoStartmark = 72mark >= 70?Output"Distinction"mark >= 40?Output "Pass"Output "Fail"Stop
  • 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.
AQA pseudo-code
mark ← 72
IF mark ≥ 70 THEN
  OUTPUT 'Distinction'
ELSE IF mark ≥ 40 THEN
  OUTPUT 'Pass'
ELSE
  OUTPUT 'Fail'
ENDIF
Quick check
?What is output when this code runs?
AQA pseudo-code
temp ← 18
wind ← 35
IF temp > 15 AND NOT (wind > 30) THEN
  OUTPUT 'Picnic'
ELSE
  OUTPUT 'Stay in'
ENDIF
Programming · control flow

Count-controlled loops

  • Iteration repeats a block of code.
  • A count-controlled loop (definite iteration) repeats a fixed number of times. A counter variable goes up by 1 each time unless a step is given.
AQA pseudo-code
FOR count ← 1 TO 5
  OUTPUT count * 3
ENDFOR

All output 3, 6, 9, 12, 15.

Programming · control flow

Condition-controlled loops

  • A condition-controlled loop (indefinite iteration) 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 (or DO … WHILE) tests at the end, so it always runs at least once.
AQA pseudo-code
total ← 1
WHILE total < 50
  total ← total * 2
ENDWHILE
OUTPUT total

All output 64.

Your answer
?What is output when this code runs?
AQA pseudo-code
n ← 3
WHILE n < 40
  n ← n * 2
ENDWHILE
OUTPUT n
Programming · control flow

Nesting

Nested loopsrowcoloutput111122133212224236313326339inner loop:3 passesinner loop:3 passesinner loop:3 passesouter loop:3 passes
  • 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.
AQA pseudo-code
FOR row ← 1 TO 3
  FOR col ← 1 TO 3
    OUTPUT row * col
  ENDFOR
ENDFOR

Output: 1, 2, 3, 2, 4, 6, 3, 6, 9 (9 values).

Calculate
?How many values does this code output?
AQA pseudo-code
FOR i ← 1 TO 4
  FOR j ← 1 TO 6
    OUTPUT i + j
  ENDFOR
ENDFOR
Mission check

?How can a few lines of code print all 100 times-table answers?
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 arrayscores12071923441520indexOUTPUT scores[1]
  • 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.
  • AQA exam questions index from 0 unless they say otherwise.
AQA pseudo-code
scores ← [12, 7, 9, 15, 4]
OUTPUT scores[1]      # 7
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.
  • You can also loop over every item directly: FOR t IN temps.
AQA pseudo-code
temps ← [18, 21, 17, 22, 19]
total ← 0
FOR i ← 0 TO LEN(temps) - 1
  total ← total + temps[i]
ENDFOR
OUTPUT total

All output 97.

Your answer
?What is output when this code runs?
AQA pseudo-code
nums ← [0, 0, 0, 0, 0]
FOR i ← 0 TO 4
  nums[i] ← i * 3
ENDFOR
OUTPUT nums[2] + nums[4]
Programming · data structures

2D arrays

A 2D arraycolumnrow00121X2Oboard[1,1]
  • 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.
AQA pseudo-code
board ← [[' ', ' ', ' '],
         [' ', ' ', ' '],
         [' ', ' ', ' ']]
board[1][1] ← 'X'    # centre square
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]?
5829
7163
410110
Programming · data structures

Records

A recordPetname'Biscuit'species'dog'age4weight12.5vaccinatedTrueStringIntegerRealBooleanarray: onedata type
  • 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.
FieldData typeExample
nameString'Biscuit'
speciesString'dog'
ageInteger4
weightReal12.5
vaccinatedBooleanTrue
Programming · data structures

Records in code

  • Declare the record's fields and types once, create a record from it, then reach a field with a dot: myPet.age.
AQA pseudo-code
RECORD Pet
  name : String
  species : String
  age : Integer
  weight : Real
ENDRECORD

myPet ← Pet('Biscuit', 'dog', 4, 12.5)
myPet.age ← 5
OUTPUT myPet.name     # Biscuit
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?
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

Calling a proceduremain programwelcome()welcome()end of programprocedurewelcomeshows thewelcome message1234
  • 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.
  • In AQA pseudo-code, a subroutine containing RETURN is a function; one without is a procedure.
AQA pseudo-code
SUBROUTINE welcome()
  OUTPUT 'Welcome to the quiz'
ENDSUBROUTINE

welcome()
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.
  • AQA uses the word parameter both for the names in the definition and for the values passed in.
AQA pseudo-code
SUBROUTINE area(width, height)
  result ← width * height
  RETURN result
ENDSUBROUTINE

roomArea ← area(4, 5)
OUTPUT roomArea    # 20
Your answer
?What is output when this code runs?
AQA pseudo-code
SUBROUTINE change(paid, price)
  RETURN paid - price
ENDSUBROUTINE

OUTPUT change(20, 13) * 2
Programming · subroutines

Local and global variables

Local and globalmain programaddBonusnewScore15localbonus5globalreadable insideaddBonusnewScore?local: exists only while addBonus runs
  • A local variable is declared inside a subroutine: it only exists while that subroutine runs and is only usable inside it.
  • (Context) 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.
AQA pseudo-code
SUBROUTINE addBonus(score)
  newScore ← score + 5    # newScore is local
  RETURN newScore
ENDSUBROUTINE

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.
  • The structured approach: a modular program whose subroutines have clear, well-documented interfaces (parameters, return values, local variables).
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?
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

String positionscard40912293142536475869710811912013114215length = 16
  • A string is a sequence of characters. Each character has a position, its index.
  • The first character is at index 0, so the last is at length − 1.
  • 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.
AQA pseudo-code
card ← '4929123456789012'
OUTPUT LEN(card)
# outputs 16
Programming · strings, files and random numbers

Taking a substring

Taking a substringcard40912293142536475869710811912013114215****9012start index = 16 − 4 = 12result
  • A substring is part of a string, picked out by position.
  • SUBSTRING(start, end, string) uses a start and an end index; both are included.
  • Using the length makes the code work for any card length.
AQA pseudo-code
card ← '4929123456789012'
last4 ← SUBSTRING(LEN(card) - 4, LEN(card) - 1, card)
OUTPUT '****' + last4
Your answer
?Trace this code. What is output?
AQA pseudo-code
word ← 'computer'
OUTPUT SUBSTRING(3, 5, word)
Programming · strings, files and random numbers

More string tools

  • Concatenation joins strings with +: 'computer' + 'science' gives 'computerscience'.
  • POSITION('computer', 'p') gives 3, the index of the first 'p'.
  • CHAR_TO_CODE('a') gives 97; CODE_TO_CHAR(97) gives 'a'.
  • STRING_TO_INT('16') gives 16; INT_TO_STRING, STRING_TO_REAL and REAL_TO_STRING convert the other ways.
Programming · strings, files and random numbers

Random numbers

  • Games, quizzes and simulations need unpredictable values, such as a dice roll.
  • The range you give includes both ends: (1, 6) can give 1, 2, 3, 4, 5 or 6.
AQA pseudo-code
dice ← RANDOM_INT(1, 6)
OUTPUT dice
Calculate
?How many different values can this code output?
AQA pseudo-code
OUTPUT RANDOM_INT(5, 9)
Mission check

?How does an app show only the last 4 digits of your card number?
Mission 6

You typed 41, not 14, for your age. Why did the form accept it?

Make a guess: You typed 41, not 14, for your age. Why did the form accept it?

Programming · validation and authentication

What validation does

ValidationAgeValidation: range 0 to 120,presence41accepted150out of rangeask againnothing enteredask again
  • Validation is an automatic check that input is sensible and follows rules before the program uses it.
  • Invalid data is rejected, and the user is told why and asked again.
  • It stops bad data causing crashes or wrong results later.
  • Validation cannot tell whether data is correct: a mistyped value that still follows the rules gets through.
Programming · validation and authentication

Types of validation check

CheckWhat it testsExample
Range checka number is between limitsage 0 to 120
Length checknumber of characterspassword at least 8
Presence checksomething was entered (not empty)surname not blank
Quick check
?A field for a 4-digit PIN accepted 12. Which check would have rejected it?
Programming · validation and authentication

Writing a validation routine

  • Ask for input, test it, and keep asking until it passes.
  • A loop does this: the user cannot move on with invalid data.
  • This WHILE loop is a range check for 1 to 10.
AQA pseudo-code
score ← STRING_TO_INT(USERINPUT)
WHILE score < 1 OR score > 10
  OUTPUT 'Must be 1 to 10'
  score ← STRING_TO_INT(USERINPUT)
ENDWHILE
Calculate
?The user types three names in this order: an empty string, Al, then Amir. How many times is "Too short" output?
AQA pseudo-code
name ← USERINPUT
WHILE LEN(name) < 3
  OUTPUT 'Too short'
  name ← USERINPUT
ENDWHILE
OUTPUT 'Hello ' + name
Programming · validation and authentication

Authentication

AuthenticationYesNoStartInput usernameInput passwordmatches storeddetails?Output "Accessgranted"Output "Accessdenied"Stop
  • Authentication confirms the identity of a user before they get access, e.g. with a username and password.
  • The program compares what is typed with the stored details.
AQA pseudo-code
username ← USERINPUT
password ← USERINPUT
IF username = 'sam01' AND password = 'Tr33house' THEN
  OUTPUT 'Access granted'
ELSE
  OUTPUT 'Access denied'
ENDIF
Quick check
?A programmer types OR instead of AND in the authentication condition. Which login is now wrongly granted access?
Mission check

?You typed 41, not 14, for your age. Why did the form accept it?
Mission 7

It passed every test. So why did it fail for an 18-year-old?

Make a guess: It passed every test. So why did it fail for an 18-year-old?

Programming · testing

Why test?

  • Testing checks that a program works as intended and meets its requirements.
  • It finds errors so they can be fixed before users meet them.
  • A program can run without crashing and still give wrong results.
  • Passing a few easy tests proves little: choose test data that could expose errors.
Programming · testing

Normal, edge and wrong data

Test datavalid: 1 to 10−1234678912015101115normalerroneousboundaryboundarytenerroneous

For a program that accepts whole numbers from 1 to 10:

  • Normal (typical) data: sensible values that should be accepted, e.g. 5.
  • Boundary (extreme) data: values either side of each limit: 0, 1, 10 and 11.
  • Erroneous data: data that should be rejected, e.g. ten or 15.
Calculate
?The rule is "adult ticket for 18 and over". Tests with ages 10, 30 and 50 all gave the right ticket. Which single age would show the error?
AQA pseudo-code
age ← STRING_TO_INT(USERINPUT)
IF age > 18 THEN
  OUTPUT 'Adult ticket'
ELSE
  OUTPUT 'Child ticket'
ENDIF
Quick check
?A program accepts whole numbers from 1 to 10. A tester types the word ten. What type of test data is this?
Programming · testing

Types of error

  • A syntax error breaks the rules of the language, e.g. a missing bracket. The program cannot be translated or run.
  • A logic error lets the program run, but it gives the wrong or unexpected result, e.g. > where ≥ was needed.
  • Testing with good test data is how you find logic errors.
Quick check
?A program should output the average of three marks, 60, 70 and 80. It runs, but outputs 156.67 because the code says a + b + c / 3. What kind of error is this?
Mission check

?It passed every test. So why did it fail for an 18-year-old?
Recap

Big ideas

  • A variable is a named memory location whose value can change; a constant keeps one value while 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.
  • A string is a sequence of characters, each with an index; the length is how many characters it has. Indexes start at 0.
  • A substring picks out part of a string by position.
  • Concatenation joins strings with +.
  • Random number functions give a value in a range, including both ends.
  • Validation automatically checks input is sensible, e.g. range, length and presence checks, and rejects data that breaks the rules; it cannot check data is correct.
  • Authentication confirms who a user is, e.g. by matching a username and password.
  • Choose test data of every kind: normal, at the edges of the range, and data that should be rejected (normal, boundary, erroneous).
  • Syntax errors stop a program running; logic errors give wrong results.
🏆

Mini-lesson complete!

⭐⭐⭐

You've worked through Programming for AQA GCSE Computer Science (8525). 🎉

  1. Mission 1How does a game turn 135 seconds into 2:15 on its timer?
  2. Mission 2How can a few lines of code print all 100 times-table answers?
  3. Mission 3How can one name hold all 9 squares of a noughts and crosses board?
  4. Mission 4Two subroutines both use a variable called total. Why no clash?
  5. Mission 5How does an app show only the last 4 digits of your card number?
  6. Mission 6You typed 41, not 14, for your age. Why did the form accept it?
  7. Mission 7It passed every test. So why did it fail for an 18-year-old?

Your stars: 0 / 0

Next: test yourself in the Verify stage.

📣 Smashed it? Share your score

Challenge a mate to beat your stars, or show a parent how you got on.

→ Back to GCSE Computer Science · AQA

Specification points and sources for this lesson are listed at the bottom of the page.

Sources (6)

The specification points and sources behind this lesson.

  1. 1 AQA 8525 spec v1.3, 3.2.1, 3.2.2, 3.2.3, 3.2.7, 3.2.8; AQA pseudo-code guide (2024)
  2. 2 AQA 8525 spec v1.3, 3.2.2, 3.2.4, 3.2.5; AQA pseudo-code guide (2024)
  3. 3 AQA 8525 spec v1.3, 3.2.6; AQA pseudo-code guide (2024), Arrays and Records
  4. 4 AQA 8525 spec v1.3, 3.2.2, 3.2.10; AQA pseudo-code guide (2024), Subroutines
  5. 5 AQA 8525 spec v1.3, 3.2.8, 3.2.9; AQA pseudo-code notes (string handling, string and character conversion, RANDOM_INT)
  6. 6 AQA 8525 spec v1.3, 3.2.11; AQA pseudo-code notes