Pseudocode — language reference

Syntax, structure, and examples for CIE 9618–style pseudocode. These notes describe the common notation for teaching and written exams, not a specific programming environment.

For assessments, use the form of pseudocode and identifier tables required by the mark scheme. The examples here are not tied to any particular interpreter or online IDE.

1. Layout and identifier rules

Each line of pseudocode is usually a single step. Use a fixed-pitch style with indentation of four spaces where required; the keywords THEN, ELSE and CASE clauses are only indented by two spaces from the keyword line above.

Identifier names should be meaningful. They use only A–Z, a–z and 0–9 and start with a letter. In pseudocode, identifiers are usually case-insensitive (unlike in many real programming languages). Use an identifier table in solutions when asked.

End-of-line comments starting with // are used in longer worked examples (e.g. linked list initialisation).

2. Input, output, assignment, operators

2.1 Input and output

To input a value (identifier only on the INPUT line in typical patterns):

INPUT StudentName

To output a message, a value, or a combination of several items:

OUTPUT "You have made an error"
OUTPUT StudentName
OUTPUT "Student name is ", StudentName

PRINT can be used in the same way as another output form; for example, an “average of integers” solution may use PRINT in parallel with INPUT in the same algorithm.

2.2 Assignment

Assignment uses the left arrow. Examples:

Counter ← 1
Counter ← Counter + 1
MyChar ← "A"
LetterValue ← ASC(MyChar)
StudentMark ← 40
Percentage ← (StudentMark / 80) * 100
Oldstring ← "Your mark is"
NewString ← Oldstring & " ninety-seven"

ASC returns the (ASCII) value of a character. String join uses & (and + appears in exam-style expressions elsewhere).

2.3 Arithmetic in expressions

Operators listed with assignment: addition +, subtraction -, multiplication *, division /, string concatenation &, assignment .

3. Relational, logic, IF, CASE

3.1 Relational operators (selection and conditions)

= equal to, <> not equal to, > greater than, < less than, >= greater than or equal to, <= less than or equal to.

3.2 Boolean logic

Comparisons combined with AND, OR, and NOT (e.g. validation with brackets).

3.3 IF — single choice, with alternative, nested

IF MyValue > YourValue
THEN
  OUTPUT "I win"
ENDIF
IF MyValue > YourValue
THEN
  OUTPUT "I win"
ELSE
  OUTPUT "You win"
ENDIF

Nested exam-grade example:

OUTPUT "Enter your exam mark"
INPUT Mark
IF Mark < 40
THEN
  Grade ← "Fail"
ELSE
  IF Mark < 60
  THEN
    Grade ← "Pass"
  ELSE
    IF Mark < 80
    THEN
      Grade ← "Merit"
    ELSE
      Grade ← "Distinction"
    ENDIF
  ENDIF
ENDIF
OUTPUT "Your grade is ", Grade

3.4 CASE — multiple choices, with OTHERWISE

Map directions example:

CASE OF Direction
  "N": Y ← Y + 1
  "S": Y ← Y – 1
  "E": X ← X + 1
  "W": X ← X – 1
ENDCASE

CASE OF Direction
  "N": Y ← Y + 1
  "S": Y ← Y – 1
  "E": X ← X + 1
  "W": X ← X – 1
OTHERWISE : OUTPUT "Error"
ENDCASE

Integer menu example — value, range, condition, and OTHERWISE:

DECLARE choice : INTEGER
OUTPUT "Please enter your choice "
INPUT choice
CASE OF choice
  1 : OUTPUT "Routine 1 "
  2 : OUTPUT "Routine 2 "
  3 : OUTPUT "Routine 3 "
  4 … 6 : OUTPUT "Routine not written"
  10 : Exit
  OTHERWISE OUTPUT "Incorrect choice"
ENDCASE

4. FOR, REPEAT, WHILE

4.1 FOR — fixed repeats; optional STEP

In a FOR loop, STEP is optional and must be a whole number. Examples:

Total ← 0
FOR Counter ← 1 TO 10
  OUTPUT "Enter a number "
  INPUT Number
  Total ← Total + Number
NEXT Counter
OUTPUT "The total is ", Total

FOR Counter ← 1 TO 10 STEP 2
  OUTPUT Counter
