GCSE · Computer Science · AQA · Spec 8525
Assignment
In maths, x = x + 1 is impossible. In code it's one of the commonest lines there is, because in code = isn't a fact. It's an order.
Computer Science · Algorithms
Trace table — dry run the code
Run this little game program one line at a time. Before each click, say out loud what you think will change. Then check.
Ready when you are — step through one line at a time.
Spot the difference
Assignment or comparison?
Pick a line, then choose what it does.
Still to sort
Assignment: changes a stored value (0)
After the line runs, a variable holds something new.
Where the line is: If the line stores a result in a variable, it's an assignment, even when a comparison appears on its right-hand side.
Comparison: tests equality, gives True or False (0)
Asks a question about values. Changes nothing.
Where the line is: A comparison produces a Boolean (True or False). It only changes a variable if an assignment stores that Boolean.
The symbols look alike, so decide by what the line DOES. In these lines = assigns and == compares, as in Python and many other languages.
Predict, then check
Commit to an answer before you reveal it.
a holds 7 and b holds 2. To swap them, someone writes two lines: a = b then b = a. What do a and b hold afterwards?
WHAT YOU'VE LEARNED
A quick recap of today's lesson.
Work out the right-hand side, then store the answer on the left
What you need to know
- Assignment stores a value in a variable. A constant is given its value once, when it is declared, and does not change while the program runs.
- Right-hand side first: the expression on the right is worked out using the values held now, then the result is stored in the variable on the left.
- Assigning a new value overwrites the old one. The old value is lost unless it was stored somewhere else first.
- The right-hand side can be a literal (7), another variable's value (best = score), an expression (total = a + b) or the variable's own current value (count = count + 1).
- Assignment changes a stored value; a comparison tests whether two values are equal and gives True or False. Many languages write these as = and ==; lots of pseudo-code uses ← to assign and = to compare.
- Assignments run in sequence, so the value a variable holds depends on the order of the lines that have already run.
The big picture
Assignment stores a value in a variable. The right-hand side is worked out first, using the values held at that moment, and the result then replaces whatever the variable on the left held before. Because lines run in order, what a variable holds at any point depends on which assignments have already run. Assignment is different from a comparison, which only tests whether two values are equal.
Key points
Worked example
Problem
Trace this program and give the final values of x and y: x = 4, y = x * 3, x = y - x, y = y + x
⚠ Watch out
Reading = in code as 'is equal to'. It makes count = count + 1 look impossible, tempts you to write total + 1 = total, and makes a = b then b = a look like a swap. Work out the right, then store on the left.
Memory hook
Right, then left: work it out, then write it in. Read = as 'becomes'.
Check yourself
Cover the page. p holds 5. The program then runs q = p, followed by p = p * 2. What do p and q hold now, and why didn't q change when p did?
Flashcards
(12)What does an assignment statement do?
In x = y + 2, which part is dealt with first?
What happens to a variable's old value when a new value is assigned?
What is a constant, and when does it get its value?
Name four kinds of thing that can appear on the right-hand side of an assignment.
count holds 9. What does count = count + 1 do?
Assignment versus comparison: what's the difference?
Why does a = b followed by b = a fail to swap two values?
How do you swap the values of a and b safely?
Why does the order of assignment lines matter?
After best = score runs, score is changed. Does best change too?
Do all languages use = for assignment?
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 postedMore AQA GCSE Computer Science topics
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.