Variables and Constants
Practise Variables and Constants for GCSE Computer Science with this free worksheet and full mark scheme — Foundation and Higher exam-style questions with worked answers for AQA GCSE Computer Science (8525). A variable stores a value that can change while a program runs; a constant cannot be changed.
Free downloads
These worksheets and mark schemes are original, written for Virtus Academy and checked against the current AQA specification. Every worksheet comes with a full mark scheme.
Topic overview
A variable is a named location in memory whose value can change while a program runs. A constant is a named value that cannot change once set.
Using a constant for a fixed value such as VAT_RATE has two benefits. If the value ever needs changing, it is changed in one place rather than hunted through the code. And a named constant makes the program readable — VAT_RATE explains itself where 0.2 does not.
Variables must be given meaningful names. A variable called totalPrice tells a reader what it holds; one called x does not. This matters for maintainability, because most code is read far more often than it is written, frequently by someone other than its author.
Revision notes
Variables and constants
A variable is a named memory location whose value can change during execution.
A constant is a named value fixed when the program is written and unchangeable while it runs. Constants are conventionally written in capitals, such as VAT_RATE or MAX_USERS.
Why use constants
If the value needs changing, it is changed in one place rather than everywhere it appears.
A named constant is self-documenting: VAT_RATE explains its purpose where the bare number 0.2 does not. And it cannot be changed by accident during execution.
Naming variables
Names should describe what the variable holds.
totalPrice, userAge and itemCount are meaningful; x, a and temp are not. Good names make a program readable and maintainable, which matters because code is read far more often than written.
Key points
- A variable's value can change during execution.
- A constant's value cannot change.
- Constants are conventionally written in capitals.
- A constant is changed in one place only.
- Named constants make code self-documenting.
- Meaningful variable names aid maintainability.
Worked examples
Example 1
State the difference between a variable and a constant. [2 marks]
Working
Example 2
Give two reasons for using a constant named VAT_RATE rather than the number 0.2 throughout a program. [2 marks]
Working
Example 3
Explain why meaningful variable names are important. [2 marks]
Working
Common mistakes
Saying a constant has no value.
It has a fixed value that cannot change.
Using single-letter variable names.
Names should describe what the variable holds.
Confusing constants with variables that happen not to change.
A constant is declared as unchangeable; the program cannot alter it.
Giving only one reason for using constants.
Maintainability and readability are both examinable.
Exam tips
- Use CAPITALS for constant names.
- Give both maintainability and readability reasons.
- Choose variable names that describe their contents.
- Say named memory location when defining a variable.
Key terms
- Variable
- A named memory location whose value can change.
- Constant
- A named value that cannot change during execution.
- Maintainability
- How easily a program can be understood and modified.
- Self-documenting
- Code whose purpose is clear from its names.
Related topics
Written and reviewed against the current AQA specification. Spotted an error? Let us know.