Skip to content
Cambridge AS & A Level9618

Algorithms (AS Level)

Computer Science 9618 Chapter Notes

Last updated November 2026

What this chapter covers

Algorithms
ShareWhatsAppPost

Algorithms (AS Level) notes as a printable PDF.

Read online free. Download a watermarked copy with a free account.

Read the notes

The full Algorithms (AS Level) notes as text: skim, search, and jump between subtopics.

~15 min read

1. Algorithms and identifier tables

An algorithm is a solution to a problem expressed as a sequence of defined steps.

The data a problem uses is held under suitable identifier names, chosen to say what each item holds. Record them in an identifier table: each identifier, its data type and an explanation of what it holds.

An array gives many values a single identifier, and a loop can then reach each value by its index.

Examiner insight

“The most popular answers were that an array allows for all data items to be referenced by a single identifier name or that the arrays can be iterated through using a loop and the index value for each element.” “Variable names such as ‘flag’ for row 2 and ‘Date’ for row three were too imprecise. ‘Date’ is also the name of a data type and therefore a keyword.”

Worked example 13 marks

A program inputs the 30 whole-number test marks of a class and outputs the highest mark. Complete the identifier table.

identifierdata typeexplanation
`Marks`
`Highest`
`Index`
  1. 1

    `Marks`: `ARRAY[1:30] OF INTEGER`, holds the 30 marks under one identifier [1]

  2. 2

    `Highest`: `INTEGER`, holds the largest mark found so far [1]

  3. 3

    `Index`: `INTEGER`, the loop counter used as the array index [1]

2. Pseudocode and the three constructs

A program has input, process and output. Keywords are capitals; `←` assigns.

Every algorithm is built from three constructs: sequence (steps in order), selection (`IF`) and iteration (a loop).

Name a loop by where its test sits. A pre-condition loop (`WHILE`) tests before each pass. A post-condition loop (`REPEAT ... UNTIL`) tests after each pass.

``` Passes ← 0 FOR Count ← 1 TO 5 INPUT Mark IF Mark >= 50 THEN Passes ← Passes + 1 ENDIF NEXT Count OUTPUT Passes ```

Examiner insight

“Many candidates initialised this to zero or a large negative number, but the question gave no indication as to the range of values input and so the only correct solution was to take the first value input and to use this as the initial Max value.” “Candidates need to use the correct terminology from the syllabus when describing a loop, which in this case was either a ‘post-condition’ loop or a ‘pre-condition’ loop.”

Worked example 15 marks

A program inputs 40 integer values, one at a time, and outputs the largest. The range of the values is not known. Write the algorithm in pseudocode.

  1. 1

    Input the first value and assign it to `Max` (not 0) [1]

  2. 2

    A loop that repeats 39 more times: `FOR Count ← 2 TO 40` [1]

  3. 3

    Prompt and `INPUT Value` inside the loop [1]

  4. 4

    `IF Value > Max THEN Max ← Value ENDIF` inside the loop [1]

  5. 5

    After the loop, `OUTPUT Max` [1]

3. Describing an algorithm in structured English

Structured English describes an algorithm as numbered plain-language steps, with no pseudocode statements. It must still map directly to a program.

Name the variable and initialise it. Give the number of iterations of any loop. Say what is compared with what.

Examiner insight

“Steps were often vague and frequently simply repeated phrases from the question.” “A common omission from the answers given was not declaring and initialising the two sum variables.”

Worked example 15 marks

A 1D array Data of type integer contains 200 elements. Each element has a unique value.

An algorithm is required to search for the largest value and output it.

Describe the steps that the algorithm should perform.

Do not include pseudocode statements in your answer.

  1. 1

    Declare a variable `Max` [1]

  2. 2

    Assign the value of the first element to `Max` [1]

  3. 3

    Set up a loop to repeat 200 times, using the loop counter as the array index [1]

  4. 4

    If the value of the current element is greater than `Max`, assign it to `Max` [1]

  5. 5

    After the loop, output `Max` [1]

4. Flowcharts

A flowchart documents the same algorithm as boxes joined by arrows, between START and END. A process or input step is one box. A test is a decision diamond with two branches labelled True and False.

A loop is drawn as an arrow back to the decision, with its counter set before and changed inside. It is never written as one box.

Examiner insight

“Some candidates unsuccessfully attempted to implement the loop via a single flowchart symbol containing a description such as ‘Repeat while Index < 51’.” “The algorithm required the tracking of the current minimum value and its index position; the second part was often not present.”

Worked example 15 marks

Data is a 1D array of integers, containing 30 elements. All element values are unique.

