CIE IGCSE | 7.2.5 Pseudocode and Precise Algorithm Design

Lesson objective

Read and write precise pseudocode using variables, assignment, input, output, selection and iteration, and check algorithms with worked traces.

Learn

7.2.5 | PSEUDOCODE AND PRECISE ALGORITHM DESIGN

01 | PSEUDOCODE IS A PLAN YOU CAN READ

You do not need to know a programming language before learning pseudocode. Begin with something familiar: a set of instructions that another person can follow.

“Work out the score” is an intention, but it does not explain the steps. Which scores should be used? Should they be added or averaged? When should the result be displayed? An algorithm must remove these uncertainties.

Pseudocode is a structured written representation of an algorithm. It uses recognisable instructions such as INPUT, OUTPUT and IF without using the full syntax of a particular programming language.

It is more precise than an informal paragraph. The Cambridge syllabus gives conventions for the pseudocode used in exams; this lesson follows those conventions. Pseudocode is usually read and traced rather than run directly as a Python program.

We will introduce a few instructions at a time, then combine them into one complete score-processing algorithm.

02 | UNDERSTAND NAMES AND VALUES FIRST

A variable is a named place for a value that can change while an algorithm runs. Imagine a labelled card: the label stays the same, but you can replace the value written on it.

The name Score might currently refer to the value 7. Later, after another input, it might refer to 9. The name and the value are different things.

A declaration introduces a variable and its data type. The data type describes the kind of value it is intended to hold. INTEGER means a whole number; REAL allows a fractional part; STRING holds text; CHAR holds one character; and BOOLEAN holds TRUE or FALSE.

DECLARE Score : INTEGER
DECLARE Average : REAL
DECLARE StudentName : STRING

DECLARE Score : INTEGER introduces the name Score for whole-number values. It does not assign a starting score of zero. If the algorithm needs zero, it must say so separately.

Meaningful names help you follow the task. Cambridge’s convention uses names such as TotalScore, beginning with a capital letter and using letters and digits rather than spaces or underscores. Keywords such as INPUT must not be used as variable names.

03 | ASSIGNMENT: STORE A VALUE USING A NAME

The assignment arrow ← means store the value on the right using the name on the left. Read it as “gets” or “is assigned”, rather than as a mathematical equality.

Score ← 7
Score ← Score + 2
Total ← Score * 3

First, Score gets 7. Next, the algorithm uses the current Score value, adds 2 and stores the result back in Score. Score is now 9. Finally, Total gets Score multiplied by 3, so Total becomes 27.

Score ← Score + 2 may look strange if you read it as algebra. It makes sense as an update: read the old value, calculate a new value and replace the old one.

Assignment updates followed in order
InstructionWhat happensCurrent values
Score ← 7Store 7 in ScoreScore = 7
Score ← Score + 2Read 7; add 2; store 9Score = 9
Total ← Score * 3Read 9; multiply by 3Score = 9; Total = 27

The order matters. The right-hand expression is evaluated using the values available at that moment. A later line does not travel backwards and change a calculation already performed.

04 | INPUT AND OUTPUT: RECEIVE AND COMMUNICATE

INPUT Score receives a value and stores it in Score. OUTPUT Score communicates the current value of Score. These instructions have different jobs.

OUTPUT "Enter your score"
INPUT Score
OUTPUT "Your score is ", Score

The first OUTPUT displays a prompt so the user knows what to enter. INPUT then receives the value. If the user enters 8, the final OUTPUT displays the message followed by 8.

Quotation marks distinguish literal text from a variable name. OUTPUT "Score" displays the word Score. OUTPUT Score displays the value currently associated with the name.

A comma separates items to output. In OUTPUT "Your score is ", Score, the quoted part is fixed text and the unquoted part supplies the changing value.

This is why you should input or assign a variable before using its value. Declaring Score alone does not give you a score to display.

05 | START WITH A PRECISE SEQUENCE

Let us receive two numbers and display their sum. Before writing instructions, identify the inputs, process and output: input two numbers, add them, then display the total.

DECLARE FirstNumber : INTEGER
DECLARE SecondNumber : INTEGER
DECLARE Total : INTEGER

INPUT FirstNumber
INPUT SecondNumber
Total ← FirstNumber + SecondNumber
OUTPUT Total

