Learn
8.1.4A | SEQUENCE AND ASSIGNMENT
01 | THE SAME INSTRUCTIONS CAN GIVE DIFFERENT RESULTS
Imagine calculating a ticket cost. You need a quantity before multiplying it by the price, and a calculated cost before displaying the result. The instructions might all be correct individually, yet placing them in the wrong order can make the solution wrong.
Sequence means executing instructions in the specified order, one after another. In a simple straight-line algorithm, follow the statements from top to bottom.
Assignment stores a value in a named item. Because one assignment can change what a later instruction reads, sequence and assignment work closely together.
We will use short examples so you can see the state after every instruction. Do not jump directly to a final answer: track what each variable holds at the moment it is used.
02 | START WITH A SIMPLE SEQUENCE
This algorithm receives a valid whole-number ticket quantity, calculates its cost at a fixed price and displays it.
CONSTANT TicketPrice ← 4.50 DECLARE Quantity : INTEGER DECLARE TotalCost : REAL OUTPUT "Enter ticket quantity" INPUT Quantity TotalCost ← Quantity * TicketPrice OUTPUT "Total cost: ", TotalCost
Declarations introduce variables. The prompt comes before receiving Quantity. The calculation uses that supplied value, and the final output uses the calculated TotalCost.
With input three, TotalCost becomes thirteen point five. Outputting TotalCost before calculating it would attempt to use a value this algorithm has not yet assigned.
Sequence does not mean every instruction has to be an assignment. Input, processing and output still occur in a meaningful order.
03 | READ THE ASSIGNMENT ARROW AS AN ACTION
In Cambridge pseudocode, the arrow points towards the item receiving a value.
Score ← 12
This means store twelve in Score. It is not a question asking whether Score already equals twelve.
In contrast, Score = 12 is an equality comparison used in a condition. Assignment changes the stored state; a comparison produces TRUE or FALSE without itself changing Score.
For the examples in this lesson, declare a variable with an appropriate type before assigning it. A constant may be given a value when defined, but must not be reassigned during execution.
04 | READ, EVALUATE, THEN STORE
When the right-hand side contains an expression, evaluate it using the current values before replacing the left-hand value.
DECLARE Score : INTEGER Score ← 7 Score ← Score + 5
READ
Score is 7Retrieve the existing value
EVALUATE
7 + 5 gives 12Calculate the right side
STORE
Score becomes 12Replace the old value
The instruction is valid because Score already has a value. It does not mean that Score somehow equals itself plus five at the same instant.
With Total ← Total + Amount, the old Total and current Amount are read, their sum is calculated and only then is Total replaced.
05 | REPLACEMENT IS NOT THE SAME AS ACCUMULATION
Compare these two assignments when Score currently holds seven.
| Instruction | New Score | Reason |
|---|---|---|
| Score ← 5 | 5 | Replace with the literal five |
| Score ← Score + 5 | 12 | Add five to the old seven, then store the sum |
The plus expression explicitly uses the old value. Without it, the earlier value is simply overwritten.
Initialisation such as Total ← 0 establishes a starting state. Repeating that reset before every addition would discard earlier accumulated values. Correct statements still need correct placement.
An assignment affects its named destination. It does not automatically change every other variable that happens to contain a related number.
06 | TRACE A DEPENDENT SEQUENCE
Follow this complete fragment using two INTEGER variables.
DECLARE A : INTEGER DECLARE B : INTEGER A ← 3 B ← A + 2 A ← B * 4 B ← A - B OUTPUT A, B
| Instruction completed | A | B | Calculation |
|---|---|---|---|
| Declarations | Not yet assigned | Not yet assigned | No useful starting values stated |
| A ← 3 | 3 | Not yet assigned | Store three |
| B ← A + 2 | 3 | 5 | Three plus two |
| A ← B * 4 | 20 | 5 | Five times four |
| B ← A - B | 20 | 15 | Twenty minus the old five |
| OUTPUT A, B | 20 | 15 | Read both values; do not change them |
The fourth assignment to B reads B’s old value of five while evaluating the expression. B becomes fifteen only after the subtraction has been calculated.
The short names keep the trace compact. In a real scenario, choose more descriptive identifiers when their roles are known.
07 | STEP THROUGH THE STATE CHANGES
Predict the next values before pressing the button. This demonstration follows the same sequence as the table, with declarations assumed complete.
A ← 3 B ← A + 2 A ← B * 4 B ← A - B OUTPUT A, B
A
Not yet assignedB
Not yet assignedReady: no assignment has executed.
No value output yet.
The output step reads the final state. It does not remove the values or reset the variables.
08 | CHANGE THE ORDER, CHANGE THE ANSWER
Begin each of these alternatives with Score equal to three.
| Version A | Version B |
|---|---|
| Score ← Score + 2 | Score ← Score * 4 |
| Score ← Score * 4 | Score ← Score + 2 |
| Final Score: 20 | Final Score: 14 |
Version A first reaches five, then multiplies five by four to reach twenty. Version B first reaches twelve, then adds two to reach fourteen.
The same addition and multiplication appear in both, but the second instruction reads a different current value. Therefore rearranging dependent assignments can change the result.
This is instruction order, not simply arithmetic precedence inside one expression. In one expression, use parentheses to make intended grouping clear: (3 + 2) * 4 gives twenty, while 3 * 4 + 2 gives fourteen.
09 | A CALCULATED VALUE IS NOT A LIVE FORMULA
An assignment stores the result calculated at that moment. It does not automatically keep recalculating when one of its inputs later changes.
DECLARE Quantity : INTEGER DECLARE Price : REAL DECLARE Total : REAL Quantity ← 2 Price ← 3.50 Total ← Quantity * Price Quantity ← 3 OUTPUT Total
Total is seven when it is calculated. Quantity later becomes three, but Total still holds seven, so the output is seven.
To calculate the new cost, execute Total ← Quantity * Price again. That new assignment stores ten point five.
This distinction matters when amending a solution: place a recalculation after the data it depends on has been updated.
10 | COPYING A VALUE DOES NOT LINK FUTURE UPDATES
A numeric assignment from one variable to another copies the current value.
DECLARE Original : INTEGER DECLARE Copy : INTEGER Original ← 8 Copy ← Original Original ← 11 OUTPUT Original, Copy
Copy receives eight. Changing Original later to eleven does not change Copy. Final outputs are eleven and eight.
Do not treat Copy ← Original as a permanent relationship saying they must always be equal. It is a single instruction executed at a particular point in the sequence.
If you need the new value copied too, execute another copy assignment. This explanation concerns the simple numeric values used here, rather than every possible object or reference behaviour in programming languages.
11 | WHY A NAIVE SWAP LOSES A VALUE
Suppose A holds four and B holds nine. You want A to become nine and B to become four. This attempt fails:
A ← B B ← A
The first instruction replaces A with nine. The original four is lost from A. The second instruction reads the current A, which is now nine, and places nine in B.
| Step | A | B |
|---|---|---|
| Start | 4 | 9 |
| A ← B | 9 | 9 |
| B ← A | 9 | 9 |
The computer follows the sequence precisely. It does not infer that you intended both assignments to use the original starting values simultaneously.
12 | SWAP CORRECTLY USING TEMPORARY STORAGE
Preserve the old A before overwriting it. A temporary variable gives you somewhere to keep that value.
DECLARE A : INTEGER DECLARE B : INTEGER DECLARE Temp : INTEGER A ← 4 B ← 9 Temp ← A A ← B B ← Temp
| Step | A | B | Temp |
|---|---|---|---|
| Before swap | 4 | 9 | Not yet assigned |
| Temp ← A | 4 | 9 | 4 |
| A ← B | 9 | 9 | 4 |
| B ← Temp | 9 | 4 | 4 |
Temp is a variable with a temporary purpose, not a new basic data type. Here it is INTEGER because the values being exchanged are integers.
You met this pattern in bubble sort: preserve one item before replacing its position. Correct storage and sequence prevent data loss.
13 | CHECK THAT EVERY VALUE EXISTS WHEN NEEDED
An instruction can refer to a variable that has been declared but not assigned. A declaration alone does not give a reliable value for a later expression.
DECLARE Quantity : INTEGER DECLARE Total : REAL // Incorrect order: Quantity has not received a value Total ← Quantity * 4.50 INPUT Quantity
The later INPUT cannot repair the earlier calculation retroactively. Move input before the calculation and ensure the value is appropriate for the task.
Likewise, a running total must be initialised before using Total ← Total + Amount. Its own current value is part of the expression.
In a trace, mark a value as not yet assigned until an instruction actually supplies it. Do not fill every newly declared numeric column with zero by assumption.
14 | SEQUENCE WORKS WITH OTHER CONTROL STRUCTURES
Sequence is one of the basic ways to organise instructions. Selection chooses between paths; iteration repeats a path. Within a selected branch or repeated body, the instructions still run in their defined order.
For example, a validation retry must receive input before checking it, and a total should update after that input is accepted. The loop structure does not remove those dependencies.
The next lesson introduces IF, THEN and ELSE. For now, establish the habit of asking which statement runs next and which values it reads at that moment.
15 | EXPLAIN THE EFFECT OF ORDER
A useful explanation is: “The cost calculation uses Quantity, so input must happen first. Otherwise Quantity has no assigned value when the expression is evaluated.”
For swapping: “The first assignment overwrites A, therefore its original value must be copied to Temp before that overwrite. The final assignment reads Temp to restore the preserved value into B.”
Check your work by tracing the exact sequence, reading current values, evaluating the right-hand side and recording only the destination change. Then record output as a separate event.
A correct final answer matters, but understanding each state change makes it possible to explain errors and adapt the algorithm.