x

Revision notes for Edexcel GCSE Computer Science Constructs for solving problems. Open the guide for explanations and worked examples. Written against the Edexcel GCSE Computer Science (1CP2) specification, so the content matches what's examinable rather than general Computer Science background.

Constructs for solving problems

What you'll learn

  • How algorithms use input, processing and output to solve problems.
  • How to recognise and write sequence, selection and repetition.
  • The difference between count-controlled loops, condition-controlled loops and iteration over a data structure.
  • How the same algorithm can be shown as a flowchart, pseudocode or Python 3 program code.

The big picture: what is an algorithm?

An algorithm is a clear, finite set of steps for solving a problem. In GCSE Computer Science, you need to be able to follow algorithms and write algorithms.

Definition

Algorithm

An algorithm is a step-by-step method for solving a problem, written so that each step is unambiguous and can be carried out.

Algorithms often use variables. A variable is a named storage location for a value, such as score, total or username. Algorithms also use conditions, which are tests that are either true or false, such as score >= 50.

Input, processing and output

Most useful algorithms follow an IPO pattern:

  • Input: data is received, such as a typed value, a sensor reading or a file value.
  • Processing: the algorithm changes, compares or calculates using the data.
  • Output: the result is displayed, stored, printed or sent somewhere.
Key Idea

Input, processing, output

A good algorithm is usually planned by asking: What data goes in? What must be done to it? What result must come out?

Processing can include arithmetic operations such as addition, subtraction, multiplication, division, modulus, integer division and exponentiation. It can also include comparisons such as >, <, >=, <=, = and !=, and logical operators such as AND, OR and NOT.

Example

Designing input, processing and output

A program must ask for two test marks, calculate the average, and output whether the student has passed. The pass mark is 50.

  1. Identify the inputs: the algorithm needs mark1 and mark2.
  2. Identify the processing: calculate average from the two marks, then compare average >= 50.
  3. Identify the outputs: display the average and either "Pass" or "Try again".
  4. Choose the constructs: the calculation is sequence, and the pass/fail decision needs selection.

Ways to represent an algorithm

You may see or write algorithms in three main ways.

Flowcharts

A flowchart is a diagram showing the steps in an algorithm using standard symbols. Arrows show the order of execution.

Common GCSE flowchart symbols include:

  • Oval: start or end.
  • Parallelogram: input or output.
  • Rectangle: process, such as a calculation.
  • Diamond: decision, usually with yes/no or true/false branches.

The flowchart below shows input, a decision, and two possible outputs.

Flowchart showing input mark, selection based on mark greater than or equal to 50, and pass or try again outputs

Pseudocode

Pseudocode is an informal, English-like way of writing an algorithm. It is not a real programming language, so the exact layout can vary. The aim is to make the logic clear.

Example pseudocode:

INPUT mark
IF mark >= 50 THEN
    OUTPUT "Pass"
ELSE
    OUTPUT "Try again"
ENDIF

Program code

Program code is written in a real programming language. For Edexcel GCSE Paper 2, this means Python 3.

The same algorithm in Python 3:

mark = int(input("Enter mark: "))

if mark >= 50:
    print("Pass")
else:
    print("Try again")
Common Mistake

Assignment and comparison

In Python, = assigns a value, but == checks whether two values are equal. For example, score = 50 stores 50, while score == 50 is a condition.

The control constructs

A construct is a building block used to control the order in which instructions run.

Definition

Control construct

A control construct controls the flow of an algorithm: whether instructions run in order, branch in different directions, or repeat.

The three main constructs are sequence, selection and repetition.

Sequence

Sequence means instructions are carried out one after another, in the order written.

For example:

INPUT width
INPUT height
area = width * height
OUTPUT area

The order matters. You cannot output area before calculating it, and you cannot calculate it before receiving width and height.

Common Mistake

Using a value before it exists

If an algorithm uses a variable before it has been input or calculated, the algorithm is not logically complete. Make sure every variable has a value before it is used.

Selection

Selection means the algorithm chooses between different paths. It uses a condition, which evaluates to true or false.

In Python, selection is usually written with if, elif and else.

temperature = int(input("Enter temperature: "))

if temperature < 0:
    print("Freezing")
elif temperature <= 20:
    print("Cool or mild")
else:
    print("Warm")
Definition

Selection

Selection is a construct that runs different instructions depending on whether a condition is true or false.

Example

Following a selection