The declarations explain the kinds of values used. The two INPUT instructions receive the numbers. Assignment calculates and stores their sum, and OUTPUT displays the result.

For inputs 4 and 3, FirstNumber becomes 4, SecondNumber becomes 3 and Total becomes 7. This is sequence: each instruction happens in the order written.

Putting OUTPUT Total before the calculation would use Total before this algorithm has assigned it a value. The correct instructions in the wrong order can still produce an incorrect solution.

06 | A CONDITION ASKS A TRUE–FALSE QUESTION

A condition compares values or combines comparisons to produce TRUE or FALSE. It does not assign a new value.

Score ≥ 5 asks whether Score is at least 5. In Cambridge pseudocode, the same comparison is commonly written Score >= 5. With Score equal to 7 it is true; with Score equal to 4 it is false.

Cambridge relational operators
ComparisonMeaningExample
=Equal toScore = 5
<>Not equal toScore <> 5
< and >Less than; greater thanScore < 5
<= and >=Less than or equal; greater than or equalScore >= 5

Compare Score ← 5 with Score = 5. The arrow assigns 5 to Score. The equals sign in a condition asks whether Score already equals 5.

Do not copy Python’s == or != into these examples. Cambridge uses = for equality and <> for not equal. Learning the chosen convention avoids mixing languages.

07 | IF: CHOOSE ONE PATH

Selection chooses actions according to a condition. The IF statement below outputs one of two messages.

IF Score >= 5
  THEN
    OUTPUT "Target reached"
  ELSE
    OUTPUT "Keep practising"
ENDIF

Read the condition first. If Score is at least 5, execute the THEN branch. Otherwise execute the ELSE branch. ENDIF closes the selection.

With Score equal to 5, the condition is true because “at least” includes equality. The output is “Target reached”. With Score equal to 4, it is false and the output is “Keep practising”.

The algorithm does not execute both branches for the same check. Indentation makes it easier to see which instructions belong to each branch. The examples put THEN and ELSE on separate lines, following the presentation in the syllabus.

A missing ELSE is allowed when nothing needs to happen on the false path, but an explicit branch is helpful when the task requires two different outcomes.

08 | SAY EXACTLY WHAT THE CONDITION MEANS

“If the number is okay” is too vague. Suppose a valid score is a whole number from 0 to 10 inclusive. You need to check both limits.

Score >= 0 AND Score <= 10 is true only when both comparisons are true. At 8, both are true. At 12, the upper-limit comparison is false. At −1, the lower-limit comparison is false.

AND requires both conditions. OR requires at least one. NOT reverses a true/false result. The expression Score < 0 OR Score > 10 detects values outside the range.

Using OR between “at least zero” and “at most ten” is a common mistake. Twelve is at least zero, so that OR expression would wrongly accept twelve.

For these introductory examples, assume input values are whole numbers. Checking type or handling non-numeric input is an additional requirement; a range comparison alone does not check every aspect of validity.

09 | FOR: REPEAT A KNOWN NUMBER OF TIMES

Iteration means repetition. A FOR loop is useful when the number of repetitions is known in advance.

DECLARE Index : INTEGER
FOR Index ← 1 TO 3
    OUTPUT Index
NEXT Index

Index takes the values 1, 2 and 3 in turn. The instruction inside the loop outputs each value. NEXT Index marks the end of the loop body and advances to the next value.

Cambridge’s FOR range includes both endpoints, so the output is 1, 2 and 3. You do not need to put a separate Index ← Index + 1 in this simple FOR loop.

The loop variable is declared as INTEGER. Instructions indented between FOR and NEXT belong to the repeated body. An instruction after NEXT runs after all repetitions have finished.

10 | WHILE AND REPEAT: READ THE STOPPING RULE

A WHILE loop checks its condition before the body. It keeps going while the condition is true.

DECLARE Count : INTEGER
Count ← 1
WHILE Count <= 3 DO
    OUTPUT Count
    Count ← Count + 1
ENDWHILE

Count starts at 1. The body outputs it and increases it. After outputting 3, Count becomes 4; the condition is false and the loop stops. Without the increase, Count would remain 1 and this loop would not reach its exit.

A REPEAT loop checks after its body, so the body runs at least once. It stops when the UNTIL condition is true.

