Develop the ability to write clear, maintainable and efficient pseudocode that follows the principles of structured programming and satisfies the Cambridge AS & A‑Level Computer Science (9618) syllabus.
Structured programming restricts programs to three fundamental control structures – sequence, selection and iteration. It promotes:
| Principle | What it means |
|---|---|
| Single‑entry, single‑exit | Every block (loop, sub‑routine, etc.) has one entry point and one exit point. |
| Modularity | Break the problem into small, self‑contained modules (procedures or functions). |
| Hierarchy | Arrange modules from high‑level control flow down to low‑level details. |
| Readability | Use meaningful identifiers, consistent indentation and comments. |
| Efficiency | Choose algorithms and data structures that minimise time and space complexity. |
Cambridge pseudocode uses a fixed set of keywords for declarations and I/O. The table below shows the exact syntax for each data type required by the syllabus.
| Data type (syllabus) | Pseudocode declaration | Typical range / notes |
|---|---|---|
| INTEGER | INTEGER varName | ‑2 147 483 648 … 2 147 483 647 |
| REAL | REAL varName | floating‑point numbers |
| CHAR | CHAR varName | single character |
| STRING | STRING varName | sequence of characters (up to 255 by default) |
| BOOLEAN | BOOLEAN varName | TRUE or FALSE |
| DATE | DATE varName | day‑month‑year format |
/* constants */ CONSTANT MAX_SIZE ← 100 CONSTANT PI ← 3.14159 /* variables – specify the data type */ INTEGER count, total REAL average CHAR grade STRING name BOOLEAN found DATE today /* input & output */ INPUT count // reads an INTEGER from the keyboard OUTPUT "Result = ", total // writes to the screen OUTPUTLN "Done." // adds a newline
The three basic structures plus the additional constructs required by the syllabus.
| Structure | Description | Pseudocode Syntax |
|---|---|---|
| Sequence | Execute statements one after another. | statement1 statement2 statement3 |
| Selection – IF/ELSE | Choose between two alternatives. | IF condition THEN
statements
ELSE
statements
ENDIF |
| Selection – CASE | Multiple mutually exclusive alternatives. | CASE variable OF
value1: statements
value2: statements
…
OTHERWISE: statements
ENDCASE |
| Iteration – Count‑controlled (FOR) | Known number of repetitions. | FOR i FROM 1 TO n DO
statements
ENDFOR |
| Iteration – Pre‑condition (WHILE) | Test before the first iteration. | WHILE condition DO
statements
ENDWHILE |
| Iteration – Post‑condition (REPEAT‑UNTIL) | Body executed at least once; test after each pass. | REPEAT
statements
UNTIL condition |
Deeply nested structures often lead to “spaghetti” code that is hard to read and test. The table shows a common nested pattern and a flatter, equivalent version.
| Nested version | Flat version (using early exit) |
|---|---|
FOR i FROM 1 TO n DO
IF condition1(i) THEN
FOR j FROM 1 TO m DO
IF condition2(i,j) THEN
… // many statements
ENDIF
ENDFOR
ENDIF
ENDFOR |
FOR i FROM 1 TO n DO
IF NOT condition1(i) THEN
CONTINUE // skip to next i
ENDIF
FOR j FROM 1 TO m DO
IF NOT condition2(i,j) THEN
CONTINUE // skip to next j
ENDIF
… // same statements
ENDFOR
ENDFOR |
Using CONTINUE (or an early RETURN in a function) reduces indentation depth, improves readability and makes the logical flow easier to analyse.
Efficiency is about expressing the algorithm so that its computational cost is obvious and unnecessary work is avoided.
FOR loops or pre‑condition WHILE loops with a clear bound over indefinite loops.| Aspect | Procedure | Function |
|---|---|---|
| Purpose | Performs actions; may modify parameters. | Computes and returns a value. |
| Return | No explicit return value. | Returns a single value (using RETURN). |
| Call syntax | CALL ProcName(arg1, arg2) | result ← FuncName(arg1, arg2) |
| Typical use | I/O, updating global state, printing reports. | Calculations, predicates, transformations. |
Cambridge pseudocode indicates the mode in the header:
PROCEDURE SortArray(INOUT arr[1..n]) // INOUT = by reference FUNCTION MaxValue(IN arr[1..n]) RETURNS INTEGER // IN = by value
/*--------------------------------------------------------------
Module: ModuleName
Purpose: brief description of what the module does
Input: list of parameters (IN, OUT, INOUT)
Output: result (if a function) or changes to parameters
--------------------------------------------------------------*/
PROCEDURE ModuleName(IN param1, INOUT param2)
// local variable declarations
INTEGER i, total
// algorithm
…
RETURN // only for functions
ENDPROCEDURE
Although not written in pseudocode, the syllabus expects students to interpret:
These visual tools help plan the modular decomposition before the pseudocode is written.
FUNCTION FindSecondLargest(IN list[1..n]) RETURNS INTEGER
// Pre‑condition: n ≥ 2 and the list contains at least two distinct values
INTEGER max ← list[1]
INTEGER secondMax ← -∞
FOR i FROM 2 TO n DO
IF list[i] > max THEN
secondMax ← max
max ← list[i]
ELSE IF list[i] > secondMax AND list[i] ≠ max THEN
secondMax ← list[i]
ENDIF
ENDFOR
RETURN secondMax
ENDFUNCTION
Analysis
O(n).O(1).CONTINUE or RETURN to flatten control flow.Write pseudocode to compute the factorial of a non‑negative integer using a FOR loop. State its time complexity.
FUNCTION Factorial(IN n) RETURNS INTEGER
// Pre‑condition: n ≥ 0
INTEGER result ← 1
FOR i FROM 2 TO n DO
result ← result * i
ENDFOR
RETURN result
ENDFUNCTION
Time complexity: O(n) (single linear pass).
Explain why the following pseudocode is inefficient and rewrite it to improve efficiency.
FOR i FROM 1 TO n DO
FOR j FROM 1 TO i DO
sum ← sum + array[j]
ENDFOR
ENDFOR
Inefficiency: The inner loop repeatedly sums the same prefix of the array, giving a quadratic O(n²) runtime.
Improved version (single pass):
INTEGER prefixSum ← 0
FOR i FROM 1 TO n DO
prefixSum ← prefixSum + array[i]
sum ← sum + prefixSum
ENDFOR
Now the algorithm runs in O(n) time and uses only O(1) extra space.
Given a list of n numbers, describe a structured‑programming approach to determine whether the list is sorted in ascending order. Provide the pseudocode and its complexity.
FUNCTION IsSorted(IN list[1..n]) RETURNS BOOLEAN
// Pre‑condition: n ≥ 1
FOR i FROM 2 TO n DO
IF list[i] < list[i‑1] THEN
RETURN FALSE
ENDIF
ENDFOR
RETURN TRUE
ENDFUNCTION
Time complexity: O(n). Space complexity: O(1).
Convert the nested loop from question 2 into a “flat” version using CONTINUE (or an early RETURN) and comment on how this improves readability.
FOR i FROM 1 TO n DO
IF i = 1 THEN
CONTINUE // nothing to add for the first element
ENDIF
sum ← sum + prefixSum // prefixSum already holds Σ_{k=1}^{i‑1} array[k]
prefixSum ← prefixSum + array[i]
ENDFOR
The flat version removes the inner loop, reduces indentation depth, and makes the invariant (prefixSum holds the sum of the processed prefix) explicit.
Create an account or Login to take a Quiz
Log in to suggest improvements to this note.
Your generous donation helps us continue providing free Cambridge IGCSE & A-Level resources, past papers, syllabus notes, revision questions, and high-quality online tutoring to students across Kenya.