First PrinciplesStart anywhere. Prove it, then move on.

Rung 3

Variables and state

A box with a name, whose contents change while the procedure runs.

Best attempted after 2. Loops and branches. Nothing stops you trying this now — the gate will tell you if you were right.

The gate

Build a Scratch project with a score that is correct after every possible sequence of actions — including the awkward ones a player would try to break it. Demonstrate it staying correct while someone else tries to break it.

Nobody checks this but you. Do it honestly and the rungs above hold; do it loosely and they will not, somewhere further up where the cause is much harder to find.

A variable is a box with a name. The name stays put; the contents change. That is the entire idea, and it is worth a rung because state — everything the machine is currently remembering — is where programs go wrong in ways that are hard to see.

Why this is on the ladder

At rung 45 you will train a network, which is nothing but a very large pile of numbers that get nudged repeatedly. Weights are variables. Training is state changing over time. If you are shaky about "the value now versus the value a moment ago", every later thing built on it is shaky too.

Scratch is here for one reason: it makes state visible. You can watch the number change on screen while the blocks run. Do not skip it because it looks like a toy — you are buying an intuition, cheaply, that is expensive to buy later.

Do this

Build something small with a score: a clicker, a catching game, a quiz. Then attack it.

  • What happens if the player clicks the same thing twice very fast?
  • What happens if they restart mid-game — does the score reset, or carry over?
  • What happens at the boundary: exactly zero, exactly the maximum?

Each of those is a question about state: what is in the box right now, and did every path through your program put the right thing there. Fix each one you find.

Where people get stuck

The classic error is confusing setting with changing: set score to 1 inside a loop keeps the score at 1 forever, while change score by 1 grows it. Read your blocks out loud — the wrong one sounds right until you say it.

The second is forgetting to reset. A variable holds its value between runs. A game that scores correctly the first time and wrongly the second time has no bug in its scoring; it has a missing reset.

Reading