Cambridge O Level Computer Science · Syllabus 2210 · Programming
Variable
What is Variable?
A named storage location, declared with an identifier and a data type, whose value may change while the program is running.
This definition is part of the Programming chapter in Cambridge O Level Computer Science.
Variable in context
A program is a stored sequence of instructions that manipulates data held in named storage. Topic 8 asks you to do four things with that idea. First, name and type your data correctly: a variable whose value may change, a constant whose value may not, each with one of the five data types INTEGER, REAL, CHAR, STRING, BOOLEAN. Second, control the order in which instructions run using only three structures — sequence, selection (IF, CASE) and iteration (FOR, WHILE, REPEAT). Third, break a long solution into procedures and functions so it stays readable and testable. Fourth, hold many related values in an array and hold them beyond the end of the run in a file. Everything else in this chapter is detail hanging off those four moves.
A variable is a named storage location whose value may change while the program runs; it is created with DECLARE <identifier> : <data type>. A constant is a named value that does not change while the program runs; it is created with CONSTANT <identifier> ← <value>, and only a literal may be used as the value — never a variable, another constant or an expression. Both must have a meaningful identifier. Every variable also has a data type that fixes what kind of value it can hold and what operations are legal on it.
Sequence means statements are executed one after another, in the order written, unless a control structure changes that order. INPUT <identifier> takes a value supplied by the user and stores it in the named variable. OUTPUT <value(s)> displays one or more values, separated by commas. Assignment is written <identifier> ← <value>: the expression on the right is evaluated first, and the single result is then stored in the variable on the left, replacing whatever was there. The identifier on the left must be a variable — it may be an element of a data structure such as an array — and the value must be of the same data type as the variable.
Iteration means a block of statements is executed repeatedly. Cambridge pseudocode has exactly three iteration structures. A count-controlled loop (FOR ... NEXT) runs a known number of times, driven by an INTEGER loop variable that takes every value from a start to an end inclusive. A pre-condition loop (WHILE ... ENDWHILE) tests its condition before the body, so it may run zero times, and it continues while the condition is TRUE. A post-condition loop (REPEAT ... UNTIL) tests its condition after the body, so it always runs at least once, and it stops when the condition becomes TRUE.
Totalling is accumulating values into a variable: Total ← Total + Value. Counting is recording how many times something happened: Count ← Count + 1. They look almost identical and are constantly confused, but they answer different questions — how much altogether? against how many? — and the value added is different: the data item for a total, always the number 1 for a counter. Both must be initialised to 0 before the loop, and a counter should be incremented only when its condition is satisfied.
A local variable is declared inside a procedure or function and is available only within that routine. A global variable is declared outside all routines and is available throughout the program, including inside the routines. A routine's parameters behave as local variables too. Local variables are preferred because they cannot be changed by accident from somewhere else in the program, which makes a routine easier to test on its own; too many globals make a program harder to understand, harder to test and harder to change safely.
A maintainable program is one that another programmer — or you, six months later — can read, understand and safely change. The syllabus names the ingredients: meaningful identifiers for variables, constants, arrays, procedures and functions; relevant and appropriate use of the commenting feature provided by the language; and the use of procedures and functions to break the solution into named pieces. Consistent indentation and the removal of unnecessary repeated code follow from those. The syllabus lists it as a learning outcome of its own, 8.1.8, so it is something you can be asked about directly as well as something to practise in every answer you write.
An array is a fixed-length structure of elements of identical data type, held under one identifier and reached by consecutive index numbers. Its purpose is to let one loop handle many related values: without arrays, thirty marks need thirty separate variables and thirty copies of every statement. It is good practice to state the lower bound explicitly, because that bound defaults to 0 in some systems and 1 in others; generally a lower bound of 1 is used. Square brackets hold the indices, and in ordinary statements only one index value is used for each dimension.
Variables and arrays live in main memory, which is volatile and is released when the program ends — so anything held only there is lost. A file is stored on secondary storage, so the data still exists after the program has finished and can be read again on a later run, by this program or another one. It is good practice to explicitly open a file, stating the mode of operation, before reading from or writing to it. The two modes are READ, for data to be read from the file, and WRITE, for data to be written to it — and in WRITE mode a new file will be created and any existing data in the file will be lost. A file should be opened in only one mode at a time, and files should be closed when they are no longer needed.
Common mistakes with Variable
- M1. “A variable and a constant are basically the same — use whichever.” TruthA variable's value may change while the program runs; a constant's may not. They are also declared differently: DECLARE Counter : INTEGER against CONSTANT PassMark ← 50. TestIf the program ever assigns to it after the start, it must be a variable. If not, a constant is clearer and safer.
- M4. “INPUT checks the data for me.” TruthINPUT stores whatever it is given in the named variable. It performs no validation of any kind. FixWrite the check yourself, usually a REPEAT ... UNTIL around the INPUT that only accepts values in range.
- M6. “CASE is the tidier choice, so use it for every selection.” TruthA case clause matches one single value of one identifier. It cannot express a range or a compound condition. RuleDiscrete values of one variable → CASE. Ranges, comparisons or AND/OR → IF. The specification itself says that if the cases are more complex, an IF should be considered.
- M7. “FOR Count ← 1 TO 5 runs four times — the limit is exclusive.” TruthBoth limits are inclusive. The variable is assigned every integer from value1 to value2, so this runs five times, with Count taking 1, 2, 3, 4 and 5. AlsoIf value1 = value2 it runs once; if value1 > value2 with the default step it runs not at all.
- M19. “A local variable can be used anywhere in the program.” TruthA local variable is declared inside a procedure or function and is available only within that routine. Elsewhere the identifier refers to nothing. AlsoTwo routines cannot see each other's locals, and a local with the same name as a global is a different store.
- M26. “Data in a file disappears when the program finishes.” TruthExactly backwards. Variables and arrays live in main memory and are lost. A file is on secondary storage precisely so the data survives the end of the program. That is the purpose— and it is what an 8.3.1 question is asking you to explain.
Examiner tips on Variable
- Never change the loop-control variable inside a FOR loop. Writing Count ← Count + 3 inside a FOR Count loop makes the number of executions unpredictable to a reader and defeats the point of a count-controlled loop. If the step is not 1, use STEP. If the number of repetitions is not known in advance, you needed a WHILE or REPEAT in the first place.
- The shortcut worth knowing. The five lines above can be written as one: Correct ← NOT (Answer < 0 OR Answer > 100), or more directly Correct ← (Answer >= 0) AND (Answer <= 100). Both are valid, because a relational expression is already a Boolean value and can be assigned straight to a BOOLEAN variable. Writing IF X = TRUE THEN Y ← TRUE ELSE Y ← FALSE ENDIF is never wrong, but it is four lines of ceremony around Y ← X.
Questions students ask about Variable
What is the difference between a variable and a constant?
A variable is a named storage location, declared with DECLARE and a data type, whose value may change while the program is running. A constant is a named value fixed with CONSTANT and a literal at the start of the program, and it must never change while the program runs — only a literal may be used as its value, never a variable, another constant or an expression. If a program ever assigns to it after the start, it must be a variable; if not, a constant is clearer and safer.
When should IF be used instead of CASE?
An IF statement tests a Boolean condition and is the right choice for ranges, comparisons and compound conditions using AND, OR or NOT. A CASE OF statement compares one identifier against a list of single discrete values, executes the statement of the first matching case, and then finishes, with an OTHERWISE clause, if present, always last. CASE cannot express a range or a compound condition, so discrete values of one variable point to CASE, while ranges or comparisons point to IF.
Why are local variables generally preferred over global variables?
A local variable is declared inside a procedure or function and is available only within that routine, so it cannot be changed by accident from anywhere else in the program, which makes the routine easier to test on its own. A global variable is declared outside all routines and is available throughout the program, including inside every routine, so any routine can change it — when a global holds the wrong value, every routine that touches it becomes a suspect, and none can be tested in isolation.