For the algorithm above, suppose the input is 15.

  1. Compare the input with the first condition: 15 < 0 is false, so the "Freezing" branch is skipped.
  2. Test the next condition: 15 <= 20 is true, so the algorithm chooses that branch.
  3. The output is "Cool or mild", and the final else branch is not run.

Logical operators can make conditions more precise:

  • AND: both conditions must be true.
  • OR: at least one condition must be true.
  • NOT: reverses true and false.

For example, age >= 13 AND age <= 19 checks whether an age is in the teenage range.

Repetition

Repetition means running instructions more than once. Repetition is also called looping.

There are three GCSE patterns you should recognise:

Comparison diagram of count-controlled repetition, condition-controlled repetition, and iteration over every item in a list

Count-controlled repetition

A count-controlled loop repeats a known number of times.

In Python:

for counter in range(5):
    print("Hello")

This prints "Hello" 5 times. The values of counter are 0, 1, 2, 3 and 4.

Condition-controlled repetition

A condition-controlled loop repeats while a condition is true. Use this when you do not know in advance how many times the loop will run.

password = ""

while password != "letmein":
    password = input("Enter password: ")

print("Access granted")

This keeps asking until the user enters the correct password.

Common Mistake

Condition-controlled loops must stop

A while loop needs something inside the loop that can eventually make the condition false. Otherwise, the loop may run forever.

Iteration over every item in a data structure

A data structure is a way of organising multiple values, such as a list. Iteration means visiting each item in turn.

scores = [12, 8, 15, 10]
total = 0

for score in scores:
    total = total + score

print(total)

Here, the loop processes each item in the list scores.

Example

Choosing the right loop

Choose the best repetition construct for each situation: printing 10 tickets, asking until a valid PIN is entered, and adding every price in a list.

  1. Printing 10 tickets uses count-controlled repetition, because the number of repeats is known before the loop starts.
  2. Asking until a valid PIN is entered uses condition-controlled repetition, because the number of attempts is not known in advance.
  3. Adding every price in a list uses iteration over a data structure, because the algorithm must visit each stored item once.
Common Mistake

Off-by-one loops

In Python, range(5) repeats 5 times but counts from 0 to 4. If you want the numbers 1 to 5, use range(1, 6).

Following algorithms by tracing

To trace an algorithm, you work through it step by step and record how variables change. A trace table is often the clearest way to do this.

Definition

Trace table

A trace table records the values of variables after key steps of an algorithm, helping you follow what the algorithm does.

Example algorithm:

numbers = [3, 5, 2]
total = 0

for number in numbers:
    if number >= 4:
        total = total + number

print(total)
Example

Tracing selection inside iteration

Trace the algorithm above.

  1. Start with total set to 0 before the loop begins.
  2. The first item is 3. The condition 3 >= 4 is false, so total stays 0.
  3. The second item is 5. The condition 5 >= 4 is true, so total becomes 0 + 5, which is 5.
  4. The third item is 2. The condition 2 >= 4 is false, so total stays 5.
  5. After every item has been processed, the algorithm outputs 5.

A matching trace table would look like this:

Current numberCondition number >= 4total after this item
3False0
5True5
2False5

Writing algorithms to solve problems

When you write your own algorithm, build it in layers:

  1. State the required output first, so you know the goal.
  2. Decide what input is needed to produce that output.
  3. Write the simple sequence of calculations or assignments.
  4. Add selection wherever the algorithm must choose between paths.
  5. Add repetition wherever the same action must happen multiple times.
  6. Trace your algorithm with small test data to check it behaves sensibly.
Tip

A useful planning sentence

Try saying: “Input these values, process them by doing these steps, and output this result.” Then decide where the algorithm needs decisions or loops.

Exam technique

In the exam

  1. When asked to follow an algorithm, trace one line at a time and update variable values carefully; do not jump to the output too early.
  2. When asked to write an algorithm, include input, processing and output unless the question clearly gives one of them already.
  3. Match the construct to the problem: fixed number of repeats means count-controlled, unknown number of repeats means condition-controlled, and every item in a list means iteration.
Self review

Check yourself

  • What is the difference between selection and repetition?
  • When would you use a while loop instead of a for loop?
  • How could you trace an algorithm that adds up every number in a list?
You've reached the end

Test yourself on this topic, or move on to the next guide.

FlashcardsSelf-test with active recall
Variables, constants and data structuresUp next

How was this guide?

Constructs for solving problems Revision Guide

  1. GCSE
  2. /Computer Science
  3. /Constructs for solving problems