Variables and Selection in Blocks

Imagine you're making a game in Scratch where a hungry cat gobbles up falling apples. Two questions come up straight away: how does the game remember your score? and how does it know when you've won? Those two questions are answered by the two biggest ideas in this lesson — a variable and an if block.

A variable is a little named box that remembers a value — and the value is allowed to change. An if block is how your program makes a decision: it only runs the blocks tucked inside it when something is true. Put them together and your program can keep track of things and make up its own mind. That's a real game!

A variable is a named box

Think of a variable as a labelled box you can put a number in. You give the box a name — like score — and inside it you keep one value. Later you can peek inside to see what's there, or swap the value for a new one. The name stays the same; only the value inside changes.

In Scratch there are two blocks you use all the time to work the box:

Here's the score box after we set it to 0 and then catch three apples. Step through it and watch the number in the box climb.

Notice that set throws away the old value and starts fresh, while change builds on what was already there. That's the whole trick of a variable: it holds on to the last value until you tell it something new.

Selection: the "if … then …" block

Selection is a fancy word for choosing. The block that does the choosing is the if … then … block. It looks like a mouth that wraps around some other blocks, and at the top it holds a condition — a question that is either true or false right now.

Some conditions you'd meet in a game:

Here's the shape of an if block, with a condition at the top and the blocks it protects tucked inside its mouth. This one checks the score box and only says "You win!" when the score has reached 10.

The two ideas working together

A variable remembers; an if block decides. Our apple game uses both at once. Every time the cat catches an apple we change score by 1 — the box keeps the running total. Then an if score = 10 then block quietly checks that box after every catch. For the first nine apples the condition is false, so nothing extra happens. On the tenth apple the box reads 10, the condition is finally true, and the "You win!" blocks fire.

Read the game loop like a story: catch an apple → change score by 1 → if score = 10 then say "You win!". The variable is the memory, and the if block is the moment of decision that watches it.

Two things beginners often get wrong:

"Variable" comes from the word vary, which means to change. That's exactly what makes it so useful: the value inside is allowed to vary. A score that could never change would be pretty boring! Mathematicians use the same word for a letter like x that can stand for different numbers — computer scientists just gave their changeable boxes friendly names like score, lives or time.

The same idea in real code

Those coloured Scratch blocks aren't just for children — grown-up programmers write the very same ideas in text. Here a variable called score starts at 0, goes up by 1 three times, and an if checks whether it has reached 3. Press Run and read the Output.

let score = 0; // set score to 0 — make the box, put 0 in it score = score + 1; // change score by 1 score = score + 1; // change score by 1 score = score + 1; // change score by 1 console.log("Score is " + score); if (score >= 3) { // the condition: is score 3 or more? console.log("You win!"); // only runs when the condition is true }

let score = 0 is set score to 0. score = score + 1 is change score by 1. And the if (...) with its curly-brace mouth { ... } is the if … then … block. Try changing a score = score + 1 line into a comment (put // in front) so the score only reaches 2 — now the condition is false and "You win!" never prints, because the inside of the if is skipped.