Study CommonsRead · reason · practise

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.

Learning objectiveTrace a program one step at a time, predict how swapping the order of steps changes the result, and choose the step or order that makes a program reach a goal.

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

  1. Start: x = 3.
  2. x = 3 * 2 = 6.
  3. x = 6 + 1 = 7.

It says 7.

Example 2 · Swap the steps

x = 3; x = x + 1; x = x * 2; say x

  1. Start: x = 3.
  2. x = 3 + 1 = 4.
  3. 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.

  1. Try add first: 4 + 1 = 5, then 5 * 2 = 10.
  2. Try multiply first: 4 * 2 = 8, then 8 + 1 = 9.
  3. 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.

  1. First order: 5 + 3 = 8, then 8 + 4 = 12.
  2. Second order: 5 + 4 = 9, then 9 + 3 = 12.
  3. Both end at 12 because addition is the same kind of step.

Both say 12.

Common mistakes

Doing the steps in your head without writing x after each step.

Why this fails: Skipping ahead causes slips. Write the value of x after every step.

Assuming order never matters.

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.

Changing the start value instead of fixing the step.

Why this fails: A bug is the first step that does not match the plan. Fix that step before changing the start.

Forgetting the last step.

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.

Trace

1. Trace: x = 3; x = x * 2; x = x + 1; say x. What does it say?

  1. 8
  2. 6
  3. 7
  4. 9
Trace

2. Trace: x = 3; x = x + 1; x = x * 2; say x. What does it say?

  1. 8
  2. 7
  3. 6
  4. 5
Trace

3. Trace: x = 10; x = x - 4; x = x * 3; say x. What does it say?

  1. 26
  2. 30
  3. 6
  4. 18
Trace

4. Trace: x = 10; x = x * 3; x = x - 4; say x. What does it say?

  1. 18
  2. 26
  3. 14
  4. 34
Does order matter?

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?

  1. A says 15 and B says 20
  2. Both say 20
  3. A says 20 and B says 15
  4. Both say 15

Practice

Practice practice

Longer programs and goals.

Trace

6. Trace: x = 2; x = x + 3; x = x * 4; x = x - 6; say x. What does it say?

  1. 20
  2. 14
  3. 26
  4. 8
Trace

7. Trace: x = 6; x = x - 2; x = x * 5; x = x + 1; say x. What does it say?

  1. 21
  2. 29
  3. 19
  4. 31
Trace

8. Trace: x = 4; x = x * 2; x = x * 2; x = x + 3; say x. What does it say?

  1. 11
  2. 24
  3. 19
  4. 16
Reorder to get a goal

9. Start with x = 4. Which program says 10?

  1. x = 4; x = x * 2; x = x + 1; say x
  2. x = 4; x = x + 2; x = x * 2; say x
  3. x = 4; x = x * 3; say x
  4. x = 4; x = x + 1; x = x * 2; say x
Predict

10. Start with x = 7. Which program leaves x unchanged at the end?

  1. x = x + 1; x = x * 1
  2. x = x + 0; x = x * 1
  3. x = x + 0; x = x * 0
  4. x = x * 2; x = x + 0
Find the missing step

11. Start with x = 3. Which missing step makes the program say 8? x = 3; x = x * 3; ___; say x

  1. x = x + 1
  2. x = x - 2
  3. x = x - 1
  4. x = x * 2

Think

Think practice

Decide when order matters and fix bugs.

Trace

12. Trace: x = 1; x = x + 1; x = x * 2; x = x + 1; x = x * 2; say x. What does it say?

  1. 6
  2. 10
  3. 5
  4. 9
Does order matter?

13. Which pair of steps gives the same answer in either order, whatever x starts as?

  1. add 3 then add 4
  2. add 3 then multiply by 4
  3. subtract 2 then multiply by 3
  4. multiply by 2 then add 1
Everyday order

14. Which order makes a good peanut-free sandwich?

  1. Close sandwich, spread sunflower butter, get bread, wash hands
  2. Wash hands, get bread, spread sunflower butter, close sandwich
  3. Get bread, close sandwich, spread sunflower butter, wash hands
  4. Spread sunflower butter, wash hands, close sandwich, get bread
Find the bug

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?

  1. Swap the two steps
  2. Change the start to x = 6
  3. Change the add step to x = x + 5
  4. Change the multiply to x = x * 3

Answer key

  1. 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. 2. 8
    x starts at 3, becomes 4, then doubles to 8. The same two steps in the other order give 7.
  3. 3. 18
    10 - 4 = 6, then 6 * 3 = 18. 26 comes from multiplying first (30) and subtracting 4.
  4. 4. 26
    10 * 3 = 30, then 30 - 4 = 26. 18 is the answer if the steps are swapped.
  5. 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. 6. 14
    2 + 3 = 5, 5 * 4 = 20, 20 - 6 = 14. Stopping after the multiply gives 20.
  7. 7. 21
    6 - 2 = 4, 4 * 5 = 20, 20 + 1 = 21. Using 6 * 5 = 30 first would give a different answer.
  8. 8. 19
    4 * 2 = 8, 8 * 2 = 16, 16 + 3 = 19. 16 forgets the last step.
  9. 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. 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. 11. x = x - 1
    3 * 3 = 9, and 9 - 1 = 8. Adding 1 gives 10, subtracting 2 gives 7 and doubling gives 18.
  12. 12. 10
    1 + 1 = 2, 2 * 2 = 4, 4 + 1 = 5, 5 * 2 = 10. Stopping early gives 5.
  13. 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. 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. 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.

Related practice