Variables: boxes that hold a value
Loading…
What you will learn
By the end, you can trace a variable line by line and say what value it holds at the end of a program.
In Rui's coin game, the number in the corner starts at 0 and jumps up every time a coin is caught. Where does the game keep that number? In a variable: a labelled box called score that the program can read and change.
Read the explanation
A variable is a named box that holds one value. "score = 0" puts 0 in the box. "score = score + 1" means: take what is in the box, add 1, and put the result back. The box keeps only the newest value; the old one is gone. To trace a program, write down the value after every line.
- name = value puts a new value in the box.
- The right side is worked out first, using the current value.
- A box holds one value at a time: the newest.
