Skip to content
Cambridge AS & A Level9626

Algorithms

Information Technology 9626 Chapter Notes

Last updated November 2026

ShareWhatsAppPost

Algorithms notes as a printable PDF.

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

Read the notes

The full Algorithms notes as text: skim, search, and jump between subtopics.

~16 min read

1. Pseudocode building blocks

Pseudocode uses capital-letter keywords. `INPUT` (or `READ`) takes a value in, and `WRITE` or `PRINT` sends one out. The mark schemes also write `OUTPUT`.

A value is stored with `←`: the variable goes on the left and the value on the right. `=` is a comparison. Arithmetic uses `+`, `-`, `*` and `/`, and comparison uses `>`, `<` and `=`.

A variable is initialised before it is used, for example `Count ← 0` before a loop. Each value read needs its own `INPUT`.

Examiner insight

“Using an input statement ‘input X and Y’. This would not correctly input the values for the two variables.” “It should be noted that where variable names are made up of two or more words, there should be no gap between these words.”

Worked example 13 marks

A program asks for two numbers. Write a pseudocode algorithm that inputs the two numbers, adds them and outputs the total.

  1. 1

    ``` INPUT Number1 INPUT Number2 Total ← Number1 + Number2 OUTPUT Total ```

  2. 2

    Each number is read with its own `INPUT` statement [1]

  3. 3

    The total is calculated with `+` and stored using `←` [1]

  4. 4

    The total is output [1]

2. Selection: IF and CASE

Conditional branching chooses between paths with `IF…THEN…ELSE…ENDIF`. `THEN` goes on a separate line from the `IF`, the lines inside are indented, and every `IF` is closed by its own `ENDIF` in capitals.

An action is credited only when it happens as a consequence of its own condition, so each `IF` holds only the actions for that condition.

`CASE…ENDCASE` chooses between several values of one variable:

``` CASE OF Choice 1 : OUTPUT "One" 2 : OUTPUT "Two" ENDCASE ```

Examiner insight

“Failure to close IF statements with ENDIF statements.” “However, some still struggled with the structure of the CASE ENDCASE construct.”

Worked example 18 marks

A combined heating and air conditioning system measures the temperature in the house. If the temperature exceeds 28 degrees Celsius, a fan will turn on and the heating system will turn off. If the temperature falls below 19 degrees Celsius, the fan will turn off and the heating will turn on.

Complete the pseudocode algorithm to show the processing that takes place in this combined system. The variable temperature represents the temperature of the house.

``` REPEAT INPUT temperature ```

  1. 1

    ``` REPEAT INPUT temperature IF temperature > 28 THEN Send signal to turn fan on Send signal to turn heating off ENDIF IF temperature < 19 THEN Send signal to turn fan off Send signal to turn heating on ENDIF UNTIL system switched off ```

  2. 2

    `IF temperature > 28` [1]

  3. 3

    `THEN` on a separate line from the `IF` [1]

  4. 4

    Send the signal to turn the fan on, indented, only as a result of `temperature > 28` [1]

  5. 5

    `ENDIF` correctly positioned [1]

  6. 6

    `IF temperature < 19` with its matching `ENDIF` [1]

  7. 7

    `THEN` on a separate line, then the signal to turn the heating on, only as a result of `temperature < 19` [1]

  8. 8

    `UNTIL` the system is switched off, correctly positioned and indented [1]

  9. 9

    All `IF`s and `ENDIF`s correctly indented [1]

3. Count-controlled loops and nested loops

Looping repeats statements. A `FOR…NEXT` loop repeats a fixed number of times, counting from a start value to an end value. `STEP` sets how much the counter changes each time round.

A nested loop is a loop inside another loop. The inner loop runs through all its repeats each time the outer loop goes round once.

``` FOR Count ← 1 TO 9 STEP 4 OUTPUT Count NEXT Count ```

Worked example 13 marks

Write pseudocode using a `FOR…NEXT` loop to output the numbers 2, 4, 6, 8 and 10.

  1. 1

    ``` FOR Count ← 2 TO 10 STEP 2 OUTPUT Count NEXT Count ```

  2. 2

    The loop runs from 2 to 10 using `STEP 2` [1]

  3. 3

    `OUTPUT Count` is inside the loop [1]

  4. 4

    The loop is closed with `NEXT Count` [1]

4. Condition-controlled loops

