0% found this document useful (0 votes)
37 views

Edexcel Pseudocode 1

Uploaded by

zem
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Edexcel Pseudocode 1

Uploaded by

zem
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Appendix 1: Pseudo-code command set

Questions in the written examination that involve code will use this pseudo-code 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’.

Pearson Edexcel Level 1/Level 2 GCSE (9-1) in Computer Science – Specification 41


Issue 1 – October 2015 © Pearson Education Limited 2015
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 pseudo-code, the < > indicates where expressions or values need to be supplied.
The < > symbols are not part of the pseudo-code.

42 Pearson Edexcel Level 1/Level 2 GCSE (9-1) in Computer Science – Specification


Issue 1 – October 2015 © Pearson Education Limited 2015
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 expression and assigns SET Sum TO Score + 10


SET Variable TO <expression>
to a variable. SET Size to LENGTH(Word)

Assigns a value to an element of a one- SET ArrayClass[1] TO ‘Ann’


SET Array[index] TO <value>
dimensional array. SET ArrayMarks[3]TO 56

Initialises a one-dimensional array with a set of


SET Array TO [<value>, …] SET ArrayValues TO [1, 2, 3, 4, 5]
values.

Assigns a value to an element of a two


SET Array [RowIndex, ColumnIndex] TO <value> SET ArrayClassMarks[2,4] TO 92
dimensional array.

Pearson Edexcel Level 1/Level 2 GCSE (9-1) in Computer Science – Specification 43


Issue 1 – October 2015 © Pearson Education Limited 2015
Selection

Syntax Explanation of syntax Example

IF <expression> THEN IF Answer = 10 THEN


<command> If <expression> is true then command is executed. SET Score TO Score + 1
END IF END IF

IF <expression> THEN IF Answer = ‘correct’ THEN


<command> If <expression> is true then first SEND ‘Well done’ TO DISPLAY
ELSE <command> is executed, otherwise second ELSE
<command> <command> is executed. SEND ‘Try again’ TO DISPLAY
END IF END IF

44 Pearson Edexcel Level 1/Level 2 GCSE (9-1) in Computer Science – Specification


Issue 1 – October 2015 © Pearson Education Limited 2015
Repetition

Syntax Explanation of syntax Example

WHILE <condition> DO WHILE Flag = 0 DO


Pre-conditioned loop. Executes
<command> SEND ‘All well’ TO DISPLAY
<command> whilst <condition> is true.
END WHILE END WHILE

REPEAT Post-conditioned loop. Executes REPEAT


<command> <command> until <condition> is true. The loop SET Go TO Go + 1
UNTIL <expression> must execute at least once. UNTIL Go = 10

REPEAT <expression> TIMES Count controlled loop. The number of times REPEAT 100-Number TIMES
<command> <command> is executed is determined by the SEND ‘*’ TO DISPLAY
END REPEAT expression. END REPEAT

FOR <id> FROM <expression> TO


FOR Index FROM 1 TO 10 DO
<expression> DO Count controlled loop. Executes
SEND ArrayNumbers[Index] TO DISPLAY
<command> <command> a fixed number of times.
END FOR
END FOR

FOR <id> FROM <expression> TO


FOR Index FROM 1 TO 500 STEP 25 DO
<expression> STEP <expression> DO
Count controlled loop using a step. SEND Index TO DISPLAY
<command>
END FOR
END FOR

SET WordsArray TO [‘The’, ‘Sky’, ‘is’, ‘grey’]


FOR EACH <id> FROM <expression> DO SET Sentence to ‘‘
Count controlled loop. Executes for each element
<command> FOR EACH Word FROM WordsUArray DO
of an array.
END FOREACH SET Sentence TO Sentence & Word & ‘ ‘
END FOREACH

Pearson Edexcel Level 1/Level 2 GCSE (9-1) in Computer Science – Specification 45


Issue 1 – October 2015 © Pearson Education Limited 2015
Input/output

Syntax Explanation of syntax Example

SEND <expression> TO DISPLAY Sends output to the screen. SEND ‘Have a good day.’ TO DISPLAY

RECEIVE Name FROM (STRING) KEYBOARD


RECEIVE <identifier> FROM (type) RECEIVE LengthOfJourney FROM (INTEGER)
Reads input of specified type.
<device> CARD_READER
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 <File> <record> Each WRITE statement writes a record to the WRITE MyFile.doc Answer1, Answer2, ‘xyz 01’
file.

46 Pearson Edexcel Level 1/Level 2 GCSE (9-1) in Computer Science – Specification


Issue 1 – October 2015 © Pearson Education Limited 2015
Subprograms

Syntax Explanation of syntax Example

PROCEDURE CalculateAverage (Mark1, Mark2,


PROCEDURE <id> (<parameter>, …)
Mark3)
BEGIN PROCEDURE
Defines a procedure. BEGIN PROCEDURE
<command>
SET Avg to (Mark1 + Mark2 + Mark3)/3
END PROCEDURE
END PROCEDURE

FUNCTION <id> (<parameter>, …) FUNCTION AddMarks (Mark1, Mark2, Mark3)


BEGIN FUNCTION BEGIN FUNCTION
<command> Defines a function. SET Total to (Mark1 + Mark2 + Mark3)/3
RETURN <expression> RETURN Total
END FUNCTION END FUNCTION

<id> (<parameter>, …) Calls a procedure or a function. Add (FirstMark, SecondMark)

Pearson Edexcel Level 1/Level 2 GCSE (9-1) in Computer Science – Specification 47


Issue 1 – October 2015 © Pearson Education Limited 2015
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
AND Returns true if both conditions
are true.

OR Returns true if any of the


conditions are true.

NOT Reverses the outcome of the


expression; true becomes
false, false becomes true.

48 Pearson Edexcel Level 1/Level 2 GCSE (9-1) in Computer Science – Specification


Issue 1 – October 2015 © Pearson Education Limited 2015

You might also like