An algorithm will output the index of the element with the smallest value.

Draw a program flowchart to represent the algorithm.

  1. 1

    Process box: `Min` set to the first value in `Data` and `MinIndex` set to 1 [1]

  2. 2

    Loop through the remaining 29 values (or 30 in total): a decision diamond on the counter, branches labelled True and False [1]

  3. 3

    Decision diamond: is `Data[Index]` less than `Min`? [1]

  4. 4

    True branch: set new `Min` and save `MinIndex`, inside the loop [1]

  5. 5

    After the loop, output `MinIndex`, then END [1]

5. Pseudocode from a description

Translate step by step. Each numbered step becomes one or more statements. A decision diamond that leads back becomes a conditional loop. A diamond that skips forward becomes an `IF`.

Declare every variable used. Make sure the loop's counter is changed inside it.

Examiner insight

“Most candidates identified the loop, although some omitted the increment of Index or included this in only one branch of the enclosed conditional clause.” “Where a loop was omitted, the tendency was to replace it with a conditional clause (IF Index < 7).”

Worked example 18 marks

A student has been asked to create a simple guessing game program. This program will generate a random integer value between 1 and 100. It will then repeatedly prompt the user to input an integer value until they input the randomly generated value.

The student has written a structured English description:

step 1 – randomly generate an integer value between 1 and 100 inclusive

step 2 – prompt the user to input an integer value

step 3 – output an appropriate message if the value input was too high; then repeat from step 2

step 4 – output an appropriate message if the value input was too low; then repeat from step 2

step 5 – output an appropriate message if the value input was the same value that was randomly generated; then end the program.

Write a pseudocode algorithm from this structured English description.

Assume no input validation is needed.

  1. 1

    Declare all variables used, such as `GeneratedValue` and `Guess` [1]

  2. 2

    Use the `RAND()` function [1]

  3. 3

    Use the `INT()` function [1]

  4. 4

    Correct syntax for 1 to 100: `GeneratedValue ← INT(RAND(100)) + 1` [1]

  5. 5

    Conditional loop until the number is guessed: `REPEAT ... UNTIL Guess = GeneratedValue` [1]

  6. 6

    Prompt and `INPUT Guess` inside the loop [1]

  7. 7

    Inside the loop, test whether the guess is too high and whether it is too low [1]

  8. 8

    Output an appropriate message for each case, and a correct-guess message after the loop [1]

6. Stepwise refinement

Stepwise refinement takes a general statement of an algorithm and breaks each step into smaller, more detailed steps. Repeat until the steps are at a level of detail from which the task may be programmed.

A vague step such as 'repeat until sorted' is replaced by the loops, the tests and the assignments that carry it out.

Worked example 16 marks

An algorithm to sort a 1D array into ascending order is described as follows:

  • move the largest value to the end
  • keep repeating until the array is sorted.

Apply the process of stepwise refinement to this algorithm in order to produce a more detailed description.

Write the more detailed description using structured English. Your explanation of the algorithm should not include pseudocode statements.

  1. 1

    Use a variable as an index to the array [1]

  2. 2

    A loop to iterate through the array [1]

  3. 3

    An inner loop with a reducing range [1]

  4. 4

    Test whether the current element is greater than the next element [1]

  5. 5

    If so, swap the elements [1]

  6. 6

    Describe the swap: copy one value to a temporary variable, then overwrite each element in turn [1]

7. Logic statements in an algorithm

A logic statement is a condition built with comparisons joined by `AND`, `OR` and `NOT`. It decides which branch is taken or when a loop ends.

In `WHILE Flag = FALSE AND Count <= 5` both conditions must hold for the loop to continue. Use brackets to make the order clear.

Examiner insight

“Very few solutions terminated the loop when 10 non-blank lines had been read.”

Worked example 13 marks

A lift may move only if the load is under 600 kg and either the door is closed or the override is on. Write the condition for the lift to move, using the variables `Load`, `DoorClosed` and `Override`.

  1. 1

    `Load < 600` as one test [1]

  2. 2

    `DoorClosed = TRUE OR Override = TRUE` as the other [1]

  3. 3

    Joined with `AND` and bracketed: `Load < 600 AND (DoorClosed = TRUE OR Override = TRUE)` [1]

Recap

  • An algorithm is a solution to a problem expressed as a sequence of defined steps.
  • Name each data item with a suitable identifier and record it in an identifier table.
  • Every algorithm uses sequence, selection and iteration.
  • A pre-condition loop tests before each pass; a post-condition loop tests after.
  • Structured English: name and initialise variables, state loop counts, no pseudocode statements.
  • Flowchart: decision diamond, True and False branches, a loop is an arrow back.
  • Initialise a maximum to the first value input.
  • Stepwise refinement: break each step down until it can be programmed.
  • Use AND, OR and brackets to define conditions exactly.

