Lesson 4 of 5 · Open
Tracing variables
A table shows the value of every variable after each step.
At the checkout, the scanner beeps for every item, and the display shows the amount so far. So the register keeps track of a number that changes with every item. How do you write something like that in an algorithm, and how do you keep track of which number holds right now?
That is what variables are for. A variable is a place with a name that holds exactly one value, for example a number. Picture a labeled box that fits exactly one slip of paper. The register needs a variable named Total.
An assignment puts a new value into a variable, such as “set Total to 0” or “increase Total by 3.” The new value replaces the old one. Whatever was in it before is gone afterward. “Increase Total by 3” means: take the old value, add 3, and put the result back in as the new value.
Writing it down, step by step
You cannot tell from any single step what value a variable has right now. You have to write it down. A trace table has one row per step. It records the values of all variables after every step. That way, any algorithm can be checked on paper.
New terms
- variable
- a place with a name that holds exactly one value
- value
- what is in a variable right now, for example a number
- assignment
- a step that gives a variable a new value. The old value is gone afterward
- trace table
- a table that records the values of all variables after every step
Example
The receipt
You buy bread for €3, milk for €2, cheese for €6, and coffee for €14. The register works through this algorithm:
- Set Total to 0.
- Increase Total by 3.
- Increase Total by 2.
- Increase Total by 6.
- Increase Total by 14.
- Show Total.
| Step | What happens | Total |
|---|---|---|
| 1 | set to 0 | 0 |
| 2 | increase by 3 | 3 |
| 3 | increase by 2 | 5 |
| 4 | increase by 6 | 11 |
| 5 | increase by 14 | 25 |
| 6 | show | 25 |
At the end, the register shows €25. The values in between, 3, 5, and 11, are no longer in Total: each assignment replaced the value before it. In the trace table you can still see them. That is exactly why you write things down.
Quick check
Total holds 11. The next step says: increase Total by 14. What does Total hold after that?
Many programming languages write an assignment with an equals sign, such as “Total = Total + 3.” That is not an equation, because no number is equal to itself plus 3. What it means is: work with the old value and put the result into the variable as its new value. That is why assignments are written in words here.
With a loop and a branch
Example
The score in a quiz app
A quiz app gives 50 points for every right answer and takes away 20 for every wrong one. You answer four questions: right, right, wrong, right. The app works with two variables:
- Set Points to 0 and Correct to 0.
- Repeat step 3 four times.
- Take the next answer. If it is right, then increase Points by 50 and Correct by 1, else decrease Points by 20.
- Show Points and Correct.
| Pass | Answer | Points | Correct |
|---|---|---|---|
| Start | none | 0 | 0 |
| 1 | right | 50 | 1 |
| 2 | right | 100 | 2 |
| 3 | wrong | 80 | 2 |
| 4 | right | 130 | 3 |
Swapping two values
Example
Two variables swap their contents
The variable A holds 37 and the variable B holds 92. Afterward it should be the other way around. That takes a third variable, called Temp here. It keeps a value before that value gets replaced.
- Set Temp to the value of A: Temp is 37.
- Set A to the value of B: A is 92.
- Set B to the value of Temp: B is 37.
- Now A holds 92 and B holds 37.
Typical mistake
Swapping without a third variable: “Set A to the value of B, set B to the value of A.” That cannot be right: after the first step, A already holds 92, and the 37 is gone. The second step sets B to the value of A, which is 92. In the end, both variables hold 92.
Remember
Tasks
Task 1 · Input
What value does Points have after these four steps?
Task 2 · Choice
A holds 48 and B holds 90. Then this runs: set A to the value of B. What do A and B hold afterward?
Task 3 · Input
Set Number to 7 and Total to 0. Repeat 4 times: increase Total by the value of Number, then increase Number by 6. What value does Total have at the end?
Go deeper
Task 4 · Order · optional
Swap the values of A and B. This time Temp first keeps the value of B. In what order do the steps run?
- 1Set Temp to the value of B
- 2Set A to the value of Temp
- 3Set B to the value of A
Task 5 · Pairs · optional
What value does Score have after which step?
Task 6 · Input · optional