pseudocode Flashcards
Data Types
pseudocode
The following keywords are used to designate some basic data types:
* INTEGER a whole number
* REAL a number capable of containing a fractional part
* CHAR a single character
* STRING a sequence of zero or more characters
* BOOLEAN the logical values TRUE and FALSE
* DATE a valid calendar date
Literals
Literals of the above data types are written as follows:
* Integer
Written as normal in the denary system, e.g. 5, -3
* Real
Always written with at least one digit on either side of the decimal point, zeros being
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ꞌ, ꞌ@ꞌ
* String
Delimited by double quotes. A string may contain no characters (i.e. the empty string)
e.g. “This is a string”, “”
* Boolean
TRUE, FALSE
* Date
This will normally be written in the format dd/mm/yyyy. However, it is good practice to
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 mixed 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.
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.
Keywords identified elsewhere in this guide should never be used as variables.
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.
Declarations are made as follows:DECLARE <identifier> : <data type>
Example – variable declarations
~~~
DECLARE Counter : INTEGER
DECLARE TotalToPay : REAL
DECLARE GameOver : BOOLEAN
~~~
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:CONSTANT <identifier> = <value>
Only literals can be used as the value of a constant. A variable, another constant or an expression must never be used.
Example
~~~
CONSTANT declarations
CONSTANT HourlyRate = 6.50
CONSTANT DefaultText = “N/A”
~~~
Assignments
The assignment operator is ← .
Assignments should be made in the following format:<identifier> ← <value>
The identifier must refer to a variable (this can be an individual element in a data structure such as an array or a user defined data type). The value may be any expression that evaluates to a value of the same data type as the variable
Counter ← 0 Counter ← Counter + 1 TotalToPay ← NumberOfHours * HourlyRate
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.
Square brackets are used to indicate the array indices.
A One-dimensional array is declared as follows:DECLARE <identifier>:ARRAY[<lower>:<upper>] OF <data type>
A two-dimensional array is declared as follows:DECLARE <identifier>:ARRAY[<lower1>:<upper1>,<lower2>:<upper2>] OF <data type>
DECLARE StudentNames : ARRAY[1:30] OF STRING
DECLARE NoughtsAndCrosses : ARRAY[1:3,1:3] OF CHAR
Using arrays
Array index values may be literal values or expressions that evaluate to a valid integer value.
Example – Accessing individual array elements
~~~
StudentNames[1] ← “Ali”
NoughtsAndCrosses[2,3] ← ꞌXꞌ
StudentNames[n+1] ← StudentNames[n]
~~~
Arrays can be used in assignment statements (provided they have same size and data type). The following is
therefore allowed:
~~~
Example – Accessing a complete array
SavedGame ← NoughtsAndCrosses
~~~
A statement should not refer to a group of array elements individually. For example, the following construction should not be used.StudentNames [1 TO 30] ← ""
Instead, an appropriate loop structure is used to assign the elements individually. For example:
Example – assigning a group of array elements
~~~
FOR Index ← 1 TO 30
StudentNames[Index] ← “”
NEXT Index
~~~
Non-composite data type – Enumerated
Defining user-defined data types
A user-defined non-composite data type with a list of possible values is called an enumerated data type.
The enumerated type should be declared as follows:TYPE <identifier> = (value1, value2, value3, ...)
Example – declaration of enumerated type
This enumerated type holds data about seasons of the year.TYPE Season = (Spring, Summer, Autumn, Winter)
Non-composite data type – Pointer
A user-defined non-composite data type referencing a memory location is called a pointer.
The pointer should be declared as follows:TYPE <identifier> = ^<data type>
The ^ shows that the variable is a pointer and the data type indicates the type of the data stored in the memory location.
Example – declarations of pointer type
~~~
TYPE TIntPointer = ^INTEGER
TYPE TCharPointer = ^CHAR
~~~
Declaration of a variable of pointer type does not require the ^ (caret) symbol to be used.
Example – declaration of a pointer variableDECLARE MyPointer : TIntPointer
Composite data type
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>
TECLARE <identifier3> : <data>
...
ENDTYPE
~~~</data></identifier3></data></identifier2></identifier1>
Example – declaration of composite type
This user-defined data type holds data about a student.
~~~
TYPE Student
DECLARE LastName : 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.
Example – using user-defined data types
This pseudocode uses the user-defined types Student, Season and TIntPointer defined in
the previous section.
~~~
DECLARE Pupil1 : Student
DECLARE Pupil2 : Student
DECLARE Form : ARRAY[1:30] OF Student
DECLARE ThisSeason : Season
DECLARE NextSeason : Season
DECLARE MyPointer : TIntPointer
Pupil1.LastName ← “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
MyPointer ← ^ThisSeason
NextSeason ← MyPointer^ + 1
// access the value stored at the memory address
~~~