Implement and write pseudocode from a given design presented as either a program flowchart or structured English

11.1 Programming Basics

Learning Objective

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.

Why Use Pseudocode?

  • Language‑independent description of an algorithm.
  • Bridges the gap between design (flowchart/structured English) and real code.
  • Allows you to check logic, data‑type choices and edge cases before programming.

Key Conventions for Pseudocode

  1. Reserved words in UPPER‑CASE: IF, THEN, ELSE, CASE, FOR, WHILE, REPEAT, UNTIL, END IF, END FOR, END WHILE, END REPEAT, READ, WRITE, CONST, etc.
  2. Indentation shows nesting of loops, conditionals and sub‑programs.
  3. Variable names should be meaningful, use camelCase or snake_case, and start with a letter.
  4. Input / Output: READ (optionally preceded by a prompt) and WRITE (or OUTPUT) for displaying results.
  5. Expressions are written as in mathematics; parentheses may be used to control precedence.

Data Types & Variable Declarations

Cambridge pseudocode recognises the following primitive data types. Declare a variable before it is used; initialise it if a value is required immediately.

Data TypeKeywordTypical UseExample Declaration
IntegerINTEGERCounts, whole‑number calculationsINTEGER i, total, n
Real (floating‑point)REALMeasurements, percentages, monetary valuesREAL price, taxRate ← 0.18
CharacterCHARSingle letters or symbolsCHAR grade
StringSTRINGWords, sentences, identifiersSTRING name
BooleanBOOLEANTrue/False conditionsBOOLEAN isValid

Constants

A constant’s value never changes during program execution. Use the CONST keyword.

SyntaxExample
CONST MAX_STUDENTS = 30Maximum number of students allowed in a class.
CONST TAX_RATE ← 0.18Fixed tax rate (18%).

Assignment Statements & Expressions

Assign a value or the result of an expression to a variable using the assignment arrow ().

OperatorCategoryExample
+, -, *, /Arithmetictotal ← (price * qty) + tax - discount
=, , <, , >, RelationalIF age ≥ 18 THEN … END IF
AND, OR, NOTLogicalIF 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.

Input & Output Statements

  • Prompting the user (optional but good practice):

    WRITE "Enter the number of items: "

  • Reading one or more values:

    READ n

    READ name, age

  • Formatted output:

    WRITE "Result = ", result

    WRITE "Average = ", avg, " (", count, " values)"

Built‑in Functions & Library Routines

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.

FunctionPurposeExample Use
ABS(x)Absolute valuedistance ← ABS(x2 - x1)
SQRT(x)Square rootroot ← SQRT(number)
LEN(s)Length of a stringIF LEN(name) = 0 THEN … END IF
INT(x), REAL(x)Type conversionwhole ← INT(value)

11.2 Constructs

Conditional Statements

IF … THEN … ELSE … END IF

IF condition THEN

statements

ELSE

other statements

END IF

CASE … OF … END CASE

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

Looping Constructs

Loop TypeSyntaxTypical UseWhen to Choose
Count‑controlled (FOR)FOR i ← start TO end DO … END FORKnown 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 WHILERepeat 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 conditionExecute at least once; condition checked after the body.When the body must run at least once (e.g., menu‑driven programs).

Examples

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!"

11.3 Structured Programming (A‑Level)

Procedures (Sub‑programs without a return value)

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

Functions (Sub‑programs that return a value)

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

Parameter Passing

  • PASS BY VALUE: a copy of the argument is supplied; changes inside the sub‑program do not affect the original variable.
  • PASS BY REFERENCE (optional in Cambridge, but useful for A‑Level): the address of the variable is supplied; modifications affect the original variable.

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

Recursion (A‑Level)

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

Using Procedures and Functions in a Main Program

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

From Design to Pseudocode – A Systematic Approach

  1. Identify Start and End symbols (they generate no code).
  2. Traverse the design (top‑to‑bottom or left‑to‑right) and list each symbol.
  3. Translate process rectangles into assignment statements or built‑in function calls.
  4. Translate decision diamonds into IF … THEN … ELSE … END IF or CASE … OF … END CASE blocks.
  5. Translate loops into the appropriate FOR, WHILE or REPEAT … UNTIL construct, justifying the choice.
  6. Insert READ and WRITE statements where the design uses input/output parallelograms.
  7. If the design includes sub‑programs, write them as PROCEDURE or FUNCTION blocks and note the parameter passing mode.

Example Flowchart – Sum of First n Integers

Design: Read n, compute S = 1 + 2 + … + n, display S.

Pseudocode (using a count‑controlled loop)

READ n

INTEGER i ← 1

INTEGER sum ← 0

FOR i ← 1 TO n DO

sum ← sum + i

END FOR

WRITE "The sum is ", sum

Summary – Mapping Design Elements to Pseudocode

Design ElementFlowchart SymbolStructured English CuePseudocode Equivalent
Start / EndOval“Begin”, “End”Implicit – no code required
Process / AssignmentRectangle“Set”, “Assign”variable ← expression
Decision / ConditionDiamond“If … then … else …”IF condition THEN … ELSE … END IF
Multi‑way DecisionDiamond 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 / OutputParallelogram“Get …”, “Display …”READ … / WRITE …
ConstantUsually noted inside a process box“Constant … = …”CONST NAME = value
ProcedureSeparate sub‑chart (optional)“Procedure …”PROCEDURE name(parameters) … END PROCEDURE
FunctionSeparate sub‑chart (optional)“Function … returns …”FUNCTION name(parameters) … END FUNCTION

Practice Questions

  1. Given a flowchart that reads an integer n, then prints all even numbers from 2 up to n, write the corresponding pseudocode.

    Solution

    READ n

    INTEGER i ← 2

    WHILE i ≤ n DO

    WRITE i

    i ← i + 2

    END WHILE

  2. 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.

    Solution

    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

  3. 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

    Correction

    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.

  4. 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.

    Solution

    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

  5. 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).

    Solution

    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

  6. Write a procedure swap that exchanges the values of two integer variables using pass‑by‑reference, then demonstrate its use in a main program.

    Solution

    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

  7. 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!.

    Solution

    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