GCSE · Computer Science · AQA · Spec 8525

Variable declaration

Every game keeps score in a named spot in memory. Win points, and the old number is thrown out as a new one takes its place. That's a variable.

Computer Science · Algorithms

Trace table — dry run the code

Illustrative code, not any one language. Before each Next, read the line about to run and predict: what kind of statement is it, and what will total_score hold afterwards?

1constant MAX_SCORE ← 100
2integer total_score
3total_score ← 0
4total_score ← total_score + 30
5total_score ← total_score + 45
6OUTPUT MAX_SCORE - total_score
MAX_SCORE
total_score
Output
Press Start to run the first line.
·

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

Computer Science · Programming

Declaration, initialisation or later assignment?

A step-counting app uses these statements. Sort each one: is it a declaration, an initialisation or a later assignment?

Still to sort

Declaration (0)

Introduces the name, plus the data type if the language needs one. No value yet.

Where the line is: A declaration names a variable without storing anything in it. As soon as a value is stored, you're looking at an assignment.

Initialisation (0)

The first value the variable ever receives.

Where the line is: Initialisation is an assignment: the first one. It can look exactly like a later assignment. What decides it is whether the variable already held a value.

Later assignment (0)

Gives a new value to a variable that already holds one.

Where the line is: The new value replaces the old one completely. The old value isn't added to or kept anywhere.

6 of 6 still to sort.

The code is illustrative. The note under each statement tells you where it sits in its program, and that can change the answer.

Variable or constant?

VariablevsConstant

Both have an identifier. Only one of them can have its value changed while the program runs.

Focus

Can the running program change its value?

Variable

Yes. Any assignment replaces the value it holds.

Constant

No. Its value stays fixed while the program runs.

The insight

This is the difference that matters, and it's where the name comes from: a variable's value can vary.

How the program refers to it

Variable

By its identifier, e.g. total_score

Constant

By its identifier, e.g. MAX_SCORE

When it gets its value

Variable

A first value, then as many later assignments as the program needs

Constant

Its value is given when it is declared

In the trace above

Variable

total_score went 0 → 30 → 75

Constant

MAX_SCORE was 100 on every row

Computer Science · Programming

Do you always have to declare a variable?

Two programmers are arguing. One writes in a language where every variable is declared with its data type first: integer total_score, then total_score ← 0. The other writes Python, with just one line: total_score = 0.

Must a variable always be declared before it is used?
How sure are you?

Predict, then check

Both lines run exactly the same way. Cover line B with your hand first.

Line A: t ← p * q. Line B: total_cost ← price * quantity. Reading line A on its own, what can you tell about what it calculates?

WHAT YOU'VE LEARNED

A quick recap of today's lesson.

Name it, fill it, fill it again. That's what makes it a variable.

What you need to know

  • A variable is a location in memory, identified by an identifier, that stores a value which can change while the program runs.
  • Declaring a variable introduces its identifier and, in languages that require it, its data type, so memory can be set aside before it is used.
  • Assignment gives a variable a value. The first value is its initialisation; every later assignment replaces the value it held.
  • Some languages require every variable to be declared with its data type before use. Others, such as Python, create the variable the first time a value is assigned to it.
  • A constant is also referred to by an identifier, but it is declared with a value that cannot change while the program runs.
  • Give variables meaningful identifiers that describe what they store, such as total_score rather than x, so code is easier to read and maintain.

The big picture

A variable is a named location in memory that stores a value, and that value can change while the program runs. Declaring a variable introduces its identifier, plus a data type in languages that need one. Giving it its first value is initialisation, and each later assignment replaces the value it held. Some languages require a declaration first; Python creates a variable the first time a value is assigned to it. A constant also has an identifier, but its value cannot change while the program runs. Meaningful identifiers make code easier to read and maintain.

Key points

1Three kinds of statement: a declaration names the variable, initialisation gives its first value, a later assignment replaces that value.
2Same text, different job: score ← 0 is an initialisation the first time and a later assignment if score already held a value.
3Read ← as 'becomes': work out the right-hand side using the current value, then store the result in the variable.
4A new assignment overwrites the old value. It isn't added on top, and the old value isn't kept.
5Variable or constant? Ask one question: may the running program change the value?
6A good identifier says what the variable stores: tickets_bought, not t (says nothing), number (only the kind of data) or customer (something related, not the value).

Worked example

Problem

Here is some illustrative code for a game. Line 1: constant START_LIVES ← 3. Line 2: integer lives. Line 3: lives ← START_LIVES. Line 4: lives ← lives - 1. Line 5: lives ← lives + 2. For lines 2 to 5, say which kind of statement each one is, and give the value of lives after it runs.

⚠ Watch out

Calling every line that mentions a variable a 'declaration'. A declaration only introduces the name (and data type, if the language needs one). The line giving the first value is the initialisation; any later line giving a new value is a later assignment, replacing what was there.

🧠

Memory hook

Name it, fill it, refill it. Declaring labels the box, initialising puts the first value in, and every later assignment tips the old value out before the new one goes in. A constant is a box with its lid glued shut.

✓

Check yourself

Without looking back: name the three kinds of statement a variable can meet during a program, say what each one does, and explain which of the three a constant can never meet.

Flashcards

(10)
What is a variable?
A named location in memory that stores a value, and that value can change while the program runs.
What does declaring a variable do?
Introduces its identifier (and its data type, where the language requires one) so memory can be set aside before it is used.
What is initialisation?
Giving a variable its first value. It is the first assignment the variable receives.
A later assignment gives a variable a new value. What happens to the old one?
It is replaced. The new value overwrites it, and the old value is gone.
How do you work out total_score ← total_score + 30?
Work out the right-hand side using the current value of total_score, then store the result back in total_score.
What is an identifier?
The name a program uses to refer to a variable or a constant.
How is a constant different from a variable?
A constant is declared with a value that cannot change while the program runs. It has no later assignments.
Does every language need variables declared before use?
No. Some require a declaration with a data type first; others, such as Python, create the variable the first time a value is assigned.
In Python, what two jobs does total = 0 do if total doesn't exist yet?
It creates the variable total and initialises it to 0.
Why use meaningful identifiers?
A name like tickets_bought says what the variable stores, which makes code easier to read and maintain. The computer runs it the same either way.

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.