DECLARE Score : INTEGER
REPEAT
    OUTPUT "Enter a whole-number score from 0 to 10"
    INPUT Score
UNTIL Score >= 0 AND Score <= 10

This example asks for another score until it lies between 0 and 10. With inputs −1, 12 and 8, the first two checks are false and the final check is true. Eight remains stored in Score when the loop ends.

Remember the difference: WHILE continues on true; REPEAT UNTIL stops on true. Both need a way for the relevant values to change.

11 | BUILD A COMPLETE SOLUTION FROM A SCENARIO

Here is the task: receive three whole-number scores, each from 0 to 10, then display their total and average. If a score is outside the range, ask again before counting it as one of the three scores.

Decompose the task into receiving a valid score, adding it to a running total, repeating for three scores and calculating the average. The average is the total divided by the number of scores.

We need Score for the current input, Total for the accumulated score, Index for the three repetitions and Average for a possibly fractional result. Initialise Total to zero before adding anything.

DECLARE Score : INTEGER
DECLARE Total : INTEGER
DECLARE Index : INTEGER
DECLARE Average : REAL

// Assume every input is a whole number
Total ← 0
FOR Index ← 1 TO 3
    REPEAT
        OUTPUT "Enter a score from 0 to 10"
        INPUT Score
    UNTIL Score >= 0 AND Score <= 10
    Total ← Total + Score
NEXT Index
Average ← Total / 3
OUTPUT "Total: ", Total
OUTPUT "Average: ", Average

The FOR loop counts three accepted scores. Inside each repetition, REPEAT may ask several times until one valid score is obtained. Only then is that score added to Total.

Average is calculated after NEXT, so the total is complete before division. A comment beginning with // explains an assumption; it is not an instruction executed by the algorithm.

12 | TRACE THE COMPLETE ALGORITHM

Use accepted scores 4, 7 and 9. Total begins at 0. The first accepted score makes it 4. The second makes it 11. The third makes it 20. Average becomes 20 ÷ 3, approximately 6.67.

Three accepted scores; average calculated after the loop
StageAccepted scoreTotal after addition
InitialisationNone0
Index = 144
Index = 2711
Index = 3920

The algorithm does not specify rounding, so its average is the real value 20/3. If the requirements asked for two decimal places, a separate rounding or display-format decision would be needed.

Now try entries 4, 12, 7 and 9. Twelve fails the range check. It is not added and does not complete that FOR repetition. Seven is entered during the same repetition and becomes the second accepted score. The final total is still 20.

This trace shows why the input-validation loop belongs inside the three-score loop and why addition happens after a valid score has been obtained.

13 | FIX ERRORS BY FOLLOWING THE VALUES

Suppose Total ← 0 is moved inside the FOR body, just before addition. Every repetition resets the accumulated total. With scores 4, 7 and 9, the total ends at 9 rather than 20.

If addition is placed inside REPEAT before the validity check, an invalid entry may be added. If Average is calculated before any accepted scores are added, it uses an incomplete total.

These mistakes can look small on the page, but they change the algorithm. Ask what value each name holds immediately before and after a line. A trace is more useful than guessing that a line “looks wrong”.

Also check every closing keyword: IF needs ENDIF, WHILE needs ENDWHILE and FOR needs NEXT with its loop variable. REPEAT closes with UNTIL and a condition. Those endings make the intended grouping clear.

14 | PLAN BEFORE YOU WRITE THE WHOLE ALGORITHM

Start by restating the required result. Identify the inputs and their assumptions. Decide what values must be retained, then describe the main steps in ordinary language.

Choose a control structure for each job. A one-time action is a sequence step. A choice needs selection. A known number of repetitions suggests FOR. Repeated input until acceptable suggests REPEAT UNTIL.

Next translate each job into precise statements. Replace “check score” with an explicit condition, and replace “work out average” with the actual calculation.

Finally dry-run suitable data. Try an ordinary case, a limit value and a rejected input. Check outputs and stored values, not just whether you recognise the keywords. The aim is an algorithm another reader can follow without inventing missing instructions.

15 | PSEUDOCODE IN YOUR EXAM

For this 2026–2028 syllabus, Paper 2 coding solutions are required in pseudocode except for the 15-mark scenario question, where pseudocode, Python, Visual Basic or Java may be used. Practise the required representation rather than assuming Python is accepted for every coding question.