A `WHILE…ENDWHILE` loop is a pre-condition loop: the condition is tested before the statements run, so the loop may not happen at all. A `REPEAT…UNTIL` loop is a post-condition loop: the condition is tested after the statements run, so it is guaranteed to happen.

A `REPEAT…UNTIL` loop ends when a condition is met. A `WHILE…ENDWHILE` loop ends when its condition is not met.

In a `REPEAT…UNTIL` loop the `UNTIL` comes after the statements it repeats.

Examiner insight

“Misinterpreted the question with some candidates rewriting the code with a WHILE…… ENDWHILE loop.” “Where responses did use appropriate pseudocode included a REPEAT…UNTIL loop and incorporated the need for the algorithm to work for different sets of numbers they often achieved full credit.”

Worked example 12 marks

A student has an algorithm that uses a REPEAT…UNTIL loop. The student wishes to change the algorithm so that it uses a WHILE…ENDWHILE loop.

Contrast a REPEAT…UNTIL loop with a WHILE…ENDWHILE loop.

  1. 1

    A `WHILE…ENDWHILE` loop is a pre-condition loop: the condition is tested before the statements run [1]

  2. 2

    A `REPEAT…UNTIL` loop is a post-condition loop: the condition is tested after the statements run, so it is guaranteed to happen [1]

5. Procedures

A procedure is a named group of statements. It is declared once with `PROCEDURE`, ended with `ENDPROCEDURE`, and run each time it is called with `CALL`.

A procedure can be called from inside a loop, so the same statements are not written out again. When a question gives a procedure, use it.

``` PROCEDURE ShowLine OUTPUT "-----" ENDPROCEDURE CALL ShowLine ```

Examiner insight

“Typically, candidates knew to declare and end the procedure, and many used suitable variable names within their work.” “Despite being told that both the BeforeTax () and the procedure created in part (a) were part of the calculation, very few candidates attempted to use these to answer the question, with many candidates attempting to recreate the whole calculation from scratch.”

Worked example 14 marks

A program prints a customer's name between two lines of dashes. Write a procedure `DrawLine` that outputs "-----", and the main program that inputs a name and uses the procedure.

  1. 1

    ``` PROCEDURE DrawLine OUTPUT "-----" ENDPROCEDURE INPUT Name CALL DrawLine OUTPUT Name CALL DrawLine ```

  2. 2

    The procedure is declared with `PROCEDURE` and ended with `ENDPROCEDURE` [1]

  3. 3

    The procedure outputs the dashes [1]

  4. 4

    The name is input and output [1]

  5. 5

    `CALL DrawLine` is used before and after the name, not the statements written out again [1]

6. Counters, totals, largest and smallest

A counter goes up by 1 each time round a loop: `Count ← Count + 1`. A total adds each value input: `Total ← Total + Number`. The variable being changed is on the left of `←`.

To find the largest and smallest values, set both to the first number input. It is the only number entered at that stage, so there is no comparison to be made. Then compare each later number with each variable in turn.

The first number is read before the loop, so the loop runs one time fewer: five numbers use a count that starts at 0 and `WHILE Count < 4`.

Examiner insight

“Confusing variables, so that the variable smallest, identified in the given section of code, was not used as the value holder for the smallest value.” “Candidates also struggled with outputting two values.”

Worked example 14 marks

An algorithm asks the user to input five numbers, one at a time. After the fifth, it displays the largest and the smallest number. These lines come before the loop.

``` 50 INPUT Number 60 Largest_Number ← Number 70 Smallest_Number ← Number ```

Explain why `Largest_Number` (line 60) and `Smallest_Number` (line 70) are both set to the same value collected in line 50.

  1. 1

    It is the only number that has been entered at that stage, so there is no comparison to be made [1]

  2. 2

    It must be both the largest and the smallest number so far [1]

  3. 3

    This ensures that the highest and lowest values recorded are valid [1]

  4. 4

    It overwrites the values set earlier with a value inputted by the user [1]

7. Finding and fixing mistakes

To edit a given algorithm, identify the mistake itself, not just its line number, and then give the correction.

Check four places: the starting values (incorrect initial value), the direction of `←`, the statement that changes a counter or total (incorrect order), and what the loop condition compares with.

When asked what the code as written would do, say what it would achieve. Stating that it would not work is not sufficient.

Examiner insight

“Identified the line number rather than identifying the mistake.” “Followed the instructions in the question and identified the mistake before then correcting it.”

Worked example 16 marks

