A-Level Computer Science / Unit 11: Structured Programming

11.1.2 Variables, Constants and Assignment

πŸ”’ Lesson slides are available to signed-in users. Sign in

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.
Scope of this page: choosing data types was introduced in Unit 10.1. Here, the focus is on using those types in declarations and assignment statements. Expressions are developed in 11.1.3.

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.

Variable: a named storage location whose value may change while an algorithm runs.

Example: room booking

The identifier AvailableRooms might initially store 18. After another room is booked, the same variable could store 17.

AvailableRooms: 18 β†’ 17
Common misconception: the variable is not the value itself. 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.

Identifier: a name used to refer to a program element.
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.
Exam tip: meaningful identifiers make your intended solution clearer, especially when several values have similar data types.

Declaring Variables

A variable declaration introduces an identifier and states the type of data that it is intended to store.

Declaration: a statement that introduces a program element and provides required information about it.

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.

Common mistake: writing 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
Type compatibility: after declaring 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.

Initialisation: giving a variable or constant its starting value.
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
Common mistake: do not assume that declaring a variable automatically gives it the value that your algorithm requires.

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.

Constant: a named value that must remain fixed while the algorithm runs.

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.
Common mistake: a constant cannot be used as a changing counter, running total or temporary result.

Assignment Stores a Value

An assignment statement evaluates the value on its right and stores that value in the variable named on its left.

Assignment: storing a value in a variable, replacing the variable’s previous value if it already had one.

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
Read the assignment arrow as β€œis assigned the value of”. For example, 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.

Common misconception: assignment does not create an ongoing equality relationship between two variables.

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
A strong explanation distinguishes what is known after each stage: the identifier and type after declaration, then a stored value after initialisation.

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.

Writing 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.

Variable lifecycle

Program state

Current effect No statement has run yet.

Step 1 of 4

Declare the variable

The identifier and data type become known, but no starting value has yet been assigned.

When tracing on paper, create a column for each variable and record its value after every assignment.

Common Mistakes and Misconceptions

  • Confusing declaration with assignment: DECLARE Count : INTEGER introduces a variable; Count ← 0 stores 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 as BOOLEAN.
  • Changing a constant: using assignment to replace a value that was declared as fixed.
  • Reading assignment as equality: assuming A ← B keeps 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:

  1. a whole-number count of bicycles;
  2. the name of a cycle station;
  3. whether the station is currently full;
  4. 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

  • FirstGate stores 7.
  • SecondGate stores 3.
  • 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
Final exam tip: use the required pseudocode notation and explain state changes precisely. State what value is read, where it is stored, and whether an earlier value is replaced.