CIE IGCSE | 8.1.4A Sequence and Assignment

Lesson objective

Explain sequence, follow assignment using current values, predict the effects of instruction order and use a temporary variable to swap values correctly.

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 7

Retrieve the existing value

EVALUATE

7 + 5 gives 12

Calculate the right side

STORE

Score becomes 12

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

These alternatives begin from the same original state
InstructionNew ScoreReason
Score ← 55Replace with the literal five
Score ← Score + 512Add 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
Final value outputs are twenty and fifteen
Instruction completedABCalculation
DeclarationsNot yet assignedNot yet assignedNo useful starting values stated
A ← 33Not yet assignedStore three
B ← A + 235Three plus two
A ← B * 4205Five times four
B ← A - B2015Twenty minus the old five
OUTPUT A, B2015Read 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 assigned

B

Not yet assigned

Ready: 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.

    Each version starts separately from three
    Version AVersion B
    Score ← Score + 2Score ← Score * 4
    Score ← Score * 4Score ← Score + 2
    Final Score: 20Final 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.

    Final values are duplicated, not swapped
    StepAB
    Start49
    A ← B99
    B ← A99

    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
    The original four survives in Temp until B receives it
    StepABTemp
    Before swap49Not yet assigned
    Temp ← A494
    A ← B994
    B ← Temp944

    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.

    MATCH THE INSTRUCTION OR STEP TO ITS ROLE

    Terminology

    Terminology

    Sequence

    Execution of instructions in their specified order.

    Assignment

    Storing an evaluated value in the named destination.

    Assignment arrow

    The Cambridge pseudocode symbol ← indicating assignment towards the destination.

    Destination

    The item receiving a value on the left of an assignment.

    Expression

    Values and operations evaluated to produce a result.

    Current value

    The value held at the moment an instruction executes.

    Initialisation

    Assigning a starting value before it is needed.

    State

    The values currently held by the algorithm’s items at a point in execution.

    Dependency

    A relationship in which one operation needs a value produced or updated by another.

    Replacement

    Overwriting a variable’s old value with a new one.

    Accumulation

    Updating a running total by adding to its previous value.

    Value copy

    Assigning the current simple numeric value of one variable to another.

    Temporary variable

    A variable used to preserve or hold an intermediate value.

    Swap

    Exchanging two stored values without losing either.

    Recalculation

    Executing a calculation again to store a result based on updated values.

    Equality comparison

    Testing whether two values are equal, producing a Boolean result.

    Dry run

    Following the exact algorithm by hand with chosen values.

    Trace table

    A record of state changes and relevant events as instructions execute.

    Questions

    Questions

    CHECK YOUR UNDERSTANDING

    Select all correct choices. Each exact set earns one point.

    1. Which statements describe sequence?
    2. Score holds seven. What does Score ← Score + 5 do?
    3. Starting from Score three, adding two then multiplying by four gives which result?
    4. Starting separately from Score three, multiplying by four then adding two gives which result?
    5. Quantity is two; Total ← Quantity * 3.50 runs; Quantity becomes three. What happens without recalculation?
    6. A is four and B is nine. A ← B then B ← A gives which final state?
    7. Which sequence swaps A and B correctly?
    8. Which checking habits are useful?

    EXPLAIN AND COMPARE

    Write your answers before revealing each sample.

    1. Define sequence and explain why it matters in a ticket-cost calculation.

    2. Explain the steps performed by Score ← Score + 5 when Score currently holds seven.

    3. Calculate A and B after A ← 3, B ← A + 2, A ← B * 4, B ← A - B. Show intermediate values.

    4. Compare Score ← 5 and Score ← Score + 5 when Score initially holds seven.

    5. Explain why changing Quantity after Total ← Quantity * Price does not automatically change Total.

    6. Explain why A ← B followed by B ← A fails to swap initial A four and B nine.

    7. Demonstrate a correct swap using Temp and trace initial A four and B nine.

    8. Explain why declaration does not make Total ← Total + Amount safe without a starting Total.

    REPAIR IT | PUT THE COST STEPS IN ORDER

    A ticket costs a fixed 2.50. A user supplies a valid non-negative whole-number quantity. These steps are scrambled: output TotalCost; calculate TotalCost from Quantity and TicketPrice; input Quantity; output the quantity prompt. Write a complete correctly ordered algorithm, including constant definition and declarations. Trace input four.

    Reveal one possible solution
    CONSTANT TicketPrice ← 2.50
    DECLARE Quantity : INTEGER
    DECLARE TotalCost : REAL
    OUTPUT "Enter the number of tickets"
    INPUT Quantity
    TotalCost ← Quantity * TicketPrice
    OUTPUT "Total cost: ", TotalCost

    Quantity receives four, then TotalCost becomes ten. The final labelled output reports ten. The prompt must precede input and the calculation must precede the result output.

    Extend: after that output, Quantity ← Quantity + 1 runs. Quantity becomes five while TotalCost remains ten. Add a recalculation and new output if the task requires the cost for five tickets.

    Flashcards

    Flashcards

    Click to flip. Select the ideas you need to revisit.

    0 cards selected for revision.

      Selections are kept while this page is open.

      Workbook

      Workbook

      COMING SOON

      The workbook for 8.1.4A Sequence and Assignment is coming soon. Complete the matching task and written questions in your exercise book.