A student has created an algorithm to calculate the average of a set of numbers. The user says how many numbers they will enter, then enters that many numbers. The algorithm totals them and divides the total by how many numbers the user said they will enter.

``` 10 total ← 10 20 PRINT "Input number of repeats" 30 INPUT n 40 count → 1 50 REPEAT 60 count ← + 1 70 PRINT "Input number ", count 80 INPUT number 90 total + number ← total 100 UNTIL count = number 110 average ← total / n 120 PRINT average ```

State three mistakes in this algorithm and suggest how each can be corrected.

  1. 1

    Line 10 `total ← 10`: incorrect initial value; it should be `total ← 0` [2]

  2. 2

    Line 60 `count ← + 1`: incorrect statement for the increment; it should be `count ← count + 1` [2]

  3. 3

    Line 90 `total + number ← total`: incorrect order, so the wrong variable is set; it should be `total ← total + number` [2]

  4. 4

    Also credited: line 40 has the arrow the wrong way and `count` should start at 0; line 100 should compare with `n`, as `UNTIL count = n`.

8. Writing a full algorithm

Build the algorithm in the order the question gives. Initialise the counter before the loop, put the `INPUT` inside the loop, decide with `IF…ENDIF`, and add 1 to the counter inside the loop.

Place `UNTIL` at the end of a `REPEAT` loop with its condition, such as `UNTIL Count = 20`. Every `OUTPUT` must match its own criterion.

Write the text of an output in speech marks and separate it from a variable with a comma.

Examiner insight

“Not using speech marks and commas within the outputs, so that variables and text could not be separated.” “Placing items in the wrong order, so, for example, the OUTPUT not being the final statement.”

Worked example 18 marks

A teacher is writing a program to process test marks for a class.

For marks above 70, the program will output "You have achieved a Distinction". For marks from 55 to 70, it will output "You have achieved a Merit". For marks from 40 to 54, it will output "You have achieved a Pass". For marks below 40, it will output "You have not reached the Pass mark".

Write a pseudocode algorithm using REPEAT...UNTIL which allows the teacher to input 20 test marks and outputs the correct message for each inputted mark.

  1. 1

    ``` Count ← 0 REPEAT INPUT Mark IF Mark > 70 THEN OUTPUT "You have achieved a Distinction" ENDIF IF Mark >= 55 AND Mark <= 70 THEN OUTPUT "You have achieved a Merit" ENDIF IF Mark >= 40 AND Mark <= 54 THEN OUTPUT "You have achieved a Pass" ENDIF IF Mark < 40 THEN OUTPUT "You have not reached the Pass mark" ENDIF Count ← Count + 1 UNTIL Count = 20 ```

  2. 2

    `Count` is initialised before the mark is input [1]

  3. 3

    `REPEAT` is placed before the `INPUT` [1]

  4. 4

    `INPUT Mark` is inside the loop [1]

  5. 5

    Correct criterion for Distinction, or for the fail message [1]

  6. 6

    Correct criterion for Merit, or for Pass [1]

  7. 7

    `Count` is increased by 1 inside the loop [1]

  8. 8

    `UNTIL Count = 20` at the end of the loop [1]

  9. 9

    All the `OUTPUT`s match their criteria [1]

Recap

  • `INPUT` takes a value in, `OUTPUT` sends one out, `←` stores a value, `=` compares.
  • Close every `IF` with its own `ENDIF`, with `THEN` on a separate line and the inside indented.
  • `CASE…ENDCASE` chooses between several values of one variable.
  • `FOR…NEXT` repeats a fixed number of times, and `STEP` sets the change. A nested loop runs fully for each pass of the outer loop.
  • `WHILE…ENDWHILE` tests the condition before the statements (pre-condition). `REPEAT…UNTIL` tests it after, so it is guaranteed to happen.
  • A procedure is declared with `PROCEDURE`, ended with `ENDPROCEDURE` and run with `CALL`.
  • Set largest and smallest to the first number input. Add 1 to a counter inside the loop.
  • Name the mistake, not the line number, and give the correction.

