Variables are boxes that hold one thing and forget the last one. Meet the equals sign that does not mean equals, and the box that quietly turns 3.9 into 3.
A variable is a box in memory with a name written on it. It holds exactly one thing at a time, and putting something new in throws away what was there.
You met these boxes already without the word. In level 4 you traced total and count through a loop, and you watched their values get replaced over and over. Those were variables. That column of numbers you wrote down was the box's contents at each moment.
The name matters more than it looks. Memory is millions of numbered boxes and nobody can work with “box 4,829,117”. So you write score on it, and from then on the computer keeps track of which numbered box that actually is.
Here is the single biggest tripwire for beginners, and it is worth slowing right down for. In maths, x = x + 1 is nonsense — no number equals itself plus one. In programming it is completely ordinary, because the equals sign does not mean “is equal to”. It means “work out the right-hand side, then put the answer in the box on the left.”
Right to left. Always. Watch it happen:
Press step twice per run: once to work out the right side, once to store it.
When you read score = score + 10, do not say “score equals score plus ten.” Say “score BECOMES score plus ten.” Do that for a week and a whole category of confusion never happens to you. C++ even has a second symbol, ==, for when you really do mean “is it equal to” — and mixing the two up is one of the most common bugs there is.
In C++ — unlike some languages — you must say up front what kind of thing a box holds, and it can never hold anything else. That sounds like bureaucracy. It is actually the compiler agreeing to catch a whole class of your mistakes before anything runs.
Click a value, then click the box you think it belongs in.
Some of these will surprise you. That is the point.
Putting 3.9 into an int does not fail. C++ takes it, throws away everything after the dot, and stores 3. No error, no warning by default — your average is just quietly wrong. The mistakes that do not announce themselves are the expensive ones, and this is the one every class meets first.
Call out instructions: “a becomes 5.” “b becomes a plus 3.” “a becomes b.” Then ask what is in each cup.
Write the new value on a note and put it in the cup — but first take the old note out and throw it on the floor. Say what you are throwing away.
Then the classic: swap what is in the two cups. Try it with only the two cups and you will lose a value on the floor — the first one you overwrite is gone. You need a third cup. That is why every programmer knows the phrase temporary variable, and feeling why beats being told.
Storing a new value destroys the old one. There is no undo and no history.
Right side is worked out first, then stored on the left. Never read it as an equation.
A whole-number box takes 3.9 and quietly keeps 3. Silent wrongness beats loud failure — for the bug.
Not recall — these are the shapes an exam actually uses. Every answer below was produced by compiling and running the code, so if you disagree with one, the compiler is the one to believe.
Have a real go before revealing. Being wrong here is worth more than being right in three weeks.
In programming, what does score = score + 10 mean?
A box holds 5. You put 12 in it. Where does the 5 go?
You put 3.9 into a box that holds whole numbers. What is in the box?
← back to all 12 levels · stuck on anything? ask peter.