Writing and Executing an Algorithm
The Order of Steps Matters
The Order of Steps Matters
An algorithm is an ordered sequence of instructions: changing the order of the steps can completely change the result, or even make the algorithm impossible to execute.
Let's go back to the tea recipe. What happens if we swap two steps?
1. (Pour the hot water over the bag)
2. (Boil some water)
Here, step 1 asks you to pour hot water that hasn't yet been heated in step 2! The instruction is impossible to carry out in this order. This is exactly what happens in computer science when you use a variable before it's been created, or display a result before it's been calculated.
Look at this small algorithm, in pseudocode, to calculate an average:
1. READ note1, note2
2. moyenne <- (note1 + note2) / 2
3. DISPLAY moyenne
If we swap steps 2 and 3, we would get:
1. READ note1, note2
2. DISPLAY moyenne
3. moyenne <- (note1 + note2) / 2
At step 2, the variable moyenne doesn't exist yet: it will only be calculated at step 3! The computer cannot display a value that doesn't exist yet: it would return an error.
This rule is fundamental: a computer executes instructions in the exact order in which they are written, one by one, never guessing or reorganizing them. This is why you must always think carefully about the logical sequence of steps before writing an algorithm: first what you need (the inputs and intermediate calculations), and only then what you want to obtain (the output).