End-of-chapter exercise

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

  1. Write one line of pseudocode that stores the number 5 in a variable called `Total`, and a second line that outputs it.2 marks
    Answer
    1. `Total ← 5` [1].
    2. `OUTPUT Total` [1].

    Trap: `Total = 5` is a comparison, not a store.

  2. State the output of this algorithm. ``` X ← 4 Y ← X * 3 + 2 X ← Y - X OUTPUT X, Y ```3 marks
    Answer
    1. Y = 4 × 3 + 2 = 14, with the multiplication first.
    2. X = 14 - 4 = 10.
    3. Output: 10 and 14.

    Trap: `Y` was worked out when `X` was 4. Changing `X` afterwards does not change `Y`.

  3. This algorithm is run. ``` INPUT Score IF Score >= 50 THEN OUTPUT "Pass" ELSE OUTPUT "Fail" ENDIF ``` State the output when 50 is input, when 49 is input, and when 50 is input if `>=` is changed to `>`.3 marks
    Answer
    1. 50 gives "Pass" [1].
    2. 49 gives "Fail" [1].
    3. With `>`, 50 gives "Fail" [1].

    Trap: `>=` includes 50 itself. `>` does not.

  4. State the output of this algorithm. ``` Total ← 0 FOR Count ← 2 TO 11 STEP 3 Total ← Total + Count NEXT Count OUTPUT Total ```3 marks
    Answer
    1. `Count` takes the values 2, 5, 8 and 11.
    2. 2 + 5 + 8 + 11 = 26.
    3. Output: 26.

    Trap: The end value 11 is reached exactly, so it is included.

  5. State the output of each algorithm. ``` Number ← 10 WHILE Number < 5 OUTPUT Number Number ← Number + 1 ENDWHILE ``` ``` Number ← 10 REPEAT OUTPUT Number Number ← Number + 1 UNTIL Number >= 5 ```3 marks
    Answer
    1. The `WHILE` algorithm outputs nothing: 10 < 5 is false before the loop starts [1].
    2. The `REPEAT` algorithm outputs 10 [1].
    3. `Number` becomes 11, the condition 11 ≥ 5 is met, and the loop ends [1].

    Trap: A `REPEAT…UNTIL` loop tests its condition after the statements, so it always runs once.

  6. State how many times `OUTPUT` runs, and the values output. ``` FOR Row ← 1 TO 3 FOR Col ← 1 TO 2 OUTPUT Row * Col NEXT Col NEXT Row ```3 marks
    Answer
    1. The inner loop runs twice for each of the 3 rows, so `OUTPUT` runs 3 × 2 = 6 times [1].
    2. Row 1 gives 1 and 2, row 2 gives 2 and 4, row 3 gives 3 and 6 [1].
    3. Output: 1, 2, 2, 4, 3, 6 [1].

    Trap: The inner loop starts again for every pass of the outer loop. Adding 3 + 2 = 5 is wrong.

  7. This algorithm should output the average of five numbers. ``` 10 Total ← 0 20 Count ← 0 30 WHILE Count < 5 40 INPUT Number 50 Total ← Number 60 Count ← Count + 1 70 ENDWHILE 80 Average ← Total / Count 90 OUTPUT Average ``` The numbers 2, 4, 6, 8 and 10 are input. Identify the mistake, state what the code as written outputs, and correct the mistake.4 marks
    Answer
    1. Mistake: `Total ← Number` replaces the total with the latest number instead of adding it [1].
    2. As written, `Total` ends as 10, so the output is 10 ÷ 5 = 2 [1].
    3. Correction: `Total ← Total + Number` [1].
    4. Then `Total` is 30 and the output is 30 ÷ 5 = 6 [1].

    Trap: Answering "line 50 is wrong" gives the line number, not the mistake.

  8. A shop pays an hourly rate by job code: code 1 pays 8, code 2 pays 10 and code 3 pays 12. Write a pseudocode algorithm that inputs a job code and the hours worked, uses `CASE…ENDCASE` to set the rate, and outputs the pay.5 marks
    Answer
    1. ``` INPUT Code INPUT Hours CASE OF Code 1 : Rate ← 8 2 : Rate ← 10 3 : Rate ← 12 ENDCASE Pay ← Rate * Hours OUTPUT Pay ```
    2. `INPUT` for the code and for the hours [1].
    3. `CASE OF Code` [1].
    4. Each of the three rates set in its own case [1].
    5. `ENDCASE`, then `Pay ← Rate * Hours` [1], and `OUTPUT Pay` [1].

    Trap: Leaving out the calculation of the pay, or the `OUTPUT` of it.

  9. Write a procedure called `ShowSquare` that outputs the value of `Number` multiplied by itself. Then write a `FOR…NEXT` loop that sets `Number` to each of 1 to 5 and calls the procedure.5 marks
    Answer
    1. ``` PROCEDURE ShowSquare OUTPUT Number * Number ENDPROCEDURE FOR Number ← 1 TO 5 CALL ShowSquare NEXT Number ```
    2. Declared with `PROCEDURE ShowSquare` [1].
    3. The body outputs `Number * Number` [1].
    4. Ended with `ENDPROCEDURE` [1].
    5. `FOR Number ← 1 TO 5` ending with `NEXT Number` [1].
    6. `CALL ShowSquare` inside the loop [1].

    Trap: Writing the output statement again inside the loop instead of calling the procedure.

  10. A weather station records the temperature at noon on 7 days. Temperatures can be below 0. Write a pseudocode algorithm using `WHILE…ENDWHILE` that inputs the 7 temperatures and outputs the highest, the lowest and the mean.8 marks
    Answer
    1. ``` INPUT Temp Highest ← Temp Lowest ← Temp Total ← Temp Count ← 1 WHILE Count < 7 INPUT Temp IF Temp > Highest THEN Highest ← Temp ENDIF IF Temp < Lowest THEN Lowest ← Temp ENDIF Total ← Total + Temp Count ← Count + 1 ENDWHILE Mean ← Total / 7 OUTPUT "Highest: ", Highest OUTPUT "Lowest: ", Lowest OUTPUT "Mean: ", Mean ```
    2. First temperature input before the loop, and `Highest` and `Lowest` set to it [1].
    3. `Total` starts as that temperature and `Count` starts at 1 [1].
    4. `WHILE Count < 7` with `ENDWHILE`, and `INPUT` inside [1].
    5. `IF Temp > Highest` with `ENDIF` [1].
    6. `IF Temp < Lowest` with `ENDIF` [1].
    7. `Total` and `Count` both updated inside the loop [1].
    8. `Mean ← Total / 7` after the loop [1].
    9. Each of the three values output separately, with text and a comma [1].

    Trap: Starting `Highest` at 0 fails when every temperature is below 0.

