Home / Reasoning / Track a Variable: Follow a Program Step by Step
Reasoning & Coding · Coding
Track a Variable: Follow a Program Step by Step
Follow short programs one command at a time, record each new value of a variable in a trace table, predict what the program says, and fix a reset bug.
The idea
In a game, the score starts at 0 and goes up each time you collect a coin. The program has to remember the score somewhere. That place is called a variable: a box with a name on the label and one value inside.
In this lesson, a program is written on one line, with a semicolon (;) between commands. Read it left to right. score = 0 means put 0 in the box called score. say score means show the value that is in the box right now. The sign × means multiply, as in math.
In code, = does not mean "is equal to". It means "store this". score = score + 2 tells the computer: look in the box, take the value, add 2, then put the answer back in the box. The old value is replaced. A box holds only one value at a time.
Loops work as in the robot lesson. Repeat 3 [score = score + 2] runs the command inside the brackets 3 times, in order. Each pass starts with the value the last pass left behind.
To follow a program, make a trace table. Write one row for each step and record the new value of every variable. Do not do it in your head. Coders trace programs like this to predict what a program will do and to find the exact command where it goes wrong. All programs on this page are invented examples, and every answer was checked by running the same program on a computer.
Worked examples
Example 1 · Change a value step by step
Program: coins = 5; coins = coins + 3; coins = coins - 2; say coins. What does it say?
- coins = 5: the box called coins holds 5.
- coins = coins + 3: take the 5 out, add 3, and store 8. The 5 is gone.
- coins = coins - 2: take the 8 out, subtract 2, and store 6.
- say coins shows what is in the box now.
It says 6.
Example 2 · Use a trace table for a loop
Program: score = 0; Repeat 4 [score = score + 3]; say score. What does it say?
- Make a table with two columns: Step and score. Before the loop, score is 0.
- Pass 1: 0 + 3 = 3. Pass 2: 3 + 3 = 6. Each pass uses the value from the row above.
- Pass 3: 6 + 3 = 9. Pass 4: 9 + 3 = 12. That is 4 passes, so the loop stops.
- Trace table: start 0 · pass 1 → 3 · pass 2 → 6 · pass 3 → 9 · pass 4 → 12.
It says 12.
Example 3 · Find the bug
Goal: add 5 points in each of 4 rounds and say 20. Program: points = 0; Repeat 4 [points = 0; points = points + 5]; say points. It says 5. Find the bug.
- Pass 1: points = 0, then 0 + 5 = 5.
- Pass 2: points = 0 wipes the box back to 0, then 0 + 5 = 5. Passes 3 and 4 do the same.
- The trace table shows points going 5, 5, 5, 5 instead of 5, 10, 15, 20. The reset inside the loop is the bug.
- Move points = 0 out of the loop so it runs only once, before the first pass.
Correct program: points = 0; Repeat 4 [points = points + 5]; say points. It says 20.
Common mistakes
Why this fails: In math, score = score + 2 could never be true. In code, = means store: work out the right side first, then put the answer in the box on the left.
Why this fails: A variable holds one value. When a new value is stored, the old value is gone.
Why this fails: Each pass begins with the value the previous pass left. Write every new value in the trace table and use the newest row.
Why this fails: Repeat 4 means exactly 4 passes. Number the rows in your trace table (pass 1, pass 2 …) and stop when the number matches.
Why this fails: A start value belongs before the loop. Inside the loop it runs on every pass and erases the total.
Foundation
Foundation practice
One box, one value. Store, replace and update.
1. Program: stickers = 7; stickers = 4. What is in stickers now?
- 4
- 7
- 11
- Both 7 and 4
2. Program: score = 10; score = score + 2; say score. What does it say?
- 10
- 12
- 2
- score + 2
3. Program: a = 3; b = a; a = 8; say b. What does it say?
- 8
- 11
- a
- 3
4. Program: lives = 3; lives = lives - 1; lives = lives - 1; say lives. What does it say?
- 3
- 2
- 1
- 0
5. Program: cookies = 12; cookies = cookies - 5; cookies = cookies + 4; say cookies. What does it say?
Practice
Practice practice
Use a trace table. Write one row for each pass of the loop.
6. Program: total = 0; Repeat 5 [total = total + 2]; say total. What does it say?
- 8
- 10
- 12
- 2
7. Program: n = 1; Repeat 3 [n = n × 2]; say n. What does it say?
- 2
- 7
- 8
- 16
8. Program: m = 20; Repeat 4 [m = m - 3]. Fill in a trace table: what is m after pass 1, pass 2, pass 3 and pass 4?
9. Program: steps = 1; total = 0; Repeat 3 [total = total + steps; steps = steps + 1]; say total. What does it say?
- 6
- 3
- 9
- 10
10. Program P: a = 5; a = a + 1; a = a × 2; say a. Program Q: a = 5; a = a × 2; a = a + 1; say a. What do they say?
- Both say 12
- Both say 11
- P says 11, Q says 12
- P says 12, Q says 11
11. Program: stars = 0; Repeat ? [stars = stars + 4]; say stars. How many times must the loop repeat so that it says 20?
Think
Think practice
Find the bug. Trace first, then change only the command that goes wrong.
12. Goal: add 5 points in each of 4 rounds and say 20. Program: points = 0; Repeat 4 [points = 0; points = points + 5]; say points. It says 5. Why?
- The loop runs only once.
- points = 0 inside the loop wipes the total on every pass.
- points + 5 should be points + 20.
- say points is in the wrong place.
13. Goal: count down from 10 by 2s and say 0. Program: t = 10; Repeat 4 [t = t - 2]; say t. It says 2. Find the bug and fix it.
14. Goal: add 3 + 4 + 5 and say 12. Program: total = 0; n = 3; Repeat 3 [n = n + 1; total = total + n]; say total. It says 15. Find the bug and fix it.
15. Program: c = 2; Repeat 3 [c = c + c]; say c. What does it say?
- 16
- 8
- 32
- 4
Challenge
Challenge practice
Write and repair programs. Test each one with a trace table.
16. Write a program with a variable called total and a Repeat loop that adds the same number 3 times and says 15.
17. a = 4 and b = 9. Ben wants to swap them, so he writes a = b; b = a. What do a and b hold after his program? How can he fix it?
18. Program: coins = 0; day = 1; Repeat 4 [coins = coins + day; day = day × 2]; say coins. Make a trace table for coins and day. What does it say?
Answer key
- 1. 4
The second command replaces the 7 with 4. Nothing is added, so 11 is wrong, and a variable cannot hold two values at once. - 2. 12
Take the 10 out, add 2, store 12. Saying 10 forgets the update. Saying 2 uses only the number being added. The computer works out score + 2; it does not say those words. - 3. 3
b = a copies the value 3 into b. Changing a to 8 later does not change b. b holds a number, not the letter a, and nothing is added, so 11 is wrong. - 4. 1
3 − 1 = 2, then 2 − 1 = 1. Saying 2 counts only one update, saying 3 ignores both, and 0 takes away one time too many. - 5. 11
12 − 5 = 7, then 7 + 4 = 11. Each command uses the value the command before it left in the box. - 6. 10
Five passes: 2, 4, 6, 8, 10. 8 stops one pass early, 12 runs one pass too many, and 2 forgets that each pass keeps the total from the pass before. - 7. 8
1 × 2 = 2, 2 × 2 = 4, 4 × 2 = 8. 2 is the value after only one pass, 7 comes from adding 2 each time instead of doubling, and 16 is one pass too many. - 8. 17, 14, 11, 8
Each pass subtracts 3 from the newest value: 20 → 17 → 14 → 11 → 8. - 9. 6
Trace both boxes. Pass 1: total 1, steps 2. Pass 2: total 3, steps 3. Pass 3: total 6, steps 4. 3 forgets that steps changes, 9 updates steps before adding it, and 10 runs a fourth pass. - 10. P says 12, Q says 11
P: 5 + 1 = 6, then 6 × 2 = 12. Q: 5 × 2 = 10, then 10 + 1 = 11. The same commands in a different order give a different result, so they cannot both say the same number. - 11. 5
Each pass adds 4: 4, 8, 12, 16, 20. That is 5 passes. - 12. points = 0 inside the loop wipes the total on every pass.
The loop does run 4 times, but each pass starts by resetting points to 0, so it ends at 5 every time. Adding 20 hides the bug instead of fixing it, and say points is already after the loop. - 13. The loop needs 5 passes: Repeat 5 [t = t - 2].
The trace is 8, 6, 4, 2 after 4 passes. Going from 10 to 0 by 2s takes 5 subtractions. This is an off-by-one bug. - 14. Swap the two commands inside the loop: Repeat 3 [total = total + n; n = n + 1].
As written, n becomes 4 before it is added, so the program adds 4 + 5 + 6 = 15. Adding first and then increasing n adds 3 + 4 + 5 = 12. - 15. 16
c + c doubles the newest value: 4, 8, 16. 8 stops one pass early, 32 runs one pass too many, and 4 uses the starting value 2 on every pass. - 16. Example: total = 0; Repeat 3 [total = total + 5]; say total
Trace it: 5, 10, 15. The start value goes before the loop, and the update goes inside it. - 17. Both hold 9. Fix: temp = a; a = b; b = temp. Now a is 9 and b is 4.
a = b replaces the 4 with 9 before the 4 is saved anywhere, so b = a copies 9 back into b. A third box, temp, keeps the 4 safe until b needs it. - 18. It says 15. After each pass: coins 1, day 2 · coins 3, day 4 · coins 7, day 8 · coins 15, day 16.
Each pass adds the current day value to coins, then doubles day. Keeping both columns in the table stops you from using an old value.