GCSE · Computer Science · AQA · Spec 8525

Subtraction (arithmetic operator)

14 °C at 9 am, 9 °C by 3 pm. Did the temperature change by -5 or by 5? In code, it depends on which number you write first.

Computer Science · Algorithms

Trace table — dry run the code

A weather app stores two temperature readings and works out the change. Before each Next, predict what the line will store. Line 4 is the one to watch.

1earlier ← 14
2later ← 9
3change ← later - earlier
4swapped ← earlier - later
5later ← later - 2
6OUTPUT change
earlier
later
change
swapped
Output
Press Start to run the first line.
·

Ready when you are — step through one line at a time.

Predict, then check

Two minus signs in one line. Which subtraction happens first?

A program runs this line: left ← 20 - 5 - 3. What number is stored in left?

Which idea is yours?

A line that subtracts from itself

A game stores how many lives a player has left. Every time the player is hit, this line runs: lives ← lives - 1

Which is closest to what you think this line does?
How sure are you?

Your turn to write it

Finish the tuck-shop program

A tuck-shop program sells one snack. Amounts are in pence. Choose the missing lines so it stores the change owed and takes the snack off the stock count.

  1. price ← 120
  2. paid ← 200
  3. missing step
Which line is step 3?

WHAT YOU'VE LEARNED

A quick recap of today's lesson.

The same two numbers can give a rise or a fall. It all depends on which one you write first.

What you need to know

  • The subtraction operator (-) takes the second operand away from the first: a - b.
  • Order matters. a - b and b - a have the same size but opposite signs, e.g. 9 - 14 = -5 and 14 - 9 = 5.
  • A chain such as 20 - 5 - 3 is worked left to right (answer 12). Brackets are worked first: 20 - (5 - 3) = 18.
  • The result must be stored (assigned to a variable) if the program needs it later.
  • x ← x - 1 updates x: the current value is used, then the answer replaces it.

The big picture

The subtraction operator (-) takes the second value away from the first. The values either side of it are called operands, and their order matters: 9 - 14 is -5, but 14 - 9 is 5. The answer has the same size and the opposite sign. A chain like 20 - 5 - 3 is worked left to right, and brackets change that order. The result only survives if it is stored, and a line like later ← later - 2 updates a variable using its own current value.

Key points

1Subtraction in code works on stored values and gives a result the program can store or output.
2Swapping the operands flips the sign of the answer, so pick the order that gives the meaning you want.
3A negative result is a normal answer. Its sign tells you the direction, such as a fall rather than a rise.
4Several subtractions in one line are worked left to right unless brackets say otherwise.
5A stored result doesn't change by itself. If a value used in the subtraction changes later, the result stays the same until the subtraction runs again.

Worked example

Problem

A car park program starts with spaces ← 150. Then these lines run in order: spaces ← spaces - 40, then spaces ← spaces - 25, then taken ← 150 - spaces. What values are stored in spaces and taken at the end?

⚠ Watch out

Writing the operands the wrong way round, e.g. earlier - later when you meant later - earlier. The answer has the right size and the wrong sign, so it's easy to miss. Decide what the answer should mean first, then choose the order.

🧠

Memory hook

First minus second, exactly as written. Swap them and the sign flips.

✓

Check yourself

These lines run in order: a ← 10, b ← 25, a ← a - b. What does a hold now? (Answer: -15. 10 - 25 = -15, and that replaces the 10.)

Flashcards

(7)
What does the subtraction operator (-) do in a program?
It takes the second operand away from the first and gives a result the program can store or output.
Does a - b give the same answer as b - a?
No. Same size, opposite sign. For example 3 - 8 = -5, but 8 - 3 = 5.
How is 30 - 10 - 4 worked out?
Left to right: 30 - 10 = 20, then 20 - 4 = 16.
What do the brackets do in 30 - (10 - 4)?
The bracket is worked out first: 10 - 4 = 6, then 30 - 6 = 24.
What does score ← score - 5 do?
It takes the current value of score, subtracts 5, and stores the answer back in score, replacing the old value.
Why should the result of a subtraction be assigned to a variable?
So the program keeps it. For example, change ← paid - price stores the difference in change, ready for later lines to use.
What does a negative result from later - earlier tell you?
That the value went down: the later reading is smaller than the earlier one.

Tap any card to flip it, or use Study as deck to go through them one at a time. In the full lesson these run as a spaced-repetition deck — you rate each card Hard, Good or Easy and the tricky ones keep coming back until they stick.

Learning with Lightbulb is opening soon

You can use this lesson now. Join the waitlist and we'll let you know when the full Lightbulb experience is ready.

Keep me posted

More AQA GCSE Computer Science topics

See the full AQA Computer Science curriculum →

How this lesson was checked. This AQA GCSE Computer Science (specification 8525)lesson was published through Lightbulb Learning's human-designed editorial process — the educational standards, accuracy rules and publication checks it must pass were authored and approved by Philip Halpin. It passed subject-specific assessment, automated educational checks and technical publication verification before going live (publication checks completed 30 September 2026). Published pages are monitored, human spot-checking is ongoing across the lesson library, and anything found wrong is corrected or withdrawn. How our lessons are made and checked. Spotted a mistake? Email hello@lightbulblearning.co and we'll review it.