Say this, not that

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

Loses the markGets the mark
Line 10 is wrong.Incorrect initial value: `total` should be set to 0.
`INPUT X and Y``INPUT X`, then `INPUT Y`: one statement for each variable.
`OUTPUT smallest and largest``OUTPUT "Smallest: ", Smallest`, then a separate `OUTPUT` for `Largest`.
Rewriting the code with a `WHILE` loop, when asked to contrast the loops.A `WHILE…ENDWHILE` loop is a pre-condition loop and a `REPEAT…UNTIL` loop is a post-condition loop.

Frequently asked questions

What is the difference between a WHILE loop and a REPEAT loop in pseudocode?
A WHILE...ENDWHILE loop is a pre-condition loop: the condition is tested before the statements run, so the loop may not happen at all. A REPEAT...UNTIL loop is a post-condition loop: the condition is tested after the statements run, so it is guaranteed to happen. A REPEAT...UNTIL loop ends when a condition is met, while a WHILE...ENDWHILE loop ends when its condition is not met.
How do you write a FOR loop that counts in steps in pseudocode?
Use FOR...NEXT. A loop such as FOR Count ← 2 TO 10 STEP 2 repeats a fixed number of times, counting from the start value to the end value. STEP sets how much the counter changes each time round, and the loop is closed with NEXT Count. A nested loop is a loop inside another loop, and the inner loop runs through all its repeats each time the outer loop goes round once.
How do you find the largest and smallest numbers in a list using pseudocode?
Set both the largest and the smallest variables to the first number input. It is the only number entered at that stage, so there is no comparison to be made. Then compare each later number with each variable in turn. Read the first number before the loop, so the loop runs one time fewer.
What is the difference between ← and = in pseudocode?
← stores a value: the variable goes on the left and the value on the right, as in Count ← 0. = is a comparison, used in a condition such as UNTIL count = n. Writing Total = 5 instead of Total ← 5 is a common error, because it compares rather than stores.
Why must every IF statement in pseudocode be closed with ENDIF?
Failure to close IF statements with ENDIF is the most damaging mistake students make with selection. Each IF needs its own ENDIF in capitals, with THEN on a separate line and the lines inside indented. An action is credited only when it happens as a consequence of its own condition, so each IF holds only the actions for that condition.
How do you find and correct a mistake in a given pseudocode algorithm?
Identify the mistake itself, not just its line number, and then give the correction. Check the starting values, the direction of ←, the statement that changes a counter or total, and what the loop condition compares with. For example, total ← 10 is an incorrect initial value, and it should be total ← 0.

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