NEXT Counter

Some algorithms use = in the FOR line (e.g. bubble sort) instead of . Use the form your course or mark scheme expects; both forms appear in past material.

4.2 REPEAT — UNTIL (post-condition)

REPEAT
  OUTPUT "Please enter a positive number "
  INPUT Number
UNTIL Number > 0

Number ← 0
WHILE Number >= 0 DO
  OUTPUT "Please enter a negative number "
  INPUT Number
ENDWHILE

4.3 REPEAT/UNTIL and INT for validation

REPEAT
  OUTPUT "Please enter a positive number less than fifty"
  INPUT Number
UNTIL (Number > 0) AND (Number < 50)
Total ← 0
REPEAT
  PRINT "Enter the number of values to average"
  INPUT Number
UNTIL (Number > 0) AND (Number = INT(Number))
FOR Counter ← 1 TO Number
  REPEAT
    PRINT "Enter an integer value "
    INPUT Value
  UNTIL Value = INT(Value)
  Total ← Total + Value
NEXT Counter
Average ← Total / Number
PRINT "The average of ", Number, " values is ", Average

4.4 WHILE — DO — ENDWHILE

OUTPUT "Please enter the radius of the sphere "
INPUT radius
WHILE radius <= 0 DO
  OUTPUT "Please enter a positive number "
  INPUT radius
ENDWHILE

4.5 Validation loop

REPEAT until value is in range (0–10 inclusive) — two equivalent patterns:

REPEAT
  OUTPUT "Enter value "
  INPUT value
UNTIL value < 0 OR value > 10

OUTPUT "Enter value "
INPUT value
WHILE value < 0 OR value > 10 DO
  OUTPUT "Enter value "
  INPUT value
ENDWHILE

5. Data types and DECLARE

Basic types you need in pseudocode:

Data typeDescription (short)Pseudocode
BooleanTrue / FalseBOOLEAN
charSingle characterCHAR
dateDateDATE
integerWhole numberINTEGER
realNumber with a decimal pointREAL
stringSequence of charactersSTRING

Declaration form in pseudocode:

DECLARE <identifier> : <data type>
DECLARE h, w, r, Perimeter, Area : REAL
DECLARE A, B, C, D, E  : BOOLEAN
DECLARE myBirthday : DATE

Constants:

CONSTANT pi ← 3.142

6. Records: TYPE / ENDTYPE

Composite type definition; each field is declared inside with DECLARE. A generic type definition can use :: on its own line between field groups (as in some course materials). Example (TbookRecord) — field access uses . (dot) after declaring a Book of that type.

TYPE
TbookRecord
  DECLARE title : STRING
  DECLARE author : STRING
  DECLARE publisher : STRING
  DECLARE noPages : INTEGER
  DECLARE fiction : BOOLEAN
ENDTYPE

DECLARE Book : TbookRecord
Book.author ← "David Watson"
Book.fiction ← FALSE

7. Arrays 1D and 2D

7.1 One-dimensional (list)

DECLARE myList : ARRAY[0:8] OF INTEGER
myList[7] ← 16

7.2 Two-dimensional (table)

DECLARE myArray : ARRAY[0:8,0:2] OF INTEGER
myArray[7,0] ← 16

8. Linear search and bubble sort

8.1 Linear search

DECLARE myList : ARRAY[0:8] OF INTEGER
DECLARE upperBound : INTEGER
DECLARE lowerBound : INTEGER
DECLARE index : INTEGER
DECLARE item : INTEGER
DECLARE found : BOOLEAN
upperBound ← 8
lowerBound ← 0
OUTPUT "Please enter item to be found"
INPUT item
found ← FALSE
index ← lowerBound
REPEAT
  IF item = myList[index]
  THEN
    found ← TRUE
  ENDIF
  index ← index + 1
UNTIL (found = TRUE) OR (index > upperBound)
IF found
THEN
  OUTPUT "Item found"
ELSE
  OUTPUT "Item not found"
ENDIF

8.2 Bubble sort (note = in FOR and assignment with array elements)

