Pseudocode Notes
Pseudocode Notes
Data Types
The following keywords are used to designate some basic data types:
Literals
Literals of the above data types are written as follows:
Always written with at least one digit on either side of the decimal point, zeros being
• Real
added if necessary, e.g. 4.7, 0.3, -4.0, 0.0
• Char A single character delimited by single quotes e.g. ꞌxꞌ, ꞌCꞌ, ꞌ@ꞌ
Delimited by double quotes. A string may contain no characters (i.e. the empty string)
• String
e.g. "This is a string", ""
This will normally be written in the format dd/mm/yyyy. However, it is good practice to
• Date state explicitly that this value is of data type DATE and to explain the format (as the
convention for representing dates varies across the world).
Identifiers
Identifiers (the names given to variables, constants, procedures and functions) are in mix case. They can only
contain letters (A–Z, a–z), digits (0–9) and the underscore character ( _ ). They must start with a letter and not
a digit. Accented letters should not be used.
As in programming, it is good practice to use identifier names that describe the variable, procedure or function
they refer to. Single letters may be used where these are conventional (such as i and j when dealing with
array indices, or X and Y when dealing with coordinates) as these are made clear by the convention.
Identifiers should be considered case insensitive, for example, Countdown and CountDown should not
be used as separate variables.
Variable declarations
It is good practice to declare variables explicitly in pseudocode.
Constants
It is good practice to use constants if this makes the pseudocode more readable, as an identifier is more
meaningful in many cases than a literal. It also makes the pseudocode easier to update if the value of the
constant changes.
Constants are normally declared at the beginning of a piece of pseudocode (unless it is desirable to restrict
the scope of the constant).
Constants are declared by stating the identifier and the literal value in the following format:
Only literals can be used as the value of a constant. A variable, another constant or an expression must
never be used.
Assignments
The assignment operator is ← .
<identifier> ← <value>
The identifier must refer to a variable (this can be an individual element in a data structure such as an array or
an abstract data type). The value may be any expression that evaluates to a value of the same data type as
the variable.
Example – assignments
Counter ← 0
Counter ← Counter + 1
TotalToPay ← NumberOfHours * HourlyRate
Arrays
Declaring arrays
Arrays are considered to be fixed-length structures of elements of identical data type, accessible by
consecutive index (subscript) numbers. It is good practice to explicitly state what the lower bound of the
array (i.e. the index of the first element) is because this defaults to either 0 or 1 in different systems.
Generally, a lower bound of 1 will be used.
Using arrays
Array index values may be literal values or expressions that evaluate to a valid integer value.
Arrays can be used in assignment statements (provided they have same size and data type). The following is
therefore allowed:
A statement should not refer to a group of array elements individually. For example, the following
construction should not be used.
Instead, an appropriate loop structure is used to assign the elements individually. For example:
A composite data type is a collection of data that can consist of different data types, grouped under one
identifier. The composite type should be declared as follows:
TYPE <identifier1>
DECLARE <identifier2> : <data type>
DECLARE <identifier3> : <data type>
...
ENDTYPE
TYPE Student
DECLARE Surname : STRING
DECLARE FirstName : STRING
DECLARE DateOfBirth : DATE
DECLARE YearGroup : INTEGER
DECLARE FormGroup : CHAR
ENDTYPE
Using user-defined data types
When a user-defined data type has been defined it can be used in the same way as any other data type in
declarations.
Variables of a user-defined data type can be assigned to each other. Individual data items are accessed
using dot notation.
Pupil1.Surname ← "Johnson"
Pupil1.Firstname ← "Leroy"
Pupil1.DateOfBirth ← 02/01/2005
Pupil1.YearGroup ← 6
Pupil1.FormGroup ← ꞌAꞌ
Pupil2 ← Pupil1
FOR Index ← 1 TO 30
Form[Index].YearGroup ← Form[Index].YearGroup + 1
NEXT INDEX
ThisSeason ← Spring
MyAddPointer ← ^ThisSeason
NextSeason ← MyAddPointer^ + 1
// pointer is dereferenced to access the value stored
at the address
Common operations
INPUT <identifier>
The identifier should be a variable (that may be an individual element of a data structure such as an array, or
a custom data type).
OUTPUT <value(s)>
Several values, separated by commas, can be output using the same command.
Arithmetic operations
Standard arithmetic operator symbols are used:
+ Addition
- Subtraction
* Multiplication
/ Division
Care should be taken with the division operation: the resulting value should be of data type REAL, even if the
operands are integers.
Multiplication and division have higher precedence over addition and subtraction (this is the normal
mathematical convention). However, it is good practice to make the order of operations in complex
expressions explicit by using parentheses.
Relational operations
The following symbols are used for relational operators (also known as comparison operators):
In complex expressions it is advisable to use parentheses to make the order of operations explicit.
Note: An error occurs if a function call is not properly formed, or if the parameters are incorrect.
STRING Functions
NUMERIC Functions
DATE Functions
OTHER Functions
EOF(FileName : STRING) RETURNS BOOLEAN
returns TRUE if there are no more lines to be read from file FileName
Note: This function will generate an ERROR if the file is not already open in READ mode
OPERATORS
The only logic operators (also called relational operators) usedare AND, OR and NOT. The operands and
results of theseoperations arealways of data type BOOLEAN.
Note: An error is generated if an operator is used with a value or values of an incorrect type.
Selection
IF statements
IF statements may or may not have an ELSE clause.
IF
<condition>
THEN
<statement(s)>
ENDIF
IF
<condition>
THEN
<statement(s)>
ELSE
<statement(s)>
ENDIF
Note, due to space constraints, the THEN and ELSE clauses may only be indented by two spaces rather than
three. (They are, in a sense, a continuation of the IF statement rather than separate statements).
When IF statements are nested, the nesting should continue the indentation of two spaces. In particular,
run-on THEN IF and ELSE IF lines should be avoided.
CASE OF <identifier>
<value 1> : <statement1>
<statement2>
...
<value 2> : <statement1>
<statement2>
...
...
ENDCASE
CASE OF <identifier>
<value 1> : <statement1>
<statement2>
...
<value 2> : <statement1>
<statement2>
...
OTHERWISE : <statement1>
<statement2>
...
ENDCASE
Note that the case clauses are tested in sequence. When a case that applies is found, its statement is
executed and the CASE statement is complete. Control is passed to the statement after the ENDCASE. Any
remaining cases are not tested.
If present, an OTHERWISE clause must be the last case. Its statement will be executed if none of the
preceding cases apply.
The identifier must be a variable of data type INTEGER, and the values should be expressions that evaluate
to integers.
The variable is assigned each of the integer values from value1 to value2 inclusive, running the statements
inside the FOR loop after each assignment. If value1 = value2 the statements will be executed once, and if
value1 > value2 the statements will not be executed.
It is good practice to repeat the identifier after NEXT, particularly with nested FOR loops. An increment can be
specified as follows:
The increment must be an expression that evaluates to an integer. In this case the identifier will be
assigned the values from value1 in successive increments of increment until it reaches value2. If it goes
past value2, the loop terminates. The increment can be negative.
REPEAT
<Statement(s)>
UNTIL <condition>
WHILE <condition>
<statement(s)>
ENDWHILE
The condition is tested before the statements, and the statements will only be executed if the condition
evaluates to TRUE. After the statements have been executed the condition is tested again. The loop
terminates when the condition evaluates to FALSE.
The statements will not be executed if, on the first test, the condition evaluates to FALSE.
PROCEDURE <identifier>
<statement(s)>
ENDPROCEDURE
The <identifier> is the identifier used to call the procedure. Where used, param1, param2 etc. are
identifiers for the parameters of the procedure. These will be used as variables in the statements of the
procedure.
CALL <identifier>
When parameters are used, Value1, Value2,... must be of the correct data type and in the same
sequence as in the definition of the procedure.
Unless otherwise stated, it should be assumed that parameters are passed by value. (See section 8.3).
IF Size = Default
THEN
CALL DefaultSquare
ELSE
CALL Square(Size)
ENDIF
Defining and calling functions
Functions operate in a similar way to procedures, except that in addition they return a single value to the point
at which they are called. Their definition includes the data type of the value returned.
The keyword RETURN is used as one of the statements within the body of the function to specify the value to
be returned. Normally, this will be the last statement in the function definition.
Because a function returns a value that is used when the function is called, function calls are not complete
program statements. The keyword CALL should not be used when calling a function. Functions should only
be called as part of an expression. When the RETURN statement is executed, the value returned replaces the
function call in the expression and the expression is then evaluated.
If the method for passing parameters is not specified, passing by value is assumed. How this should
be called and how it operates has already been explained in Section 8.1.
A file must be opened in a specified mode before any file operations are attempted. This is written
as follows:
The file identifier may be a literal string containing the file names, or a variable of type STRING that has been
assigned the file name.
Data is read from the file (after the file has been opened in READ mode) using the READFILE command as
follows:
The Variable should be of data type STRING. When the command is executed, the next line of
text in the file is read and assigned to the variable.
The function EOF is used to test whether there are any more lines to be read from a given file. It is called as
follows:
EOF(<File Identifier>)
This function returns TRUE if there are no more lines to read (or if an empty file has been opened in READ
mode) and FALSE otherwise.
Data is written into the file (after the file has been opened in WRITE or APPEND mode) using the
WRITEFILE command as follows:
Files should be closed when they are no longer needed using the CLOSEFILE command as
follows:
Random files are opened using the RANDOM file mode as follows:
As with text files, the file identifier will normally be the name of the file.
The address should be an expression that evaluates to an integer which indicates the location of a record to
be read or written. This is usually the number of records from the beginning of the file. It is good practice to
explain how the addresses are computed.
The command GETRECORD should be used to read the record at the file pointer:
When this command is executed, the variable is assigned to the record that is read, and must be of the
appropriate data type for that record (usually a user-defined type).
The command PUTRECORD is used to write a record into the file at the file pointer:
When this command is executed, the data in the variable is inserted into the record at the file pointer. Any
data that was previously at this location will be replaced.
Example – handling random files
The records from positions 10 to 20 of a file StudentFile.Dat are moved to the next position and
a new record is inserted into position 10. The example uses the user-defined type Student defined
in Section 4.1.
NewPupil.Surname ← "Johnson"
NewPupil.Firstname ← "Leroy"
NewPupil.DateOfBirth ← 02/01/2005
NewPupil.YearGroup ← 6
NewPupil.FormGroup ← ꞌAꞌ
SEEK "StudentFile.Dat", 10
PUTRECORD "StudentFile.Dat", NewPupil
CLOSEFILE "StudentFile.dat"