- Variables and constants
A variable is a labelled box whose contents can change; a constant is fixed for the whole run.
A variable is best pictured as a labelled box in memory. The label is the variable's name and the contents are its current value, which can change as the program runs.
You bring a variable into existence with a DECLARE statement that gives its name and its data type:
DECLARE Count : INTEGER
DECLARE Average : REAL
DECLARE Name : STRING
A constant is a value you fix when you write the program and that cannot change while it runs. It is declared on one line, with its value, using CONSTANT — there is no data type and no separate assignment:
CONSTANT Pi = 3.14
CONSTANT TaxRate = 0.07
Why bother with a constant rather than just typing the number? Because it gives the value a meaningful name, defines it in one place, and stops it being changed by accident. If the tax rate ever changes, you edit a single line.
- Variable = labelled box; its value can change.
- DECLARE Name : Type introduces a variable.
- Constant = fixed value, set once: CONSTANT Pi = 3.14.
- Constants improve readability and are changed in one place.