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.
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).
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.
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).
Operators listed with assignment: addition +, subtraction -,
multiplication *, division /, string concatenation &,
assignment ←.
= equal to, <> not equal to, > greater than, < less than, >= greater than or equal to, <= less than or equal to.
Comparisons combined with AND, OR, and NOT (e.g. validation
with brackets).
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
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
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.
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
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
OUTPUT "Please enter the radius of the sphere " INPUT radius WHILE radius <= 0 DO OUTPUT "Please enter a positive number " INPUT radius ENDWHILE
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
DECLAREBasic types you need in pseudocode:
| Data type | Description (short) | Pseudocode |
|---|---|---|
| Boolean | True / False | BOOLEAN |
| char | Single character | CHAR |
| date | Date | DATE |
| integer | Whole number | INTEGER |
| real | Number with a decimal point | REAL |
| string | Sequence of characters | STRING |
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
TYPE / ENDTYPEComposite 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
DECLARE myList : ARRAY[0:8] OF INTEGER myList[7] ← 16
DECLARE myArray : ARRAY[0:8,0:2] OF INTEGER myArray[7,0] ← 16
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
= 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)
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>
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:
DECLARE stack ARRAY[1:10] OF INTEGER DECLARE topPointer : INTEGER DECLARE basePointer : INTEGER DECLARE stackful : INTEGER basePointer ← 1 topPointer ← 0 stackful ← 10
IF topPointer < stackful THEN topPointer ← topPointer + 1 stack[topPointer] ← item ELSE OUTPUT "Stack is full, cannot push" ENDIF
IF topPointer = basePointer - 1 THEN OUTPUT "Stack is empty, cannot pop" ELSE Item ← stack[topPointer] topPointer ← topPointer - 1 ENDIF
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).
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
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
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
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:
| Identifier | Description |
|---|---|
myLinkedList | Linked list to be searched |
myLinkedListPointers | Pointers for linked list |
startPointer | Start of the linked list |
heapStartPointer | Start of the heap |
index | Pointer to current element in the linked list |
BYVALUE with procedures (examination-style)PROCEDURE AddName(BYVALUE NewName : STRING) // ... body as required by the question ENDPROCEDURE
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.
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
BYREF, CALLPROCEDURE <identifier> <statements> ENDPROCEDURE CALL <identifier>
PROCEDURE <identifier>(<parameter1>:<datatype>, <parameter2>:<datatype>...) <statements> ENDPROCEDURE CALL <identifier> (Value1, Value2...)
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)
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
RETURNS, RETURNFUNCTION <identifier> RETURNS <data type> <statements> ENDFUNCTION
FUNCTION <identifier>(<parameter1>:<datatype>, <parameter2>:<datatype>...) RETURNS <data type> <statements> ENDFUNCTION
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