Use precise expressions such as X > Y, not “X is greater than Y” as an algorithm instruction. Follow Cambridge’s conventions for names, assignment, comparisons and control structures.

You do not have to make the page look like a printed exam listing with line numbers to understand the algorithm. In this lesson, unnumbered statements keep the focus on their meaning.

When asked to explain an algorithm, link steps to purposes: “Total is set to zero before the loop because it must accumulate all three accepted scores rather than restart at each repetition.”

The later programming lessons revisit these constructs in more depth. Here the goal is a reliable foundation: know what each line means, group the lines correctly and follow the changing values.

CHOOSE THE PRECISE INSTRUCTION

Terminology

Terminology

Pseudocode

A structured written representation of an algorithm following stated conventions.

Algorithm

A precise sequence of steps for completing a task.

Variable

A named place for a value that can change during execution.

Identifier

A name used for a variable, constant or routine.

Declaration

Introducing a name and its data type; it does not assign a starting value.

INTEGER

A whole-number data type.

REAL

A numeric data type capable of holding a fractional part.

STRING

A text data type holding a sequence of characters.

BOOLEAN

The logical data type with values TRUE and FALSE.

Assignment

Evaluating an expression and storing its value using the name on the left of ←.

Literal

A value written directly, such as 5 or "Hello".

Input

Receiving data into the algorithm.

Output

Communicating information or results.

Condition

An expression evaluated as TRUE or FALSE.

Sequence

Executing instructions in the order specified.

Selection

Choosing which instructions to execute according to a condition.

Iteration

Repeating a group of instructions.

FOR loop

Count-controlled repetition over specified values, including the endpoints in Cambridge pseudocode.

WHILE loop

Repetition while a pre-condition is true; may run zero times.

REPEAT UNTIL loop

Post-condition repetition that stops when its condition becomes true; runs at least once.

Initialisation

Assigning a starting value before it is used.

Running total

A value accumulating successive additions.

Dry run

Following an algorithm by hand using chosen data.

Comment

An explanatory note beginning with //; not an executed instruction.

Questions

Questions

CHECK YOUR UNDERSTANDING

Select all correct choices. Each exact set earns one point.

1. What does DECLARE Score : INTEGER do?
2. Score is 7. After Score ← Score + 2, what is Score?
3. Which distinctions are correct?
4. Score contains 8. Which statements are correct?
5. Which checks accept only scores from zero to ten inclusive?
6. Which loop statements are correct?
7. Why initialise Total before the three-score loop?
8. Scores entered are 4, 12, 7 and 9 in the full algorithm. Which are correct?

EXPLAIN AND COMPARE

Write your answers before revealing each sample.

1. Explain the difference between declaring and initialising a variable.

2. Explain how Score ← Score + 2 works when Score is seven.

3. Compare OUTPUT Score and OUTPUT "Score".

4. Explain why AND is needed to test an inclusive range zero to ten.

5. Describe the roles of FOR and REPEAT in the full algorithm.

6. Explain the result if Total ← 0 is placed inside the FOR body before each addition.

7. Explain why the average is calculated after the loop.

8. Suggest suitable test inputs for the range check and state expected outcomes.

BUILD IT | FOUR ITEM PRICES

Write pseudocode to input four non-negative item prices and output their total. Assume numeric input; use REAL values. Ask again for a negative price. Plan the variables, initialise the total once, choose an outer loop and put the retry loop in the correct place.

Reveal a possible solution
DECLARE Price : REAL
DECLARE Total : REAL
DECLARE Index : INTEGER
Total ← 0.0
FOR Index ← 1 TO 4
    REPEAT
        INPUT Price
    UNTIL Price >= 0.0
    Total ← Total + Price
NEXT Index
OUTPUT Total

Zero is accepted; a negative price is retried within the same item. The total is initialised before the outer loop and displayed after it.

Flashcards

Flashcards

Click to flip. Select the ideas you need to revisit.

0 cards selected for revision.

    Selections are kept while this page is open.

    Workbook

    Workbook

    COMING SOON

    The workbook for 7.2.5 Pseudocode and Precise Algorithm Design is coming soon. Complete the matching task and written questions in your exercise book.