Pseudocode command set
Questions in the written examination that involve code will use this pseudocode
for clarity and consistancy. 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 = 1125
(integer + real = real)
Mixed mode arithmeticis 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 canstant are shown,
‘CONST REAL PI
SET PITO 3.14159
SET circumference TO radius * PI* 2Data 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 nan-STRING will coerce to STRING. For example, SEND ‘Fred’
age TO DISPLAY, will display a single STRING of Fradi8"
Identifiers
Identifiers are sequences of letters, digits and’
MyValue, myValue, My Value, Counter2
starting with a letter, for example:
Functions
LENGTHO
For data structures consisting of an array or string.
RANDOMIn)
This generates a random number from 0 to n,
‘Comments
‘Comments are indicated by the # symbol, followed by any text.
‘Acomment 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 pseudocode on the following pages, the < > indicates where expressions or values
need to be supplied. The < > symbols are not part of the pseudocode.Variables and arrays
syntax
SET Variable TO
SET Variable TO
Explanation of syntax
Assignsa value toa variable.
Computes the value of an
expression and assigns to
a variable,
Example
SET Counter TO 0
SET MyString TO ‘Hello world’
SET Sum TO Score + 10
SET Size to LENGTH(Word)
SET Array[index) TO
Assignsa value to an element
of a one-dimensional array.
SET ArrayClass{1] TO ‘Anni
SET ArrayMarks[3]TO 56
SET Array TO [,
SET Array [Rowindex,
Columnindex] TO
Initialises @ one-dimensional
array with a set of values.
Assignsa value toan element
of a two dimensional array.
SET AniayValues TO [1,2,3, 4, 5]
SET ArrayClassMarks{2,4]TO 92
Selection
Syntax
IF THEN
ENDIF
IF THEN
ELSE
‘
ENDIF
Explanation of syntax
If is true then
command is executed.
If is true then first
command? is executed,
otherwise second,
is executed.
Example
IF Answer = 10 THEN
SET ScoreTO Score + 1
END IF
IF Answer =‘correct” THEN
‘SEND ‘Well done’ TO DISPLAY
ELSE
‘SEND "Try again’ TO DISPLAY
END IFRepetition
Syntax
Explanation of syntax
Example
END WHILE
REPEAT
UNTIL
REPEAT TIMES
‘
END REPEAT
Preconditioned loop. Executes
whilst
is true.
Post-conditioned loop.
Executes
until
is true. The loop must execute
at least once.
Count controlled loop. The
number of times
is executed is determined by
the expression.
WHILE Flag =0 DO
SEND ‘All well’ TO DISPLAY
END WHILE
REPEAT
‘SET GoTO Go +1
UNTIL Go = 10
REPEAT 100-Number TIMES
‘SEND "TO DISPLAY
END REPEAT
FOR FROM
To
DO
END FOR
FOR FROM
10
STEP
DO
END FOR,
FOR EACH FROM
DO
END FOREACH
Count controlled loop.
Executes
@ fixed number of
times.
Count controlled loop using
a step.
Count controlled loop.
Executes for each element of
an array.
FOR Index FROM 1 T0 100
SEND ArrayNumbers{index]
TO DISPLAY
END FOR
FOR Index FROM 1 10 500 STEP
250
‘SEND Index TO DISPLAY
END FOR
SET WordsArray TO [’Thes'Sky,
'isygrey’)
SET Sentence to"
FOR EACH Word FROM
WordsUArray DO
SET Sentence TO Sentence &
Word &'"
END FOREACHSEND ‘Have a good day’T0
SEND TO DISPLAY | Sends output to the screen. Bicpt ay.
RECEIVE Name FROM (STRING)
identi KEYBOARD.
necenve FROM onde inpuofepectted RECEIVE Lengthotlourney
ype) ‘input of specified type. Exo (INTEGER) CARD_READER
RECEIVE YesNo FROM
(CHARACTER) CARD_READER
READ
Reads in a record from a
and assigns to a .
Each READ statement reads a
record from the file.
READ Myfile.doc Record
WRITE
‘Writes 2 record to a file.
Each WRITE statement writes a
record to thefile,
WRITE MyFile.doc Answert,
Answer2,'xy2 01"Subprograms
Syntax
Explanation of syntax
Example
PROCEDURE
(cparameter>, ...]
BEGIN PROCEDURE
‘
END PROCEDURE
Defines a procedure.
PROCEDURE CalculateAverage
(Mark1, Mark2, Mark3)
BEGIN PROCEDURE
SET Avg to (Mark + Mark2 +
Mark3|/3
END PROCEDURE
FUNCTION
(cparameter>, ...]
BEGIN FUNCTION
RETURN
END FUNCTION
(,
Defines a function.
Calls a procedure or a
function
FUNCTION AddMarks (Mark!
Mark2, Mark3)
BEGIN FUNCTION
‘SET Total to (Marki + Mark2 +
Mark3i/3.
RETURN Total
END FUNCTION
‘Add (FirstMark, SecondMark)+ Add
- Subtract
/ bi
. Multiply
a Exponent
MoD Modulo
ow Integer division
= equal to
° not equal to
> greater than
greater than or equal 10
< less than
less than or equal toReturns true if both conditions
AND
are true.
Returns true fany ofthe
OR conditions are true.
Reverses the outcome of the
Nor expression; true becomes false,
false becomes true.