Sequencing

⏱ 7 min✏️ Quiz at the end

One Step After Another

Sequencing means putting instructions in the correct order and carrying them out one after another. It is the simplest of the three building blocks of every algorithm β€” before selection and repetition, there is sequence.

Step 1  β†’  Step 2  β†’  Step 3  β†’  Step 4

Each step waits for the previous one to finish. Step 3 never runs before step 2. The computer never skips ahead, never reorders, never runs two steps of a sequence at once.

Order Changes Everything

The same steps in a different order can give a completely different result.

Start with the number 4:

Sequence ASequence B
Add 2 β†’ gives 6Multiply by 3 β†’ gives 12
Multiply by 3 β†’ gives 18Add 2 β†’ gives 14

Same two steps, opposite order, different answers. This is exactly why order of operations matters in mathematics, and why a computer must be told the exact order.

Dependencies Decide the Order

Some steps depend on others β€” they cannot happen until an earlier step is done:

  • You cannot butter toast before the bread is toasted
  • You cannot put on shoes before socks
  • You cannot pour the water before boiling it (if you want hot tea!)

When designing an algorithm, ask for each step: "What must already have happened for this step to work?" The answers tell you the required order.

Worked Example β€” Robot Sandwich

Put these shuffled steps into a correct sequence for a robot making a jam sandwich:

  • Spread jam on one slice
  • Take two slices of bread out of the bag
  • Press the two slices together
  • Open the bread bag
  • Open the jam jar

Correct sequence:

  1. Open the bread bag
  2. Take two slices of bread out of the bag
  3. Open the jam jar
  4. Spread jam on one slice
  5. Press the two slices together

Notice steps 1–2 and step 3 are independent β€” the jam jar could be opened first. But step 4 needs both the bread (step 2) and the open jar (step 3), and step 5 must come last.

Spotting Sequence Errors

When an algorithm gives the wrong result, a wrong order is one of the first things to check while debugging. Trace it like a robot:

  1. Point at step 1, do exactly what it says
  2. Move to the next step, do exactly what it says
  3. Continue to the end β€” did you get the expected result?

If you had to assume something had already happened, you have found a missing or misplaced step.

Sequences Inside Bigger Algorithms

Real algorithms are rarely just a sequence β€” but sequence is always the skeleton holding everything else together. A loop is really "run this sequence of steps, then check a condition, then decide whether to run the sequence again". A selection is really "run this sequence of steps down one of two possible paths". Even a whole program built from several functions is, underneath, one long sequence: call function A, wait for it to finish completely, then call function B. Understanding plain sequencing thoroughly β€” that each step waits its turn and nothing runs out of order β€” is what makes the more advanced building blocks make sense later, rather than feeling like separate, unrelated ideas.

Worked Example β€” A Morning Routine, Reordered

Here is a routine written in the wrong order, alongside a corrected version, to practise spotting dependencies:

Wrong order:

  1. Get dressed
  2. Take a shower
  3. Set an alarm
  4. Wake up

Why it fails: you cannot get dressed before showering (you'd have to undress again), and you cannot do any of it before waking up β€” and setting the alarm needs to happen before going to sleep the previous night, not after waking.

Corrected sequence:

  1. Set an alarm (the night before)
  2. Wake up
  3. Take a shower
  4. Get dressed

Notice the technique used to fix it: for each step, ask "what must be true, or already done, for this step to make sense?" β€” showering requires being awake; getting dressed sensibly happens after showering, not before. This same dependency-checking question is exactly what you should ask when debugging a sequence of code that produces the wrong output: find the first step where "what actually happened" and "what needed to have already happened" don't match.

Key Words

  • Sequence β€” instructions carried out one after another, in order
  • Dependency β€” when one step requires another step to have happened first
  • Trace β€” following an algorithm step by step, exactly as written, to check what it really does