This chapter is the pseudocode toolkit for Paper 2: the statements you must be able to write, read and trace. The exam paper comes with an insert listing the pseudocode functions and operators, so you do not need to memorise their exact spelling, but you do need to know how to use them correctly.
11.1 Programming basics
Implementing a design
Programs are written from a design: a program flowchart, structured English or a structure chart (chapters 9 and 12). Converting the design to pseudocode means: declare the data (from the identifier table), then write the steps in order, using the constructs for decisions and repetition.
Variables, constants and assignment
A variable is a named storage location whose value can change while the program runs. A constant is a named value that is fixed for the whole program. Using a constant instead of a "magic number" makes the program easier to read and maintain (change it in one place).
DECLARE Score : INTEGER // a variable DECLARE Name : STRING CONSTANT PassMark = 50 // a constant Score ← 0 // initialisation: give a starting value Name ← "Amina" Score ← Score + 10 // assignment: the arrow stores the result
Initialise variables before they are used (especially totals and counters) so they do not hold an unknown value. Assignment stores the value of the right-hand side in the variable on the left.
Input and output
INPUT Name // read a value typed by the user OUTPUT "Hello ", Name // display items; commas join the parts OUTPUT "Total: ", Total
Good practice: show a prompt before an input (OUTPUT "Enter your age"), and give output a clear message.
Operators
| Type | Operators |
|---|---|
| Arithmetic | + - * / and ^ (power); DIV (integer division, quotient) and MOD (remainder) |
| Comparison | = <> < > <= >= |
| Logic | AND OR NOT |
| String | & joins (concatenates) strings |
17 DIV 5 is 3 and 17 MOD 5 is 2. A number is even if N MOD 2 = 0. MOD and DIV are used to split a number into digits, test divisibility and wrap around (clock arithmetic).
Operators are evaluated in the order brackets, power, multiplication/division/MOD/DIV, addition/subtraction; use brackets to be clear.
Built-in functions and library routines
A built-in function is a ready-made routine that returns a value; using one saves effort and is reliable. The insert that comes with Paper 2 lists them, so you do not need to remember the exact spelling (the teachers' guide also shows UCASE and LCASE, which take a single CHAR):
| Function | Returns | Example |
|---|---|---|
LENGTH(s) | Number of characters in the string | LENGTH("Hello") → 5 |
LEFT(s, n) / RIGHT(s, n) | The first / last n characters | LEFT("Computer", 3) → "Com" |
MID(s, start, n) | n characters starting at position start (first position is 1) | MID("Computer", 4, 3) → "put" |
TO_UPPER(x) / TO_LOWER(x) | A CHAR or STRING converted to upper / lower case | TO_UPPER("Error 803") → "ERROR 803" |
ASC(c) / CHR(n) | The ASCII value of a character / the character for a code | ASC('A') → 65 |
INT(x) | The whole-number part of a real number | INT(7.9) → 7 |
RAND(n) | A random real number from 0 up to (not including) n | INT(RAND(6)) + 1 → 1 to 6 |
NUM_TO_STR(x) / STR_TO_NUM(s) | Converts between number and string | STR_TO_NUM("42") → 42 |
IS_NUM(s) | TRUE if the string can be a number | IS_NUM("4a") → FALSE |
DAY(d), MONTH(d), YEAR(d), DAYINDEX(d) | The day, month and year of a DATE, and the day of the week (Sunday = 1) | YEAR(04/10/2003) → 2003 |
SETDATE(d, m, y) / TODAY() | A DATE built from three integers / today's date | SETDATE(26, 10, 2003) |
Initial ← TO_UPPER(LEFT(Surname, 1)) DiceThrow ← INT(RAND(6)) + 1
A library routine is a routine from a program library (chapter 5) that is available to many programs, for example for maths or sorting. Using built-in functions and library routines saves time, and the code is already tested.
11.2 Constructs
Selection
IF statement with an ELSE clause, and nested IF statements (an IF inside another):
IF Mark >= 70 THEN
Grade ← "A"
ELSE
IF Mark >= 50 THEN
Grade ← "B"
ELSE
Grade ← "C"
ENDIF
ENDIF
Each IF must have its own ENDIF. Indent the blocks. Order matters: test the most specific or highest condition first.
CASE statement: choose between many values of one variable. It is neater than a long chain of IFs:
CASE OF Choice
'A' : OUTPUT "Add"
'D' : OUTPUT "Delete"
'Q' : OUTPUT "Quit"
OTHERWISE : OUTPUT "Invalid choice"
ENDCASE
OTHERWISE handles every value not listed. A range can be written with TO, for example 1 TO 9 : OUTPUT "Single digit".
Iteration: the three loops
Count-controlled loop (FOR): repeats a block a known number of times. The loop variable counts up from the start to the end value, inclusive, and an optional STEP changes the increment.
FOR Count ← 1 TO 10 STEP 2 // 1, 3, 5, 7, 9
OUTPUT Count
NEXT Count
Pre-condition loop (WHILE ... ENDWHILE): the condition is tested before each pass, so the loop body may run zero times. It repeats while the condition is TRUE.
INPUT Number
WHILE Number <> 0
Total ← Total + Number
INPUT Number
ENDWHILE
Post-condition loop (REPEAT ... UNTIL): the condition is tested after each pass, so the body runs at least once. It repeats until the condition becomes TRUE.
REPEAT
OUTPUT "Enter a number from 1 to 10"
INPUT Number
UNTIL Number >= 1 AND Number <= 10
To justify a choice in an exam, say what is known: "A count-controlled loop is better because the number of values (50) is known in advance, so the code is shorter and there is no need to manage a counter by hand," or "A post-condition loop is better because the user must be prompted at least once before the input can be checked".
11.3 Structured programming
Procedures and functions
A subroutine is a named, self-contained block of code that carries out one task and can be called whenever it is needed. Splitting a program into subroutines (modular programming) makes it easier to write, test, understand, maintain and reuse.
- A procedure performs a task and does not return a value. It is called with
CALL. - A function performs a task and returns a single value, so it is used in an expression (on the right of an assignment, or in output).
PROCEDURE ShowMenu()
OUTPUT "1. Add"
OUTPUT "2. Delete"
ENDPROCEDURE
FUNCTION Average(Total : INTEGER, Count : INTEGER) RETURNS REAL
RETURN Total / Count
ENDFUNCTION
CALL ShowMenu()
Mean ← Average(Sum, 10)
OUTPUT "Mean: ", Average(Sum, 10)
Use the terminology: the header (name and parameters), the parameters (listed in the definition) and arguments (the values given when it is called), the return value and the call.
When to use a procedure and when a function: use a function when the subroutine's job is to work out and return a result (a calculation, a validity check that returns TRUE or FALSE). Use a procedure when it does something (displays a menu, prints a report, sets up an array) and has no single result.
Parameters
A parameter passes data into a subroutine, so it works on different values each time it is called without using global variables. Two ways of passing:
- By value (
BYVAL): the subroutine gets a copy. Changes inside do not affect the original variable. - By reference (
BYREF): the subroutine gets the address of the original. Changes made inside do change the original. Used when a procedure needs to send back more than one result.
PROCEDURE Swap(BYREF A : INTEGER, BYREF B : INTEGER)
DECLARE Temp : INTEGER
Temp ← A
A ← B
B ← Temp
ENDPROCEDURE
If neither is stated, assume by value.
Local and global variables
A local variable is declared inside a subroutine; it exists only while the subroutine runs and cannot be seen from outside, so subroutines cannot interfere with each other and the same name can be reused. A global variable is declared outside all subroutines, visible to the whole program, and keeps its value. Prefer local variables and parameters: they make a subroutine self-contained and easier to test, reuse and maintain; excessive use of globals causes unexpected changes that are hard to track.
Writing efficient pseudocode
"Efficient" pseudocode does the job with no unnecessary steps. It is a mark point in many Paper 2 questions. Ways to be efficient:
- Use a loop instead of repeating the same statements.
- Use
FORwhen the number of repeats is known; stop a search early once the item is found (use aWHILEor aFoundflag, rather than going through the whole array). - Use ELSE / nested IF / CASE rather than a series of separate
IFstatements that all get tested. - Do not calculate the same thing repeatedly inside a loop if it can be done once outside it.
- Use built-in functions where they exist, and meaningful identifiers, constants rather than repeated literal values, and subroutines for repeated tasks.
- Do not use more variables or data than needed.
Practise this chapter
Every past-paper question on these topics, with the mark scheme.

