11.1.2 Variables, Constants and Assignment
Programs need named places in which to store data. Some stored values may change while an algorithm runs, while others must remain fixed.
This section explains how to declare variables, create constants, initialise stored values and use assignment statements correctly in pseudocode.
By the end of this section, you should be able to:
- Distinguish between an identifier, a variable and its current value.
- Declare variables using appropriate pseudocode data types.
- Declare and initialise named constants.
- Explain the difference between declaration and initialisation.
- Use assignment to store, copy and replace values.
- Trace how assignments change the program state.
- Recognise assignments that are invalid because of type or constant rules.
Variables Store Values That May Change
A variable gives a name to a storage location used by an algorithm. The value held in that location can be read, replaced or used elsewhere in the program.
Example: room booking
The identifier AvailableRooms might initially store
18. After another room is booked, the same variable could
store 17.
AvailableRooms is the identifier; 18 is one value
that it may store.
Choosing Clear Identifiers
An identifier is the name used to refer to a variable, constant or other program element. A meaningful identifier makes pseudocode easier to understand and maintain.
| Less useful name | Clearer name | What the clearer name communicates |
|---|---|---|
x |
VisitorCount |
The value represents a number of visitors |
n |
LockerNumber |
The value identifies a locker |
p |
IsProjectComplete |
The value represents a Boolean condition |
d |
SubmissionDate |
The value stores a date |
Useful naming principles
- Choose a name that indicates the purpose of the stored value.
- Use the same spelling and capitalisation consistently.
- Avoid names that could be confused with pseudocode keywords.
- Do not reuse one identifier for unrelated data.
Declaring Variables
A variable declaration introduces an identifier and states the type of data that it is intended to store.
Pseudocode declaration pattern
DECLARE <Identifier> : <DataType>
Original examples
DECLARE VisitorCount : INTEGER
DECLARE AverageTemperature : REAL
DECLARE ZoneCode : CHAR
DECLARE ContactName : STRING
DECLARE IsDoorLocked : BOOLEAN
DECLARE InspectionDate : DATE
A declaration tells the reader what kind of value is expected. It does not necessarily give the variable its first meaningful value.
DECLARE VisitorCount = 24 combines two different ideas and does
not follow the required variable-declaration pattern. Declare the variable,
then assign its value.
Brief Data-Type Recall
The detailed selection of data types belongs to Unit 10.1. The following table provides the recall needed for writing declarations on this page.
| Data type | Suitable example | Example value |
|---|---|---|
INTEGER |
Number of completed tasks | 14 |
REAL |
Measured rainfall | 12.7 |
CHAR |
Single zone letter | 'C' |
STRING |
Name of a workshop | "Robotics Lab" |
BOOLEAN |
Whether a door is locked | TRUE |
DATE |
Date of an inspection | A valid date value |
IsDoorLocked : BOOLEAN, assigning the string
"yes" would be inappropriate. The variable should receive
TRUE or FALSE.
Initialising a Variable
Initialisation gives a variable its first usable value. This normally happens before the variable is read or used in another statement.
DECLARE CompletedTasks : INTEGER
CompletedTasks β 0
The first line introduces the variable. The second line stores its initial value.
| Stage | Pseudocode | Program state |
|---|---|---|
| Declaration | DECLARE CompletedTasks : INTEGER |
The identifier and type are known |
| Initialisation | CompletedTasks β 0 |
The variable now has a defined starting value |
Constants Store Fixed Values
Some values should remain unchanged throughout an algorithm. Giving such a value a meaningful name makes the pseudocode easier to understand and reduces repeated use of unexplained literals.
Pseudocode constant pattern
CONSTANT <Identifier> = <Value>
Original examples
CONSTANT MAX_TEAM_SIZE = 8
CONSTANT SCHOOL_DAY_HOURS = 7
CONSTANT STANDARD_LABEL = "Checked"
CONSTANT DEFAULT_ZONE = 'A'
A constant declaration normally gives the name and starting value together. The value should not later be changed through assignment.
Why use a constant?
| Benefit | Explanation |
|---|---|
| Meaning |
MAX_TEAM_SIZE communicates more than the unexplained
value 8.
|
| Consistency | The same fixed value can be referred to by one identifier throughout the solution. |
| Maintenance | A permitted design change can be made at the declaration rather than at many separate statements. |
| Protection | The algorithm communicates that the value must not be reassigned. |
Assignment Stores a Value
An assignment statement evaluates the value on its right and stores that value in the variable named on its left.
Pseudocode assignment pattern
<Variable> β <Value>
| Purpose | Example | Effect |
|---|---|---|
| Store a literal value | FloorNumber β 6 |
FloorNumber now stores 6 |
| Store a text value | RoomName β "Media Studio" |
RoomName stores the given string |
| Store a Boolean value | IsAvailable β TRUE |
IsAvailable stores TRUE |
| Copy a current value | PreviousRoom β CurrentRoom |
The current value of CurrentRoom is copied |
| Replace an earlier value | FloorNumber β 9 |
The previous value is overwritten |
PreviousRoom β CurrentRoom means that
PreviousRoom receives a copy of the current value.
Trace Assignment from Right to Left
To trace an assignment, first obtain the value on the right-hand side. Then store that value in the variable on the left-hand side.
Example trace
DECLARE CurrentRoom : INTEGER
DECLARE PreviousRoom : INTEGER
CurrentRoom β 407
PreviousRoom β CurrentRoom
CurrentRoom β 512
| After statement | CurrentRoom |
PreviousRoom |
|---|---|---|
| Declarations | Not yet assigned | Not yet assigned |
CurrentRoom β 407 |
407 | Not yet assigned |
PreviousRoom β CurrentRoom |
407 | 407 |
CurrentRoom β 512 |
512 | 407 |
The two variables do not remain permanently linked. The assignment copied the
value 407 at that moment. Changing CurrentRoom
later does not automatically change PreviousRoom.
Declaration, Initialisation and Reassignment
| Term | Purpose | Example |
|---|---|---|
| Declaration | Introduces the identifier and its data type | DECLARE LockerNumber : INTEGER |
| Initialisation | Gives the variable its first value | LockerNumber β 48 |
| Reassignment | Replaces the currently stored value | LockerNumber β 62 |
Worked Example: Equipment Check-In
A workshop system must store the identifier of an equipment cabinet, whether it is currently open and the fixed maximum number of items allowed inside.
Step 1: identify the stored data
| Identifier | Role | Data type |
|---|---|---|
CabinetCode |
Stores the cabinetβs label | STRING |
IsCabinetOpen |
Stores the current open/closed state | BOOLEAN |
MAX_ITEMS |
Stores a fixed permitted limit | Constant integer value |
Step 2: write declarations and initial values
CONSTANT MAX_ITEMS = 24
DECLARE CabinetCode : STRING
DECLARE IsCabinetOpen : BOOLEAN
CabinetCode β "ENG-C4"
IsCabinetOpen β FALSE
Step 3: change only the variable state
IsCabinetOpen β TRUE
The Boolean variable may change as the cabinet is opened or closed.
MAX_ITEMS remains fixed.
MAX_ITEMS β 30 later would conflict with the constant
declaration.
Interactive: Storage and Assignment Tracer
Select a scenario and move through each statement. The widget shows when an identifier is declared, when its first value is stored, and how later assignments affect the program state.
Common Mistakes and Misconceptions
-
Confusing declaration with assignment:
DECLARE Count : INTEGERintroduces a variable;Count β 0stores a value. - Using an uninitialised value: reading a variable before the algorithm has assigned it a suitable starting value.
-
Assigning the wrong type: storing
"FALSE"as text in a variable declared asBOOLEAN. - Changing a constant: using assignment to replace a value that was declared as fixed.
-
Reading assignment as equality: assuming
A β Bkeeps the two variables linked. - Using unclear identifiers: choosing names that do not communicate the meaning of the stored data.
-
Using programming-language syntax: writing
=when the required pseudocode assignment symbol isβ.
Practice
Question 1: declarations
Write pseudocode declarations for:
- a whole-number count of bicycles;
- the name of a cycle station;
- whether the station is currently full;
- the date of the next inspection.
Question 2: constants
A drone-testing room permits no more than 12 people. Write a suitable constant declaration using a meaningful identifier.
Question 3: initialisation
Declare ReturnedDevices as an integer and initialise it to
zero.
Question 4: trace the assignments
DECLARE FirstGate : INTEGER
DECLARE SecondGate : INTEGER
FirstGate β 3
SecondGate β FirstGate
FirstGate β 7
State the final value stored in each variable and explain why the values are different.
Question 5: find the invalid statement
CONSTANT MAX_ATTEMPTS = 4
DECLARE AttemptsUsed : INTEGER
AttemptsUsed β 0
MAX_ATTEMPTS β 6
Identify the invalid statement and explain why it is invalid.
Question 6: correct the type error
DECLARE IsLabOpen : BOOLEAN
IsLabOpen β "open"
Rewrite the assignment using a suitable value.
Show suggested answers
Question 1
DECLARE BicycleCount : INTEGER
DECLARE StationName : STRING
DECLARE IsStationFull : BOOLEAN
DECLARE NextInspection : DATE
Question 2
CONSTANT MAX_ROOM_OCCUPANCY = 12
Question 3
DECLARE ReturnedDevices : INTEGER
ReturnedDevices β 0
Question 4
FirstGatestores7.SecondGatestores3.-
The second assignment copied the value
3. It did not create a permanent link between the variables.
Question 5
MAX_ATTEMPTS β 6 is invalid because
MAX_ATTEMPTS was declared as a constant.
Question 6
IsLabOpen β TRUE
Review
| Concept | Key idea | Pseudocode example |
|---|---|---|
| Identifier | The name used to refer to a program element | VisitorCount |
| Variable | A named storage location whose value may change | DECLARE VisitorCount : INTEGER |
| Declaration | Introduces the identifier and its type | DECLARE RoomName : STRING |
| Initialisation | Provides the first usable value | VisitorCount β 0 |
| Constant | A named value that must remain fixed | CONSTANT MAX_USERS = 30 |
| Assignment | Stores or replaces a variableβs value | CurrentRoom β 407 |