Home / Reasoning / Order of Steps: Why Sequence Matters in a Program
Reasoning & Coding · Coding
Order of Steps: Why Sequence Matters in a Program
Trace short programs step by step, see why changing the order of steps changes the answer, and fix a program by reordering or replacing one step.
The idea
A program runs its steps in order, from the first to the last. Changing the order can change the answer, just as putting on your boots before your socks does not work the same as socks before boots.
In these programs, x = 3 means x starts as 3. x = x * 2 means take x, multiply by 2 and store the new value in x. say x shows the value at the end. All programs here use only whole numbers and are invented examples.
To trace, write the value of x after every step. Do not skip ahead. The answer is the last value you wrote.
Order matters when you mix different kinds of steps: add then multiply gives a different result from multiply then add. Order does not matter when you repeat the same kind of step, such as add 3 then add 4.
Worked examples
Example 1 · Trace in order
x = 3; x = x * 2; x = x + 1; say x
- Start: x = 3.
- x = 3 * 2 = 6.
- x = 6 + 1 = 7.
It says 7.
Example 2 · Swap the steps
x = 3; x = x + 1; x = x * 2; say x
- Start: x = 3.
- x = 3 + 1 = 4.
- x = 4 * 2 = 8.
It says 8. The same steps in the other order gave 7 before.
Example 3 · Reach a goal
Start with x = 4. Order the steps add 1 and multiply by 2 so the program says 10.
- Try add first: 4 + 1 = 5, then 5 * 2 = 10.
- Try multiply first: 4 * 2 = 8, then 8 + 1 = 9.
- Only the first order reaches 10.
Add 1, then multiply by 2.
Example 4 · Do two additions care about order?
Start with x = 5. Compare add 3 then add 4 with add 4 then add 3.
- First order: 5 + 3 = 8, then 8 + 4 = 12.
- Second order: 5 + 4 = 9, then 9 + 3 = 12.
- Both end at 12 because addition is the same kind of step.
Both say 12.
Common mistakes
Why this fails: Skipping ahead causes slips. Write the value of x after every step.
Why this fails: Order matters when the steps are different kinds, such as add and multiply. It can be ignored only when the steps are the same kind.
Why this fails: A bug is the first step that does not match the plan. Fix that step before changing the start.
Why this fails: The program says the value after the last step. Stop only when the steps are used up.
Foundation
Foundation practice
Trace short programs.
1. Trace: x = 3; x = x * 2; x = x + 1; say x. What does it say?
- 8
- 6
- 7
- 9
2. Trace: x = 3; x = x + 1; x = x * 2; say x. What does it say?
- 8
- 7
- 6
- 5
3. Trace: x = 10; x = x - 4; x = x * 3; say x. What does it say?
- 26
- 30
- 6
- 18
4. Trace: x = 10; x = x * 3; x = x - 4; say x. What does it say?
- 18
- 26
- 14
- 34
5. Both programs start with x = 5. Program A: add 5, then multiply by 2. Program B: multiply by 2, then add 5. What do A and B say?
- A says 15 and B says 20
- Both say 20
- A says 20 and B says 15
- Both say 15
Practice
Practice practice
Longer programs and goals.
6. Trace: x = 2; x = x + 3; x = x * 4; x = x - 6; say x. What does it say?
- 20
- 14
- 26
- 8
7. Trace: x = 6; x = x - 2; x = x * 5; x = x + 1; say x. What does it say?
- 21
- 29
- 19
- 31
8. Trace: x = 4; x = x * 2; x = x * 2; x = x + 3; say x. What does it say?
- 11
- 24
- 19
- 16
9. Start with x = 4. Which program says 10?
- x = 4; x = x * 2; x = x + 1; say x
- x = 4; x = x + 2; x = x * 2; say x
- x = 4; x = x * 3; say x
- x = 4; x = x + 1; x = x * 2; say x
10. Start with x = 7. Which program leaves x unchanged at the end?
- x = x + 1; x = x * 1
- x = x + 0; x = x * 1
- x = x + 0; x = x * 0
- x = x * 2; x = x + 0
11. Start with x = 3. Which missing step makes the program say 8? x = 3; x = x * 3; ___; say x
- x = x + 1
- x = x - 2
- x = x - 1
- x = x * 2
Think
Think practice
Decide when order matters and fix bugs.
12. Trace: x = 1; x = x + 1; x = x * 2; x = x + 1; x = x * 2; say x. What does it say?
- 6
- 10
- 5
- 9
13. Which pair of steps gives the same answer in either order, whatever x starts as?
- add 3 then add 4
- add 3 then multiply by 4
- subtract 2 then multiply by 3
- multiply by 2 then add 1
14. Which order makes a good peanut-free sandwich?
- Close sandwich, spread sunflower butter, get bread, wash hands
- Wash hands, get bread, spread sunflower butter, close sandwich
- Get bread, close sandwich, spread sunflower butter, wash hands
- Spread sunflower butter, wash hands, close sandwich, get bread
15. A program should say 15 starting from x = 5: x = 5; x = x * 2; x = x + 1; say x. It says 11. Which fix works?
- Swap the two steps
- Change the start to x = 6
- Change the add step to x = x + 5
- Change the multiply to x = x * 3
Answer key
- 1. 7
x starts at 3, doubles to 6, then 7. Choosing 8 means the add was done before the multiply, which is the other order. - 2. 8
x starts at 3, becomes 4, then doubles to 8. The same two steps in the other order give 7. - 3. 18
10 - 4 = 6, then 6 * 3 = 18. 26 comes from multiplying first (30) and subtracting 4. - 4. 26
10 * 3 = 30, then 30 - 4 = 26. 18 is the answer if the steps are swapped. - 5. A says 20 and B says 15
A: 5 + 5 = 10, then 10 * 2 = 20. B: 5 * 2 = 10, then 10 + 5 = 15. Order changes the result when you mix add and multiply. - 6. 14
2 + 3 = 5, 5 * 4 = 20, 20 - 6 = 14. Stopping after the multiply gives 20. - 7. 21
6 - 2 = 4, 4 * 5 = 20, 20 + 1 = 21. Using 6 * 5 = 30 first would give a different answer. - 8. 19
4 * 2 = 8, 8 * 2 = 16, 16 + 3 = 19. 16 forgets the last step. - 9. x = 4; x = x + 1; x = x * 2; say x
4 + 1 = 5, 5 * 2 = 10. The other order gives 9, adding 2 first gives 12, and 4 * 3 is 12. - 10. x = x + 0; x = x * 1
Adding 0 and multiplying by 1 change nothing. Multiplying by 0 would make x 0, and the others change x. - 11. x = x - 1
3 * 3 = 9, and 9 - 1 = 8. Adding 1 gives 10, subtracting 2 gives 7 and doubling gives 18. - 12. 10
1 + 1 = 2, 2 * 2 = 4, 4 + 1 = 5, 5 * 2 = 10. Stopping early gives 5. - 13. add 3 then add 4
Two additions can swap places (x + 3 + 4 = x + 4 + 3). A mix of add and multiply, or subtract and multiply, changes the answer when swapped. - 14. Wash hands, get bread, spread sunflower butter, close sandwich
You wash hands first, then get the bread, then spread, then close. Closing before spreading would leave the filling out. - 15. Change the add step to x = x + 5
5 * 2 = 10, and 10 + 5 = 15. Swapping the steps gives 12, starting at 6 gives 13 and multiplying by 3 gives 16.