Pseudocode Command Set - GCSE Computers
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
INTEGERREALBOOLEANCHARACTER
Type coercion
Type coercion is automatic if indicated by context. For example 3 + 8.25 = 11.25 (integer + real = real)
Arithmetic Coercion
Mixed mode arithmetic coercion
| INTEGER | REAL | |
|---|---|---|
| INTEGER | INTEGER | REAL |
| REAL | REAL | REAL |
Explicit Coercion & Constants
Explicit Coercion
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
ARRAYSTRING- 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'.
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
KEYBOARDandDISPLAYare suitable for input and output. - Additional devices may be required, but their function will be obvious from the context. For example,
CARD_READERandMOTORare 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.
Pseudocode Syntax Reference
Variables and arrays
| Syntax | Explanation of syntax | Example |
|---|---|---|
SET Variable TO <value> | Assigns a value to a variable. | SET Counter TO 0 |
SET Variable TO <expression> | Computes the value of an expression and assigns to a variable. | SET Sum TO Score + 10 |
SET Array[index] TO <value> | Assigns a value to an element of a one-dimensional array. | SET ArrayClass[1] TO 'Ann' |
SET Array TO [<value>, ...] | Initialises a one-dimensional array with a set of values. | SET ArrayValues TO [1, 2, 3, 4, 5] |
SET Array[RowIndex, ColumnIndex] TO <value> | Assigns a value to an element of a two-dimensional array. | SET ArrayClassMarks[2,4] TO 92 |
Selection
| Syntax | Explanation of syntax | Example |
|---|---|---|
| If <expression> is true then command is executed. | |
| If <expression> is true then first <command> is executed, otherwise second <command> is executed. | |
Repetition
| Syntax | Explanation of syntax | Example |
|---|---|---|
| Pre-conditioned loop. Executes <command> whilst <condition> is true. | |
| Post-conditioned loop. Executes <command> until <condition> is true. The loop must execute at least once. | |
| Count controlled loop. The number of times <command> is executed is determined by the expression. | |
| Count controlled loop. Executes <command> a fixed number of times. | |
| Count controlled loop using a step. | |
| Count controlled loop. Executes for each element of an array. | |
Input/output
| Syntax | Explanation of syntax | Example |
|---|---|---|
SEND <expression> TO DISPLAY | Sends output to the screen. | SEND 'Have a good day.' TO DISPLAY |
RECEIVE <identifier> FROM (type) <device> | Reads input of specified type. | RECEIVE Name FROM (STRING) KEYBOARD |
File handling
| Syntax | Explanation of syntax | Example |
|---|---|---|
READ <File> <record> | Reads in a record from a <file> and assigns to a <variable>. Each READ statement reads a record from the file. | READ MyFile.doc Record |
WRITE <File> <record> | Writes a record to a file. Each WRITE statement writes a record to the file. | WRITE MyFile.doc Answer1, Answer2, 'xyz 01' |
Subprograms
| Syntax | Explanation of syntax | Example |
|---|---|---|
| Defines a procedure. | |
| Defines a function. | |
<id> (<parameter>, ...) | Calls a procedure or a function. | Add (FirstMark, SecondMark) |
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. |