[8.1.4a] Sequence

[8.1.4a] Sequence

Sequence is the simplest of the three fundamental control structures in programming (sequence, selection, iteration). It means instructions are carried out in the exact order they are written: first step, then the next, and so on. If the steps are rearranged, skipped, or duplicated, the program's behaviour changes and the result is often wrong. Understanding sequence helps you plan algorithms clearly and explain why a given output occurs.

Why sequence matters

Many tasks depend on earlier steps having already happened. You cannot calculate a total before you have added all the items; you cannot print a personalised greeting before you know the user's name. In exams you will often need to reason about the effect of sequence in pseudocode or trace the contents of variables as statements execute one by one. Getting comfortable with sequence prepares you to combine it safely with selection (IF) and iteration (loops).

Sequencing scenarios

Use these tabs to compare correct straight-line code with common sequencing mistakes. Notice how small changes in order cause different outputs.

Straight-line recipe (correct order)

This tiny program calculates the price after a discount then prints the final message. Each line relies on the previous one, so the order is crucial.

// CAIE Pseudocode
DECLARE price : REAL ← 20.00
DECLARE discount : REAL ← 0.10
DECLARE final : REAL
final ← price - (price * discount)
OUTPUT "Final price: £" & FORMAT(final, 2)

# Python
price = 20.00
discount = 0.10
final = price - (price * discount)
print(f"Final price: £{final:.2f}")

The assignment to final must come after price and discount are set, otherwise the calculation will be wrong or cause an error.

Out-of-order steps (bug)

If you print before you calculate, you will show an uninitialised or old value. The lines look similar to the correct version, but the order ruins the outcome.

// CAIE Pseudocode (Incorrect sequence)
DECLARE price : REAL ← 20.00
DECLARE discount : REAL ← 0.10
OUTPUT "Final price: £" & FORMAT(final, 2)   // final not set yet
DECLARE final : REAL
final ← price - (price * discount)

// Python (Incorrect sequence)
price = 20.00
discount = 0.10
print(f"Final price: £{final:.2f}")  # NameError: final before assignment
final = price - (price * discount)

Sequencing errors like this are easy to miss in long scripts, but trace tables make them visible by showing values line by line.

Hidden dependency revealed

Sometimes a later line silently depends on an earlier input or conversion. Forgetting that dependency creates logic errors even if the program runs.

// CAIE Pseudocode
DECLARE t : STRING
DECLARE age : INTEGER
OUTPUT "Enter age:"
INPUT t
age ← TOINTEGER(t)                // must happen before comparison
IF age >= 16 THEN
  OUTPUT "Eligible."
ELSE
  OUTPUT "Not eligible."
ENDIF

# Python
t = input("Enter age: ")
age = int(t)
if age >= 16:
    print("Eligible.")
else:
    print("Not eligible.")

The conversion must happen before the comparison. That ordering is part of the algorithm's sequence.

Reading sequence with trace tables

A trace table helps you follow sequence by listing variables and tracking their values after each line. You can spot where a value is used before it is set, or where a later assignment overwrites something you still need.

LineStatementpricediscountfinalOutput
1price ← 20.0020.00---
2discount ← 0.1020.000.10--
3final ← price - (price × discount)20.000.1018.00-
4OUTPUT ... final20.000.1018.00Final price: £18.00

Notice how the table makes the dependence of line 4 on line 3 explicit. If line 4 came earlier, the final column would show blank or incorrect values.

Design tips for writing in sequence

  • State your goal first: write a short comment or sentence describing the required output; then list the inputs needed to achieve it. This clarifies the order.
  • Gather then act: read all inputs and perform conversions before calculations that rely on them.
  • One job per line: avoid mixing reading, converting, calculating, and outputting in a single statement while learning; it makes tracing harder.
  • Check overwrites: if a later assignment reuses a variable name, be sure you do not still need the old value.

Sequence in real-world programs

Sequencing is everywhere. A ticket machine must read the destination, then calculate the fare, then accept payment, then print the ticket. An online form must validate required fields before submission. A microcontroller must read a sensor, then decide whether to switch a motor on, then update a display. Each of these is a chain of dependent steps where getting the order wrong leads to unwanted behaviour.

Common sequencing pitfalls and fixes

PitfallWhat goes wrongFix
Using a variable before assignment Run-time errors or default/garbage values Initialise variables and compute before output
Comparing strings when numbers are required Wrong ordering or decisions Convert to INTEGER/REAL before comparison
Printing intermediate values as if final User sees misleading results Print only after the calculation chain is complete
Overwriting inputs too early Later calculations lose needed data Use separate variables or store copies before reuse

Connecting sequence with selection and iteration

Even when you add IF statements and loops, each block still runs in sequence. For example, within a loop you might read input, validate it, then update a total, then display a summary. The order inside the loop matters just as much as the overall order of the program. Thinking carefully about sequence prevents subtle logic errors when structures are nested.

Key terminology

  • Sequence: executing statements one after another in a fixed order.
  • Initialise: set a starting value for a variable before it is used in calculations.
  • Dependency: when one step requires the result of an earlier step to be correct.
  • Trace table: a layout to record variable values after each line to follow sequence and find errors.

 Key Takeaways

  • Sequence means instructions run in the written order; changing the order changes the result.
  • Later steps often depend on earlier inputs, conversions, and calculations.
  • Trace tables reveal sequencing errors by showing variable values line by line.
  • Read and convert input before calculations, then output the final result.
  • Even inside IFs and loops, the order of statements still matters.