Pearson Edexcel International GCSE (9–1)
Paper
reference 4CP0/02
Computer Science
Component 2
Pseudocode command set
Resource Booklet
Do not return this Booklet with the question paper.
Turn over
*P72406A*
P72406A
©2022 Pearson Education Ltd.
Q:1/1/1/e2/
Pseudocode command set
Questions in the written examination that involve code will use this pseudocode
for clarity and consistency. However, students may answer questions using any
valid method.
Data types
INTEGER
REAL
BOOLEAN
CHARACTER
Type coercion
Type coercion is automatic if indicated by context. For example 3 + 8.25 = 11.25
(integer + real = real)
Mixed mode arithmetic is coerced like this:
INTEGER REAL
INTEGER INTEGER REAL
REAL REAL REAL
Coercion can be made explicit. For example, RECEIVE age FROM (INTEGER) KEYBOARD
assumes that the input from the keyboard is interpreted as an INTEGER, not a STRING.
Constants
The value of constants can only ever be set once. They are identified by the keyword
CONST. Two examples of using a constant are shown.
CONST REAL PI
SET PI TO 3.14159
SET circumference TO radius * PI * 2
Data structures
ARRAY
STRING
Indices start at zero (0) for all data structures.
All data structures have an append operator, indicated by &.
Using & with a STRING and a non-STRING will coerce to STRING. For example, SEND ‘Fred’
& age TO DISPLAY, will display a single STRING of ‘Fred18’.
2 P72406A
Identifiers
Identifiers are sequences of letters, digits and ‘_’, starting with a letter, for example:
MyValue, myValue, My_Value, Counter2
Functions
LENGTH()
For data structures consisting of an array or string.
RANDOM(n)
This generates a random number from 0 to n.
Comments
Comments are indicated by the # symbol, followed by any text.
A comment can be on a line by itself or at the end of a line.
Devices
Use of KEYBOARD and DISPLAY are suitable for input and output.
Additional devices may be required, but their function will be obvious from the context.
For example, CARD_READER and MOTOR are two such devices.
Notes
In the following pseudocode, the < > indicates where expressions or values need to be
supplied. The < > symbols are not part of the pseudocode.
P72406A 3
Turn over
Variables and arrays
Syntax Explanation of syntax Example
SET Counter TO 0
SET Variable TO <value> Assigns a value to a variable.
SET MyString TO ‘Hello world’
Computes the value of an
SET Sum TO Score + 10
SET Variable TO <expression> expression and assigns to
SET Size to LENGTH(Word)
a variable.
Assigns a value to an element SET ArrayClass[1] TO ‘Ann’
SET Array[index] TO <value>
of a one-dimensional array. SET ArrayMarks[3]TO 56
Initialises a one-dimensional
SET Array TO [<value>, ...] SET ArrayValues TO [1, 2, 3, 4, 5]
array with a set of values.
SET Array [RowIndex, Assigns a value to an element
SET ArrayClassMarks[2,4] TO 92
ColumnIndex] TO <value> of a two dimensional array.
Selection
Syntax Explanation of syntax Example
IF <expression> THEN IF Answer = 10 THEN
If <expression> is true then
<command> SET Score TO Score + 1
command is executed.
END IF END IF
IF <expression> THEN IF Answer = ‘correct’ THEN
If <expression> is true then first
<command> SEND ‘Well done’ TO DISPLAY
<command> is executed,
ELSE ELSE
otherwise second
<command> SEND ‘Try again’ TO DISPLAY
<command> is executed.
END IF END IF
4 P72406A
Repetition
Syntax Explanation of syntax Example
WHILE <condition> DO Pre-conditioned loop. Executes WHILE Flag = 0 DO
<command> <command> whilst SEND ‘All well’ TO DISPLAY
END WHILE <condition> is true. END WHILE
Post-conditioned loop.
REPEAT Executes REPEAT
<command> <command> until <condition> SET Go TO Go + 1
UNTIL <expression> is true. The loop must execute UNTIL Go = 10
at least once.
Count controlled loop. The
REPEAT <expression> TIMES REPEAT 100-Number TIMES
number of times <command>
<command> SEND ‘*’ TO DISPLAY
is executed is determined by
END REPEAT END REPEAT
the expression.
FOR <id> FROM <expression>
Count controlled loop. FOR Index FROM 1 TO 10 DO
TO
Executes SEND ArrayNumbers[Index]
<expression> DO
<command> a fixed number of TO DISPLAY
<command>
times. END FOR
END FOR
FOR <id> FROM <expression>
TO FOR Index FROM 1 TO 500 STEP
<expression> STEP Count controlled loop using 25 DO
<expression> DO a step. SEND Index TO DISPLAY
<command> END FOR
END FOR
SET WordsArray TO [‘The’, ‘Sky’,
‘is’, ‘grey’]
FOR EACH <id> FROM SET Sentence to ‘‘
Count controlled loop.
<expression> DO FOR EACH Word FROM
Executes for each element of
<command> WordsUArray DO
an array.
END FOREACH SET Sentence TO Sentence &
Word & ‘ ‘
END FOREACH
P72406A 5
Turn over
Input/output
Syntax Explanation of syntax Example
SEND ‘Have a good day.’ TO
SEND <expression> TO DISPLAY Sends output to the screen.
DISPLAY
RECEIVE Name FROM (STRING)
KEYBOARD
RECEIVE <identifier> FROM
RECEIVE LengthOfJourney
(type) Reads input of specified type.
FROM (INTEGER) CARD_READER
<device>
RECEIVE YesNo FROM
(CHARACTER) CARD_READER
File handling
Syntax Explanation of syntax Example
Reads in a record from a <file>
and assigns to a <variable>.
READ <File> <record> READ MyFile.doc Record
Each READ statement reads a
record from the file.
Writes a record to a file.
WRITE MyFile.doc Answer1,
WRITE <File> <record> Each WRITE statement writes a
Answer2, ‘xyz 01’
record to the file.
Subprograms
Syntax Explanation of syntax Example
PROCEDURE CalculateAverage
PROCEDURE <id>
(Mark1, Mark2, Mark3)
(<parameter>, …)
BEGIN PROCEDURE
BEGIN PROCEDURE Defines a procedure.
SET Avg to (Mark1 + Mark2 +
<command>
Mark3)/3
END PROCEDURE
END PROCEDURE
FUNCTION AddMarks (Mark1,
FUNCTION <id>
Mark2, Mark3)
(<parameter>, …)
BEGIN FUNCTION
BEGIN FUNCTION
Defines a function. SET Total to (Mark1 + Mark2 +
<command>
Mark3)/3
RETURN <expression>
RETURN Total
END FUNCTION
END FUNCTION
Calls a procedure or a
<id> (<parameter>, …) Add (FirstMark, SecondMark)
function.
6 P72406A
Arithmetic operators
Symbol Description
+ Add
- Subtract
/ Divide
* Multiply
^ Exponent
MOD Modulo
DIV Integer division
Relational operators
Symbol Description
= equal to
<> not equal to
> greater than
>= greater than or equal to
< less than
<= less than or equal to
Logical operators
Symbol Description
Returns true if both conditions
AND
are true.
Returns true if any of the
OR
conditions are true.
Reverses the outcome of the
NOT expression; true becomes false,
false becomes true.
P72406A 7
BLANK PAGE
8 P72406A