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:

  1. Set Total to 0.
  2. Increase Total by 3.
  3. Increase Total by 2.
  4. Increase Total by 6.
  5. Increase Total by 14.
  6. Show Total.
StepWhat happensTotal
1set to 00
2increase by 33
3increase by 25
4increase by 611
5increase by 1425
6show25

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:

  1. Set Points to 0 and Correct to 0.
  2. Repeat step 3 four times.
  3. Take the next answer. If it is right, then increase Points by 50 and Correct by 1, else decrease Points by 20.
  4. Show Points and Correct.
PassAnswerPointsCorrect
Startnone00
1right501
2right1002
3wrong802
4right1303

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.

  1. Set Temp to the value of A: Temp is 37.
  2. Set A to the value of B: A is 92.
  3. Set B to the value of Temp: B is 37.
  4. 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

A variable is a named place for exactly one value. An assignment replaces the old value, and a table with one row per step shows every value.

Tasks

Three tasks complete the lesson. The order does not matter, the text above stays where it is.

Task 1 · Input

What value does Points have after these four steps?

Enter the result.

Points =

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?

One answer is right.

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?

Enter the result.

Total =

Go deeper

Three more tasks, optional. They do not count for completing the lesson.

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?

Drag the rows into the right order.

  1. 1Set Temp to the value of B
  2. 2Set A to the value of Temp
  3. 3Set B to the value of A

Task 5 · Pairs · optional

What value does Score have after which step?

Drag every card to its place, or tap it and then the place.

  • after step 1
  • after step 2
  • after step 3
  • after step 4

Task 6 · Input · optional

A store gives €8 off on purchases of €55 or more. The register: set Total to 0, increase Total by 26, by 18, and by 13. If Total is at least 55, then decrease Total by 8. What does the register show?

Enter the result.

€

Back to the chapter