Implement and write correct pseudocode from a given design (flowchart or structured English) using the full range of Cambridge‑required constructs: constants, variable declarations, data types, expressions, input/output and built‑in functions.
IF, THEN, ELSE, CASE, FOR, WHILE, REPEAT, UNTIL, END IF, END FOR, END WHILE, END REPEAT, READ, WRITE, CONST, etc.READ (optionally preceded by a prompt) and WRITE (or OUTPUT) for displaying results.Cambridge pseudocode recognises the following primitive data types. Declare a variable before it is used; initialise it if a value is required immediately.
| Data Type | Keyword | Typical Use | Example Declaration |
|---|---|---|---|
| Integer | INTEGER | Counts, whole‑number calculations | INTEGER i, total, n |
| Real (floating‑point) | REAL | Measurements, percentages, monetary values | REAL price, taxRate ← 0.18 |
| Character | CHAR | Single letters or symbols | CHAR grade |
| String | STRING | Words, sentences, identifiers | STRING name |
| Boolean | BOOLEAN | True/False conditions | BOOLEAN isValid |
A constant’s value never changes during program execution. Use the CONST keyword.
| Syntax | Example |
|---|---|
CONST MAX_STUDENTS = 30 | Maximum number of students allowed in a class. |
CONST TAX_RATE ← 0.18 | Fixed tax rate (18%). |
Assign a value or the result of an expression to a variable using the assignment arrow (←).
| Operator | Category | Example |
|---|---|---|
+, -, *, / | Arithmetic | total ← (price * qty) + tax - discount |
=, ≠, <, ≤, >, ≥ | Relational | IF age ≥ 18 THEN … END IF |
AND, OR, NOT | Logical | IF isMember AND NOT overdue THEN … END IF |
Operator precedence follows the usual mathematical rules: parentheses → multiplication/division → addition/subtraction → relational → logical. Use parentheses to make complex expressions clear.
WRITE "Enter the number of items: "
READ n
READ name, age
WRITE "Result = ", result
WRITE "Average = ", avg, " (", count, " values)"Cambridge pseudocode permits a small set of standard functions. When a function is not listed in the syllabus, you may assume it is provided by the exam board.
| Function | Purpose | Example Use |
|---|---|---|
ABS(x) | Absolute value | distance ← ABS(x2 - x1) |
SQRT(x) | Square root | root ← SQRT(number) |
LEN(s) | Length of a string | IF LEN(name) = 0 THEN … END IF |
INT(x), REAL(x) | Type conversion | whole ← INT(value) |
IF condition THEN
statements
ELSE
other statements
END IF
Use CASE when a variable can take one of several discrete values.
CASE grade OF
"A":
WRITE "Excellent"
"B":
WRITE "Good"
"C":
WRITE "Average"
OTHERWISE:
WRITE "Other grade"
END CASE
| Loop Type | Syntax | Typical Use | When to Choose |
|---|---|---|---|
| Count‑controlled (FOR) | FOR i ← start TO end DO … END FOR | Known number of repetitions (e.g., iterate over an array). | When the exact number of iterations is known before the loop starts. |
| Pre‑condition (WHILE) | WHILE condition DO … END WHILE | Repeat while a condition is true; condition checked before each iteration. | When the loop may execute zero times and the condition is naturally expressed at the start. |
| Post‑condition (REPEAT … UNTIL) | REPEAT … UNTIL condition | Execute at least once; condition checked after the body. | When the body must run at least once (e.g., menu‑driven programs). |
FOR loop – sum of first n integers
READ n
INTEGER sum ← 0
FOR i ← 1 TO n DO
sum ← sum + i
END FOR
WRITE "Sum = ", sum
WHILE loop – print even numbers up to n
READ n
INTEGER i ← 2
WHILE i ≤ n DO
WRITE i
i ← i + 2
END WHILE
REPEAT…UNTIL loop – menu selection
INTEGER choice
REPEAT
WRITE "1 – Add"
WRITE "2 – Subtract"
WRITE "0 – Exit"
READ choice
IF choice = 1 THEN
// add routine
ELSE IF choice = 2 THEN
// subtract routine
END IF
UNTIL choice = 0
WRITE "Good‑bye!"
Procedures perform a task and may receive parameters. They do not return a value.
PROCEDURE displayMaximum(A, B, C)
// parameters are passed BY VALUE unless otherwise stated
IF A > B THEN
IF A > C THEN
WRITE "Maximum is ", A
ELSE
WRITE "Maximum is ", C
END IF
ELSE
IF B > C THEN
WRITE "Maximum is ", B
ELSE
WRITE "Maximum is ", C
END IF
END IF
END PROCEDURE
A function must end with an assignment to a variable that has the same name as the function.
FUNCTION factorial(n) // n is passed BY VALUE
IF n = 0 THEN
factorial ← 1
ELSE
factorial ← n * factorial(n - 1) // recursive call
END IF
END FUNCTION
Syntax for explicit reference (if required by the exam board):
PROCEDURE swap(REF a, REF b)
INTEGER temp ← a
a ← b
b ← temp
END PROCEDURE
When a function calls itself, a base case must be defined to stop the infinite recursion.
FUNCTION fibonacci(n)
IF n = 0 THEN
fibonacci ← 0
ELSE IF n = 1 THEN
fibonacci ← 1
ELSE
fibonacci ← fibonacci(n - 1) + fibonacci(n - 2)
END IF
END FUNCTION
CONST ONE ← 1
INTEGER a, b, c
READ a, b, c
CALL displayMaximum(a, b, c) // procedure call
INTEGER fact ← factorial(5) // function call, result stored in fact
WRITE "5! = ", fact
IF … THEN … ELSE … END IF or CASE … OF … END CASE blocks.FOR, WHILE or REPEAT … UNTIL construct, justifying the choice.READ and WRITE statements where the design uses input/output parallelograms.PROCEDURE or FUNCTION blocks and note the parameter passing mode.Design: Read n, compute S = 1 + 2 + … + n, display S.
READ n
INTEGER i ← 1
INTEGER sum ← 0
FOR i ← 1 TO n DO
sum ← sum + i
END FOR
WRITE "The sum is ", sum
| Design Element | Flowchart Symbol | Structured English Cue | Pseudocode Equivalent |
|---|---|---|---|
| Start / End | Oval | “Begin”, “End” | Implicit – no code required |
| Process / Assignment | Rectangle | “Set”, “Assign” | variable ← expression |
| Decision / Condition | Diamond | “If … then … else …” | IF condition THEN … ELSE … END IF |
| Multi‑way Decision | Diamond with multiple exits | “Case … of …” | CASE variable OF … END CASE |
| Loop (count‑controlled) | Rectangle with back‑arrow, labelled “FOR i=1 TO n” | “For i = 1 to n do … End for” | FOR i ← 1 TO n DO … END FOR |
| Loop (pre‑condition) | Diamond with back‑arrow, labelled “WHILE …” | “While … do … End while” | WHILE condition DO … END WHILE |
| Loop (post‑condition) | Diamond with back‑arrow, labelled “REPEAT … UNTIL …” | “Repeat … until …” | REPEAT … UNTIL condition |
| Input / Output | Parallelogram | “Get …”, “Display …” | READ … / WRITE … |
| Constant | Usually noted inside a process box | “Constant … = …” | CONST NAME = value |
| Procedure | Separate sub‑chart (optional) | “Procedure …” | PROCEDURE name(parameters) … END PROCEDURE |
| Function | Separate sub‑chart (optional) | “Function … returns …” | FUNCTION name(parameters) … END FUNCTION |
Given a flowchart that reads an integer n, then prints all even numbers from 2 up to n, write the corresponding pseudocode.
READ n
INTEGER i ← 2
WHILE i ≤ n DO
WRITE i
i ← i + 2
END WHILE
Convert the following structured English into pseudocode:
Get a list of marks.
Set total to 0.
For each mark in the list do
Add mark to total.
End for.
Calculate average = total / number of marks.
Display average.
CONST ZERO ← 0
READ marks[] // assume marks is an array
INTEGER total ← ZERO
INTEGER count ← LEN(marks) // number of marks in the list
FOR i ← 1 TO count DO
total ← total + marks[i]
END FOR
REAL average ← total / count
WRITE "Average = ", average
Identify the error in the pseudocode below and correct it:
READ x
IF x > 0 THEN
WRITE "Positive"
ELSE IF x = 0 THEN
WRITE "Zero"
END IF
READ x
IF x > 0 THEN
WRITE "Positive"
ELSE IF x = 0 THEN
WRITE "Zero"
ELSE
WRITE "Negative"
END IF
The original code omitted the final ELSE branch and did not close the nested condition correctly. The corrected version uses a proper ELSE IF … THEN … END IF chain.
Write pseudocode that declares a constant TAX_RATE = 0.18, reads a price (real), calculates the tax amount and the final price, and displays both values with appropriate prompts.
CONST TAX_RATE ← 0.18
REAL price, tax, finalPrice
WRITE "Enter the price (excluding tax): "
READ price
tax ← price * TAX_RATE
finalPrice ← price + tax
WRITE "Tax amount = ", tax
WRITE "Final price = ", finalPrice
Using the built‑in function SQRT, write pseudocode to read a positive real number x, compute y = √x, and display y rounded to two decimal places. Include input validation (repeat until a positive number is entered).
REAL x, y
REPEAT
WRITE "Enter a positive number: "
READ x
UNTIL x > 0
y ← SQRT(x)
// rounding to two decimal places can be shown using a built‑in function ROUND if available
WRITE "Square root = ", y
Write a procedure swap that exchanges the values of two integer variables using pass‑by‑reference, then demonstrate its use in a main program.
PROCEDURE swap(REF a, REF b)
INTEGER temp ← a
a ← b
b ← temp
END PROCEDURE
INTEGER x ← 5
INTEGER y ← 12
CALL swap(x, y)
WRITE "x = ", x, ", y = ", y // outputs: x = 12, y = 5
Define a function factorial that returns n! using recursion. Then write a main program that reads an integer n (0 ≤ n ≤ 10) and displays n!.
FUNCTION factorial(n)
IF n = 0 THEN
factorial ← 1
ELSE
factorial ← n * factorial(n - 1)
END IF
END FUNCTION
INTEGER n
READ n
IF n < 0 OR n > 10 THEN
WRITE "Invalid input"
ELSE
INTEGER result ← factorial(n)
WRITE n, "! = ", result
END IF
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.