DECLARE myList : ARRAY[0:8] OF INTEGER
DECLARE upperBound : INTEGER
DECLARE lowerBound : INTEGER
DECLARE index : INTEGER
DECLARE swap : BOOLEAN
DECLARE temp : INTEGER
DECLARE top : INTEGER
upperBound ← 8
lowerBound ← 0
top ← upperBound
REPEAT
  FOR index = lowerBound TO top - 1
    Swap ← FALSE
    IF myList[index] > myList[index + 1]
    THEN
      temp ← myList[index]
      myList[index] ← myList[index + 1]
      myList[index + 1] ← temp
      swap ← TRUE
    ENDIF
  NEXT
  top ← top - 1
UNTIL (NOT swap) OR (top = 0)

9. Text files

Open modes: READ, WRITE (overwrites), APPEND (end of file). Variable for a line is STRING. EOF(<file identifier>) is boolean.

Typical form (commas and identifiers must match your question; ask your teacher if uncertain):

OPEN <file identifier> FOR READ
READFILE <file identifier>, <variable>
OPEN <file identifier> FOR WRITE
WRITEFILE <file identifier>, <variable>
WRITEFILE <file identifier>, <variable>   // APPEND context in activities
EOF(<file identifier>)
CLOSEFILE <file identifier>

10. Stacks, queues, linked lists (advanced)

Overview and pointer diagrams; stack and queue algorithms in pseudocode appear in full advanced courses. Full linked-list implementation is often for reference; at AS you may only need to explain adding and removing items. Examples include:

10.1 Stack

To set up a stack

DECLARE stack ARRAY[1:10] OF INTEGER
DECLARE topPointer : INTEGER
DECLARE basePointer : INTEGER
DECLARE stackful : INTEGER
basePointer ← 1
topPointer ← 0
stackful ← 10

To push an item, stored in item, onto a stack

IF topPointer < stackful
THEN
  topPointer ← topPointer + 1
  stack[topPointer] ← item
ELSE
  OUTPUT "Stack is full, cannot push"
ENDIF

To pop an item, stored in item, from the stack

IF topPointer = basePointer - 1
THEN
  OUTPUT "Stack is empty, cannot pop"
ELSE
  Item ← stack[topPointer]
  topPointer ← topPointer - 1
ENDIF

10.2 Queue (circular)

In this set-up, endPointer ← 0 appears in the initialisation, while enqueue and dequeue use rearPointer (check your course notes for the exact naming your examiner uses).

10.2.1 To set up a queue

DECLARE queue ARRAY[1:10] OF INTEGER
DECLARE rearPointer : INTEGER
DECLARE frontPointer : INTEGER
DECLARE queueful : INTEGER
DECLARE queueLength : INTEGER
frontPointer ← 1
endPointer ← 0
upperBound ← 10
queueful ← 10
queueLength ← 0

10.2.2 To add an item, stored in item, onto a queue

IF queueLength < queueful
THEN
  IF rearPointer < upperBound
  THEN
    rearPointer ← rearPointer + 1
  ELSE
    rearPointer ← 1
  ENDIF
  queueLength ← queueLength + 1
  queue[rearPointer] ← item
ELSE
  OUTPUT "Queue is full, cannot enqueue"
ENDIF

10.2.3 To remove an item from the queue and store in item

IF queueLength = 0
THEN
  OUTPUT "Queue is empty, cannot dequeue"
ELSE
  Item ← queue[frontPointer]
  IF frontPointer = upperBound
  THEN
    frontPointer ← 1
  ELSE
    frontPointer ← frontPointer + 1
  ENDIF
  queueLength ← queueLength - 1
ENDIF

10.3 Linked list — set-up

This code sets up a linked list using two parallel arrays (data and next-pointers) and pointer variables. At AS you may not need to write a full implementation, but you should understand how items are added and removed.

To set up a linked list (note: the first DECLARE uses mylinkedList; the identifier table below uses myLinkedList — match the spelling your question uses):

DECLARE mylinkedList ARRAY[0:11] OF INTEGER
DECLARE myLinkedListPointers ARRAY[0:11] OF INTEGER
DECLARE startPointer : INTEGER
DECLARE heapStartPointer : INTEGER
DECLARE index : INTEGER
heapStartPointer ← 0
startPointer ← -1 // list empty
FOR index ← 0 TO 11
  myLinkedListPointers[index] ← index + 1