End-of-chapter exercise

Test yourself on the whole chapter. Work through these before moving on.

  1. State what is meant by an algorithm.1 mark
    Answer
    1. A solution to a problem expressed as a sequence of defined steps.
  2. A program stores a customer's name, the date of the customer's last visit and whether the customer is a member. Give a suitable identifier name and data type for each.3 marks
    Answer
    1. `CustomerName` : `STRING` [1].
    2. `LastVisitDate` : `DATE` [1].
    3. `IsMember` : `BOOLEAN` [1].

    Trap: `Date` is the name of a data type and so a keyword, and a name such as `Flag` does not say what it holds.

  3. State the value output by this pseudocode. ``` Total ← 0 FOR Count ← 1 TO 4 Total ← Total + Count * 2 NEXT Count OUTPUT Total ```3 marks
    Answer
    1. The loop runs for `Count` = 1, 2, 3, 4 [1].
    2. `Total` takes the values 2, 6, 12, 20 [1].
    3. Output: 20 [1].

    Trap: Stopping at `Count` = 3 gives 12; the loop includes its end value.

  4. A program must keep asking for a number until 0 is input. State the type of loop that suits this and give a reason.2 marks
    Answer
    1. A post-condition loop (`REPEAT ... UNTIL`) [1].
    2. The body runs once before the test, so the first number is read before 0 is checked [1].

    Trap: 'A repeat loop' on its own names a keyword, not the type of loop.

  5. Describe the steps of an algorithm that inputs 20 temperatures and outputs how many were below zero. Do not include pseudocode statements.5 marks
    Answer
    1. Declare a counter and set it to zero [1].
    2. Repeat the following 20 times [1].
    3. Prompt and input a temperature [1].
    4. If the temperature is less than zero, add one to the counter [1].
    5. After the loop, output the counter [1].

    Trap: A loop with no stated number of iterations, or a counter that is never set to zero.

  6. Write pseudocode from this structured English. step 1 – set Sum to zero step 2 – prompt and input a number step 3 – if the number is greater than zero, add it to Sum step 4 – repeat from step 2 until the number input is 0 step 5 – output Sum6 marks
    Answer
    1. ``` DECLARE Sum : INTEGER DECLARE Number : INTEGER Sum ← 0 REPEAT OUTPUT "Enter a number" INPUT Number IF Number > 0 THEN Sum ← Sum + Number ENDIF UNTIL Number = 0 OUTPUT Sum ```
    2. Declare and initialise `Sum` [1], conditional loop [1], prompt [1], input in loop [1], test and add [1], output after loop [1].

    Trap: `WHILE Number <> 0` before any number is input tests an unset variable.

  7. A flowchart sets Count to 0. A decision asks whether Count < 3. On True it outputs Count, adds 1 to Count and returns to the decision. On False it ends. Write the pseudocode and state the output.4 marks
    Answer
    1. ``` Count ← 0 WHILE Count < 3 OUTPUT Count Count ← Count + 1 ENDWHILE ```
    2. Initialise [1], loop with the correct condition [1], output and increment inside [1].
    3. Output: 0, 1, 2 [1].

    Trap: Replacing the loop with `IF Count < 3` runs the body once only.

  8. Apply stepwise refinement to: 'find the average of the positive numbers in an array of 10 numbers'. Write the refined steps in structured English.5 marks
    Answer
    1. Set the sum and the count of positive numbers to zero [1].
    2. Loop through the 10 elements using an index [1].
    3. If an element is greater than zero, add it to the sum and add one to the count [1].
    4. After the loop, if the count is greater than zero, divide the sum by the count [1].
    5. Output the result [1].

    Trap: Dividing by 10 instead of the count of positive numbers, or dividing when the count is zero.

  9. A cash machine allows a user at most 3 attempts to enter a PIN. The correct PIN is 4721. Write pseudocode using a pre-condition loop that stops as soon as the correct PIN is input or 3 attempts have been made, then outputs "Access granted" or "Locked". State the output for the inputs 1111 then 4721. Explain what goes wrong if the two loop conditions are joined with `OR` instead of `AND`.7 marks
    Answer
    1. ``` DECLARE PIN : INTEGER DECLARE Attempts : INTEGER DECLARE Correct : BOOLEAN Attempts ← 0 Correct ← FALSE WHILE Correct = FALSE AND Attempts < 3 OUTPUT "Enter PIN" INPUT PIN Attempts ← Attempts + 1 IF PIN = 4721 THEN Correct ← TRUE ENDIF ENDWHILE IF Correct = TRUE THEN OUTPUT "Access granted" ELSE OUTPUT "Locked" ENDIF ```
    2. Initialise `Attempts` and `Correct` [1], `WHILE` with both conditions joined by `AND` [1], input and increment inside the loop [1], `Correct` set when the PIN matches [1], final test after the loop [1].
    3. 1111: `Attempts` = 1, `Correct` = FALSE, so the loop repeats. 4721: `Attempts` = 2, `Correct` = TRUE, so the loop ends. Output: Access granted [1].
    4. With `OR`, the condition stays TRUE while `Correct` is FALSE, whatever `Attempts` is, so a user who never enters 4721 is asked forever and is never locked [1].

    Trap: A loop that does not end on both conditions: `OR` in place of `AND`, or a count that is never incremented.

  10. A weather station records the lowest night-time temperature, in °C, for 7 nights. The values are real numbers and may be negative. Write pseudocode that inputs the 7 values, one at a time, and outputs the number of nights below 0 °C and the highest of the 7 values. Give the two outputs for the values -3.5, -1.0, -6.0, -0.5, -2.0, -4.5, -1.5. State the highest value your algorithm would output if `Max` had been set to 0 before the loop.9 marks
    Answer
    1. ``` DECLARE Temp : REAL DECLARE Max : REAL DECLARE Nights : INTEGER DECLARE Night : INTEGER Nights ← 0 INPUT Temp Max ← Temp IF Temp < 0 THEN Nights ← Nights + 1 ENDIF FOR Night ← 2 TO 7 INPUT Temp IF Temp > Max THEN Max ← Temp ENDIF IF Temp < 0 THEN Nights ← Nights + 1 ENDIF NEXT Night OUTPUT Nights, Max ```
    2. Declarations with suitable types [1], `Nights` set to 0 [1], `Max` set to the first value input [1], first value also tested [1], loop 6 more times [1], both tests inside the loop [1].
    3. All 7 values are below 0, so `Nights` = 7 [1].
    4. `Max` goes -3.5, -1.0, -1.0, -0.5 and then stays, so the highest is -0.5 [1].
    5. With `Max ← 0`, no value is greater than 0, so it outputs 0, a value that was never input [1].

    Trap: `Max ← 0` is wrong when the range of values is unknown, and starting the loop at 2 must not drop the first value from the count.

