Programming Concepts · Cambridge syllabus 8.1 · Lesson 1 of 4
Variables and Constants
Declare variables with names and suitable types, assign initial values before use, and update them deliberately. Constants express values that should not change, reducing repeated literal values and making later changes easier.
By the end, you can
- Declare and use variables.
- Assign values to variables.
- Update variables during program execution.
- Use meaningful variable names.
- Understand constants.
- Use constants for values that should not change.
- Explain why constants can improve maintainability.
Variables
A variable in a program is a named piece of memory holding a value that can change while the program runs. Declaring one reserves the space and fixes what kind of data it holds: DECLARE Score : INTEGER in pseudocode, Dim Score As Integer in Visual Basic, and in Python simply the first assignment, since the type comes from the value.
Assignment stores a value and discards whatever was there. Score ← 0 sets it to zero; Score ← Score + 10 reads the current value, adds ten and stores the result back. That last line is an instruction, not an equation, which is why it makes perfect sense in a program and none in algebra.
A variable must be given a value before it is read. Using one that has never been assigned gives either an error or whatever happened to be in that memory, and the second is worse because the program appears to work. Initialising a total to zero and a flag to FALSE before a loop is not a formality — it is the reason the loop produces the right answer on the second run as well as the first.
Where a variable is declared decides where it can be used. One declared inside a procedure is local to it: it exists only while that procedure runs and cannot be seen from outside, so two procedures can each have a count without interfering. One declared in the main program is global and visible everywhere, which sounds convenient and causes trouble, because any part of the program can change it and finding which part did is hard.
Names should describe the content, and 0478 pseudocode writes them in Pascal case, each word starting with a capital letter. TotalScore, PupilCount and IsValid explain themselves; A, N and Flag2 do not, and a program of thirty such names cannot be maintained by anybody, including the person who wrote it.
Worth remembering
- A variable is named memory whose value changes; declaring it fixes its type.
- Score ← Score + 10 reads, adds and stores back; it is an instruction, not an equation.
- Initialise before use, and prefer local variables to global ones.
Constants
A constant is a named value that is fixed when the program is written and cannot be changed while it runs. CONSTANT VatRate ← 0.20 declares one: the keyword CONSTANT, the identifier, an arrow and a literal value. Only a literal can be used, never a variable, another constant or an expression, and any later attempt to assign to it is an error.
That last point is the practical difference from a variable initialised once and never touched. The compiler enforces the promise, so a value that must not change cannot be changed by accident anywhere in a program of ten thousand lines.
Constants make a program maintainable. A tax rate, a maximum class size, a discount threshold or the number of columns in a grid may appear in dozens of calculations. Named once, a change means editing one line; written as a literal number each time, it means finding every occurrence and hoping none was missed — and worse, distinguishing the 30 that is the class size from the 30 that is the number of days.
They also document the code. Area ← Pi * Radius ^ 2 reads as mathematics; Area ← 3.14159 * Radius ^ 2 reads as a puzzle. MaxAttempts explains why a loop stops where it does.
The rule of thumb is simple: any number in the code other than 0 or 1 that has a meaning should probably be a named constant.
Worth remembering
- A constant is fixed when the program is written; assigning to it is an error.
- One named value means one line to change when it changes.
- A named constant explains what a bare number in the code means.
Worked example
CONSTANT PassMark ← 50 and Score ← 42 distinguish a fixed rule from a changing input. A constant's value must be a literal, never a variable or an expression.
Practice
Warm-up · not marked
Check your understanding
Answer in your own words, then compare with the worked answer. Nothing here is saved or scored.
Worked answer
The meaning is clear and the rate can be changed in one definition rather than many scattered literals.
Check your method, units and explanation against it.
Marked practice
Set 1 of 4 · easiest first · Checked as you go, with the worked answer. Sign in or create a free account to save your progress.