x

Revision notes for Edexcel GCSE Computer Science Read, write, analyse and refine programs. 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.

Read, write, analyse and refine programs

What you'll learn

  • Why Python 3 is a high-level programming language: one written closer to human language than machine code.
  • How to read programs by following variables, inputs, outputs and control structures.
  • How to write small programs from a requirement using sequence, selection and iteration.
  • How to analyse and refine programs using trace tables, testing and debugging.

The programming cycle

This topic is about being able to work with code, not just memorise definitions. In an exam or programming task, you may need to understand existing code, write new code, predict what code will do, spot errors, and improve it.

Program development cycle showing read code, write code, analyse code with a trace table, and refine code before retesting

Key Idea

The main idea

Programming is a cycle: read the code, write or change the code, analyse what it does, then refine it until it meets the requirement.

High-level programming languages

Definition

High-level programming language

A high-level programming language is a programming language whose source code — the human-written instructions — is closer to English and maths than machine code, which is the binary instruction form a processor can execute directly.

Python 3 is the high-level programming language used for Paper 2. It hides lots of low-level details, so you can write code using words like if, while, for, print and meaningful variable names.

A program is a set of instructions that a computer can follow. Syntax means the grammar rules of the language. Semantics means what the instructions actually do when they run.

The building blocks you must recognise

Before you can read or write programs confidently, you need to recognise these core ideas.

Variables and assignment

A variable is a named storage location for a value. The name is called an identifier. For example:

score = 12

This stores the value 12 in the variable score.

Common Mistake

Assignment is not comparison

In Python, = means assign a value. To compare two values, use ==. For example, score = 10 changes score, but score == 10 checks whether score is equal to 10.

Inputs, outputs and data types

Input is data entered into a program, often using input(). Output is data displayed or returned by a program, often using print().

A data type is the kind of value being stored. Common Python data types include:

  • int: whole numbers, such as 42
  • float: decimal numbers, such as 3.5
  • str: text, such as "hello"
  • bool: Boolean values, either True or False

Because input() gives a string, you often need to convert it:

age = int(input("Enter your age: "))

Operators

An operator performs an action on values.

TypePython examplesMeaning
Arithmetic+, -, *, /addition, subtraction, multiplication, division
Other arithmetic%, //, **modulus, integer division, exponentiation
Relational==, !=, <, <=, >, >=compare values
Logicaland, or, notcombine or reverse conditions

In the specification, logical operators are written as AND, OR and NOT. In Python, they are written as and, or and not.

Control structures

A control structure controls the order in which instructions run.

  • Sequence: instructions run one after another.
  • Selection: the program chooses a path using if, elif and else.
  • Iteration: the program repeats code using a loop, such as for or while.

A condition is an expression that evaluates to True or False.

Reading programs

To read a program, follow it in the order Python would run it. Keep track of variable values, conditions, loop repetitions and outputs.

A trace table is a table used to record the changing values of variables as each line of code runs.

Tip

Range sanity check

In Python, range(start, stop) includes the start value but stops before the stop value. So range(1, 4) gives 1, 2, 3.

Example

Tracing a loop and selection

For this code, suppose the user enters 8000, then 12000, then 10000.

total = 0

for day in range(1, 4):
    steps = int(input("Steps: "))
    if steps >= 10000:
        total = total + 1

print(total)
  1. total starts at 0. The loop uses range(1, 4), so it runs for day values 1, 2 and 3.
  2. On the first loop, steps is 8000. The condition steps >= 10000 is False, so total stays 0.
  3. On the second loop, steps is 12000. The condition is True, so total becomes 1.
  4. On the third loop, steps is 10000. The condition is also True because >= includes equality, so total becomes 2.
  5. The print(total) line is after the loop, so the final output is 2.

Writing programs

When writing code, start from the problem requirement. An algorithm is an ordered set of steps that solves a problem. Decomposition means breaking a larger problem into smaller parts.

A useful structure is:

  1. What inputs are needed?
  2. What processing must happen?
  3. What output is required?