NEXT index
// the linked list heap is a linked list of all the
// spaces in the linked list, this is set up when the
// linked list is initialised
myLinkedListPointers[11] ← -1
// the final heap pointer is set to -1 to show no
// further links

Identifier table:

IdentifierDescription
myLinkedListLinked list to be searched
myLinkedListPointersPointers for linked list
startPointerStart of the linked list
heapStartPointerStart of the heap
indexPointer to current element in the linked list

10.4 BYVALUE with procedures (examination-style)

PROCEDURE AddName(BYVALUE NewName : STRING)
  // ... body as required by the question
ENDPROCEDURE

11. String functions, DIV, MOD, INT, library

11.1 DIV and MOD

Example: DIV(10,3) returns 3, MOD(10,3) returns 1. Use the form your examination paper uses. INT(x) gives the integer part of a real x.

11.2 String functions

LENGTH(anyString : STRING) RETURNS INTEGER
RIGHT(anyString: STRING, x : INTEGER) RETURNS STRING
LEFT(anyString: STRING, x : INTEGER) RETURNS STRING
MID(anyString: STRING, x : INTEGER, y : INTEGER) RETURNS STRING

Function header example: SQUAREROOT(anyPosVal : REAL) RETURNS REAL

12. Procedures, BYREF, CALL

12.1 No parameters

PROCEDURE <identifier>
  <statements>
ENDPROCEDURE

CALL <identifier>

12.2 With parameters (by value) — form and call

PROCEDURE <identifier>(<parameter1>:<datatype>, <parameter2>:<datatype>...)
  <statements>
ENDPROCEDURE

CALL <identifier> (Value1, Value2...)

12.3 Example: line of stars

Some handouts omit the arrow in the FOR line; the usual pattern is:

PROCEDURE stars (Number : INTEGER)
  FOR Counter ← 1 TO Number
    OUTPUT "*"
  NEXT Counter
ENDPROCEDURE

CALL stars (7)
// or
myNumber ← 7
CALL stars (myNumber)

12.4 By reference

PROCEDURE celsius(BYREF temperature : REAL)
  temperature ← (temperature – 32) / 1.8
ENDPROCEDURE

CALL celsius(myTemp)

General form (parameters may mix BYREF and by value):

PROCEDURE <identifier> (BYREF <parameter1>:<datatype>, <parameter2>:<datatype>...)
  <statements>
ENDPROCEDURE

13. Functions, RETURNS, RETURN

13.1 No parameters

FUNCTION <identifier> RETURNS <data type>
  <statements>
ENDFUNCTION

13.2 With parameters

FUNCTION <identifier>(<parameter1>:<datatype>, <parameter2>:<datatype>...)
  RETURNS <data type>
  <statements>
ENDFUNCTION

13.3 Temperature as function; substring example with RETURN

FUNCTION celsius (temperature : REAL) RETURNS REAL
  RETURN (temperature – 32) / 1.8
ENDFUNCTION

myTemp ← celsius(myTemp)

FUNCTION substring (myString : STRING, start : INTEGER, length : INTEGER)
  RETURNS STRING
  IF LENGTH (myString) >= length + start
  THEN
    RETURN MID (myString, start, length)
  ELSE
    RETURN ""
  ENDIF
ENDFUNCTION

14. Worked program fragment (examination style)

REPEAT/IF/ENDIF structure: temperature conversion menu (with line numbers as in a past paper style):

01  REPEAT
02    OUTPUT "Menu Temperature Conversion"
03    OUTPUT "Celsius to Fahrenheit  1"
04    OUTPUT "Fahrenheit to Celsius  2"
05    OUTPUT "Exit  3"
06    OUTPUT "Enter choice"
07    IF Choice = 1 OR Choice = 2
08    THEN
09      OUTPUT "Enter temperature"
10      INPUT Temperature
11      IF Choice = 1
12      THEN
13        ConvertedTemperature ← 1.8*Temperature + 32
14      ELSE
15        ConvertedTemperature ← (Temperature – 32) * 5 / 9
16      ENDIF
17      OUTPUT "Converted temperature is ", ConvertedTemperature
18    ELSE
19      IF Choice <> 3
20      THEN
21        OUTPUT "Error in choice"
22      ENDIF
23    ENDIF
24  UNTIL Choice = 3