Notes and guidance: Pseudo-code
The pseudo-code is described in this resource to help students prepare for their AQA GCSE Computer Science exam (8525/1).
We will use this consistent style of pseudo-code in all assessment material. This will ensure that, with enough preparation, students will
understand the syntax of the pseudo-code used in assessments. Students do not have to use this style of pseudo-code in their own work
or written assessments, although they are free to do so. The only direction to students when answering questions or describing algorithms
written in pseudo-code is that their code is clear, consistent and unambiguous.
This resource may be updated as required and the latest version will always be available on our website. It is not confidential and can be
freely shared with students.
General Syntax
• IntExp, RealExp, BoolExp, CharExp and StringExp mean any expression which can be evaluated to an integer, real, Boolean
(False or True), character or string respectively.
• Exp means any expression.
• Emboldened pseudo-code is used to indicate the keywords/operators.
• Exam paper questions will assume that indexing for arrays and strings starts at 0 unless specifically stated otherwise.
© 2024 AQA 1 of 15
Comments
Single line comments # comment
# comment
Multi-line comments
# comment and so on
Variables and constants
a ← 3
Variable assignment Identifier ← Exp b ← a + 1
c ← 'Hello'
CONSTANT PI ← 3.141
CONSTANT CLASS_SIZE ← 23
Constant assignment CONSTANT IDENTIFIER ← Exp
# Names of constants will always be
# written in capitals
© 2024 AQA 2 of 15
Arithmetic operations
Used in the normal way with brackets to indicate
precedence where needed. For example, a + b * c
+ would multiply b and c together and then add the
- result to a, whereas (a + b) * c would add a and b
Standard arithmetic operations together and then multiply the result by c.
*
/
The / symbol is used instead of ÷ for division
(for integer division use DIV)
9 DIV 5 evaluates to 1
Integer division IntExp DIV IntExp 5 DIV 2 evaluates to 2
8 DIV 4 evaluates to 2
9 MOD 5 evaluates to 4
Modulus operator IntExp MOD IntExp 5 MOD 2 evaluates to 1
8 MOD 4 evaluates to 0
© 2024 AQA 3 of 15
Relational operators for types that can be clearly ordered
4 < 6
Less than Exp < Exp 'A' < 'B'
'adam' < 'adele'
Greater than Exp > Exp 4.1 > 4.0
Equal to Exp = Exp 3 = 3
Not equal to Exp ≠ Exp qty ≠ 7
Exp ≤ Exp 3 ≤ 4
Less than or equal to
4 ≤ 4
4 ≥ 3
Greater than or equal to Exp ≥ Exp
4.5 ≥ 4.5
Boolean operations
Logical AND BoolExp AND BoolExp (3 = 3) AND (3 ≤ 4)
Logical OR BoolExp OR BoolExp (x < 1) OR (x > 9)
Logical NOT NOT BoolExp NOT (a < b)
© 2024 AQA 4 of 15
Indefinite (condition controlled) iteration
a ← 1
REPEAT
REPEAT
REPEAT-UNTIL (repeat the OUTPUT a
# statements here
statements until the Boolean a ← a + 1
expression is True). UNTIL a = 4
UNTIL BoolExp
# will output 1, 2, 3
a ← 1
WHILE BoolExp
WHILE a < 4
WHILE-ENDWHILE (while the OUTPUT a
# statements here
Boolean expression is True, repeat a ← a + 1
the statements). ENDWHILE
ENDWHILE
# will output 1, 2, 3
© 2024 AQA 5 of 15
Definite (count controlled) iteration
FOR Identifier ← IntExp TO IntExp [STEP IntExp] FOR a ← 1 TO 3
OUTPUT a
# statements here ENDFOR
# will output 1, 2, 3
ENDFOR
FOR-TO-[STEP]-ENDFOR FOR a ← 1 TO 5 STEP 2
(If STEP IntExp is # If STEP IntExp is omitted the step value is 1. OUTPUT a
# Note that in STEP IntExp the value of IntExp
missing it is considered to ENDFOR
# can be negative (see the third example)
be 1). # will output 1, 3, 5
# The loop counter variable (a in the examples)
FOR a ← 5 TO 1 STEP -2
# is always declared in the FOR statement and
OUTPUT a
# does not exist after the loop has finished.
# It never takes any value above the upper (or ENDFOR
# lower) limit given in the statement. # will output 5, 3, 1
length ← 0
FOR char IN message
length ← length + 1
FOR Identifier IN StringExp ENDFOR
# will calculate the
FOR-IN-ENDFOR # statements here # number of characters
(repeat the statements # in message
ENDFOR
the number of times
that there are characters reversed ← ''
# The loop variable (char in the examples) FOR char IN message
in a string). # is always declared in the FOR statement and reversed ← char + reversed
# does not exist after the loop has finished.
ENDFOR
OUTPUT reversed
# will output the
# string in reverse
© 2024 AQA 6 of 15
Selection
IF-THEN-ENDIF (execute the IF BoolExp THEN a ← 1
IF (a MOD 2) = 0 THEN
statements only if the Boolean # statements here
OUTPUT 'even'
expression is True). ENDIF
ENDIF
a ← 1
IF-THEN-ELSE-ENDIF (execute IF BoolExp THEN
the statements following the
IF (a MOD 2) = 0 THEN
# statements here
OUTPUT 'even'
THEN if the Boolean expression ELSE
ELSE
is True, otherwise execute the # statements here
OUTPUT 'odd'
statements following the ELSE). ENDIF
ENDIF
a ← 1
IF (a MOD 4) = 0 THEN
IF BoolExp THEN OUTPUT 'multiple of 4'
NESTED IF-THEN-ELSE ENDIF ELSE
(use nested versions of the # statements here
IF (a MOD 4) = 1 THEN
ELSE
above to create more complex OUTPUT 'leaves a remainder of 1'
IF BoolExp THEN
conditions). ELSE
# statements here
IF (a MOD 4) = 2 THEN
ELSE
Note that IF statements can be OUTPUT 'leaves a remainder of 2'
# statements here
ELSE
nested inside the THEN part, the ENDIF OUTPUT 'leaves a remainder of 3'
ELSE part or both. ENDIF ENDIF
ENDIF
ENDIF
© 2024 AQA 7 of 15
Selection (continued)
a ← 1
IF BoolExp THEN IF (a MOD 4) = 0 THEN
# statements here OUTPUT 'multiple of 4'
IF-THEN-ELSE IF ENDIF (removes
ELSE IF BoolExp THEN ELSE IF (a MOD 4) = 1 THEN
# statements here OUTPUT 'leaves a remainder of 1'
the need for multiple
# possibly more ELSE IFs ELSE IF (a MOD 4) = 2 THEN
indentation levels). ELSE OUTPUT 'leaves a remainder of 2'
# statements here ELSE
ENDIF OUTPUT 'leaves a remainder of 3'
ENDIF
© 2024 AQA 8 of 15
Arrays
Assignment Identifier ← [Exp, … ,Exp] primes ← [2, 3, 5, 7, 11, 13]
primes[0]
# evaluates to 2
Accessing an element Identifier[IntExp]
(questions on exam papers will start indexing
at 0 unless specifically stated otherwise)
primes[5] ← 17
Updating an element Identifier[IntExp] ← Exp
# array is now [2, 3, 5, 7, 11, 17]
table ← [[1, 2],[2, 4],[3, 6],[4, 8]]
table[3][1]
Accessing an element in a
Identifier[IntExp][IntExp]
two-dimensional array # evaluates to 8 as second element
# (with index 1) of fourth array
# (with index 3) in table is 8
table[3][1] ← 16
# table is now
Updating an element in a
Identifier[IntExp][IntExp] ← Exp #[ [1, 2],
two-dimensional array
# [2, 4],
# [3, 6],
# [4, 16] ]
© 2024 AQA 9 of 15
Arrays (continued)
LEN(primes)
# evaluates to 6 using example above
LEN(table)
Array length LEN(Identifier)
# evaluates to 4 using example above
LEN(table[0])
# evaluates to 2 using example above
FOR-IN-ENDFOR
(repeat the statements primes ← [2, 3, 5, 7, 11, 13]
the number of times FOR Identifier IN array total ← 0
that there are elements
FOR prime IN primes
# statements here total ← total + prime
in an array)
ENDFOR
ENDFOR OUTPUT 'Sum of the values in primes is'
NOTE: array items OUTPUT total
cannot be modified
using this method
© 2024 AQA 10 of 15
Records
RECORD Record_identifier RECORD Car
make : String
field1 : <data type> model : String
Record declaration field2 : <data type> reg : String
… price : Real
noOfDoors : Integer
ENDRECORD ENDRECORD
varName ← Record_identifier(value1, myCar ← Car('Ford', 'Focus', 'DX17 GYT',
Variable Instantiation
value2, …) 1399.99, 5)
[Link] ← 'Fiesta'
Assigning a value to a field in a
[Link] ← Exp # The model field of the myCar
record # record is assigned the value
# 'Fiesta'.
OUTPUT [Link]
Accessing values of fields
[Link]
within records # Will output the value stored in the
# model field of the myCar record
© 2024 AQA 11 of 15
Subroutines
Note: for the purposes of this pseudo-code definition subroutines that contain a RETURN keyword are functions. Those that do not contain a RETURN
keyword are procedures.
SUBROUTINE showAdd(a, b)
result ← a + b
OUTPUT result
SUBROUTINE Identifier(parameters) ENDSUBROUTINE
Subroutine definition # statements here
SUBROUTINE sayHi()
ENDSUBROUTINE OUTPUT 'Hi'
ENDSUBROUTINE
# Both of these subroutines are procedures
SUBROUTINE add(a, b)
result ← a + b
RETURN result
Subroutine return value RETURN Exp
ENDSUBROUTINE
# This subroutine is a function
# Subroutines without a return value
showAdd(2, 3)
Identifier(parameters)
Calling subroutines
# Subroutines with a return value
answer ← add(2, 3) * 6
Identifier ← Identifier(parameters)
© 2024 AQA 12 of 15
String handling
LEN('computer science')
String length LEN(StringExp)
# evaluates to 16(including space)
POSITION('computer science', 'm')
# evaluates to 2 (as with arrays
Position of a character POSITION(StringExp, CharExp)
# exam papers will start
# indexing at 0 unless
# specifically stated otherwise)
Substring (the substring is
created by the first parameter
indicating the start position
SUBSTRING(2, 9, 'computer science')
within the string, the second
SUBSTRING(IntExp, IntExp, StringExp)
parameter indicating the final
position within the string and the # evaluates to 'mputer s'
third parameter being the string
itself).
'computer' + 'science'
Concatenation StringExp + StringExp
# evaluates to 'computerscience'
© 2024 AQA 13 of 15
String and Character Conversion
STRING_TO_INT('16')
Converting string to integer STRING_TO_INT(StringExp)
# evaluates to the integer 16
STRING_TO_REAL('16.3')
Converting string to real STRING_TO_REAL(StringExp)
# evaluates to the real 16.3
INT_TO_STRING(16)
Converting integer to string INT_TO_STRING(IntExp)
# evaluates to the string '16'
REAL_TO_STRING(16.3)
Converting real to string REAL_TO_STRING(RealExp)
# evaluates to the string '16.3'
CHAR_TO_CODE('a')
Converting character to
CHAR_TO_CODE(CharExp)
character code # evaluates to 97 using ASCII/Unicode
CODE_TO_CHAR(97)
Converting character code to
CODE_TO_CHAR(IntExp)
character # evaluates to 'a' using ASCII/Unicode
© 2024 AQA 14 of 15
Input/output
User input USERINPUT a ← USERINPUT
OUTPUT a
OUTPUT a, g
Output OUTPUT StringExp, … StringExp
# The output statement can be followed by
multiple StringExp separated by commas
Random number generation
diceRoll ← RANDOM_INT(1, 6)
Random integer generation
(between two integers Identifier ← RANDOM_INT(IntExp, IntExp) # will randomly generate an
inclusively). # integer between 1 and 6
# inclusive
© AQA 2024 and its licensors. All rights reserved. 15 of 15