Say this, not that

Wording that loses the mark, against the wording the mark scheme credits.

Loses the markGets the mark
a loop 'repeat while Index < 51' written in one flowchart boxa decision diamond with branches labelled True and False, and the counter changed in a separate box
`Max ← 0` before the loop`Max` set to the first value input
a 'repeat loop' or a 'while loop'a pre-condition loop or a post-condition loop
'use a loop' with no counta loop to repeat 200 times, using the counter as the array index

Frequently asked questions

What is an algorithm?
An algorithm is a solution to a problem expressed as a sequence of defined steps. Every algorithm is built from three constructs: sequence, selection and iteration. The data a problem uses is held under suitable identifier names, recorded in an identifier table.
What is the difference between a pre-condition loop and a post-condition loop?
A pre-condition loop tests before each pass, and a post-condition loop tests after each pass. A pre-condition loop is written as WHILE, and a post-condition loop is written as REPEAT ... UNTIL. Name a loop by where its test sits.
How do I find the largest value in an array in pseudocode?
Set Max to the first value input, then loop through the rest of the values. Inside the loop, IF Value > Max THEN Max ← Value ENDIF keeps the largest value found so far. After the loop, OUTPUT Max. The loop should repeat a stated number of times, using the loop counter as the array index.
How do I draw a flowchart for a loop?
A test is a decision diamond with two branches labelled True and False. A loop is drawn as an arrow back to the decision, with its counter set before the loop and changed inside it, in a separate box. A loop is never written as one box, such as repeat while Index < 51.
How do I describe an algorithm in structured English without pseudocode?
Write numbered plain-language steps, with no pseudocode statements. Name each variable and initialise it, and give the number of iterations of any loop, such as a loop to repeat 200 times using the loop counter as the array index. Say what is compared with what.
What mistake do students make when they set the maximum value in an algorithm?
Do not set Max ← 0 before the loop. Set Max to the first value input instead. Setting Max to 0 is wrong when the range of values is not known, because the largest value may be smaller than 0 and then the output would be wrong.

Free notes are just the start.

Unlock every Workbook and Chapter at a Glance, and generate your own worksheets and predicted papers.

Explore plans

Related chapters