cadence_learn
think_like_a_programmer / level 09 of 12 ▶ 14 min

a box with a name on it.

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.

after this level you'll be able to
  • Read an assignment right-to-left and say what ends up in the box
  • Explain why x = x + 1 is ordinary rather than nonsense
  • Predict what happens when a value does not match its box’s kind
the whole idea

a box, a name on the box, and one thing inside

the one sentence

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.

the thing that confuses everyone

the equals sign is a lie

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
score = score + 10 RIGHT SIDE FIRST — THEN STORED ON THE LEFT BEFORE 0 score step 1 · look in the box → 0 step 2 · work out 0 + 10 → 10 step 3 · put 10 in the box AFTER 10 score 0 gone Say it as “score BECOMES score plus ten.” Never “equals” — that one word is the source of a whole category of beginner confusion.
The old value is not moved or remembered. It is destroyed. That is why swapping two variables needs a third one — the first thing you overwrite is gone for good.

Right to left. Always. Watch it happen:

▤ lab 09a · the box

Press step twice per run: once to work out the right side, once to store it.

score
0
score = score + 10
Press step. The right-hand side gets worked out first, using whatever is in the box right now.
say it out loud the right way

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.

◈ ask an ai about this

“Why does x = x + 1 make sense in programming when it is impossible in maths?”

chatgpt ↗ claude ↗

the other half

every box has a kind

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.

▤ lab 09b · what kind of thing is it

Some of these will surprise you. That is the point.

int
whole numbers only
double
numbers with decimals
string
text
Pick a value above, then pick the box you think it goes in.
the silent one

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.

◈ ask an ai about this

“Why does C++ make me declare a type for every variable, and what happens if I put 3.9 into an int?”

chatgpt ↗ claude ↗

do this together

the shoebox game

☶ two people · 10 minutes · sticky notes and a couple of cups

Two cups, two labels. One of you is memory. Nothing goes in a cup without being read out.

person a — the program

Call out instructions: “a becomes 5.” “b becomes a plus 3.” “a becomes b.” Then ask what is in each cup.

person b — memory

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.

what to keep

three things worth remembering

01

One box, one thing

Storing a new value destroys the old one. There is no undo and no history.

02

“=” means becomes

Right side is worked out first, then stored on the left. Never read it as an equation.

03

The kind is a promise

A whole-number box takes 3.9 and quietly keeps 3. Silent wrongness beats loud failure — for the bug.

check yourself

3 questions before you move on

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.

▢ check yourself3 questions

Have a real go before revealing. Being wrong here is worth more than being right in three weeks.

01

In programming, what does score = score + 10 mean?

02

A box holds 5. You put 12 in it. Where does the 5 go?

03

You put 3.9 into a box that holds whole numbers. What is in the box?

answered: 0 of 3right first time: 0
Stuck? Peter reads these personally and replies to your email.
Ask Peter →