This is sometimes called IPO: input, process, output.

Example

Writing an average program

Requirement: ask for five marks, calculate the average, then print Pass if the average is at least 50, otherwise print Resit.

  1. The program needs repeated input, so a for loop is suitable because the number of marks is fixed at five.
  2. The program needs an accumulator variable, such as total, to keep the running total of the marks.
  3. After the loop, calculate average = total / 5, then use selection to compare the average with 50.
  4. A suitable Python 3 program is:
total = 0

for counter in range(5):
    mark = int(input("Enter mark: "))
    total = total + mark

average = total / 5

if average >= 50:
    print("Pass")
else:
    print("Resit")
Tip

Use meaningful identifiers

Names like total, mark and average make the program easier to understand than names like x, y and z.

Analysing programs

To analyse a program, decide whether it behaves as required. You are not just checking whether the code runs — you are checking whether it gives the correct results.

Useful methods include:

  • tracing the code line by line
  • comparing actual output with expected output
  • testing normal values, boundary values and unusual values
  • checking whether each condition matches the requirement

A boundary value is a value at the edge of a decision, such as 50 in a pass mark of “at least 50”.

Example

Finding and fixing a boundary error

Requirement: a child aged 5 or under gets a free ticket. Everyone else pays £4.

Faulty code:

age = int(input("Age: "))

if age < 5:
    print("Free")
else:
    print("£4")
  1. The important boundary is age 5, because the requirement says “aged 5 or under”.
  2. Substitute age = 5 into the condition age < 5. This is False, so the program goes to the else branch.
  3. The actual output is £4, but the expected output is Free, so the condition is too strict.
  4. Refine the condition to age <= 5, so age 5 is included.

Refining programs

To refine a program means to improve it so that it better meets the requirement. Refining might involve fixing errors, improving readability, removing repeated code, or making the program more robust.

There are three common error types:

  • A syntax error breaks the grammar rules of Python, such as missing a colon after if.
  • A runtime error happens while the program is running, such as dividing by zero.
  • A logic error means the program runs but gives the wrong result.

Debugging means finding and fixing errors.

Example

Refining an average program

Requirement: ask how many scores will be entered, then calculate the average. There must be at least one score.

Faulty code:

number_of_scores = int(input("How many scores? "))
total = 0

for counter in range(number_of_scores):
    score = int(input("Score: "))
    total = score

average = total / number_of_scores
print("Average:", average)
  1. Trace the update to total. If the scores are 10, 20, 30, the variable becomes 10, then 20, then 30. It is being overwritten each time.
  2. The requirement needs the sum of all scores, so the update should accumulate: total = total + score.
  3. Check the runtime risk. If number_of_scores is 0, the line average = total / number_of_scores will divide by zero.
  4. Refine the program by adding validation and fixing the accumulator:
number_of_scores = int(input("How many scores? "))

while number_of_scores < 1:
    number_of_scores = int(input("Enter at least 1 score: "))

total = 0

for counter in range(number_of_scores):
    score = int(input("Score: "))
    total = total + score

average = total / number_of_scores
print("Average:", average)
Key Idea

Refine, then retest

After changing code, test it again. A fix is only successful if the refined program now matches the requirement.

Exam technique

In the exam

  1. For “read” or “analyse” questions, trace the code exactly as Python would run it; do not rely on a quick guess.
  2. For “write” questions, identify the inputs, processing and outputs before choosing variables, loops and conditions.
  3. For “refine” questions, state the specific error or weakness, change the code, then mentally retest it with suitable data.
Self review

Check yourself

  • What is the difference between a syntax error, a runtime error and a logic error?
  • Why does range(1, 4) run three times, not four?
  • How could a trace table help you find a wrong output in a loop?
You've reached the end

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

FlashcardsSelf-test with active recall
Converting algorithms into programsUp next

How was this guide?

Read, write, analyse and refine programs Revision Guide

  1. GCSE
  2. /Computer Science
  3. /Read, write, analyse and refine programs