Tutorpip← About this lessonOpen the sheet →Download PDF
Ages 8+ · Coding · Variables and data
Counting stars with a variable
1when a star lands:2 put stars + 1 into stars
A counter adds one to its old value.
The big ideaA variable can count. Each time, it takes its old value and adds one.

How it works

A variable can count. Each time, it takes its old value and adds one.

A counter starts at zero. The new value is the old value plus one.

A rule that puts 1 in the box every time is a bug. It forgets what was in the box, so it always gives one.

Between players, put zero back in the box, so everyone starts fresh.

put 1 in stars• 1st star: 1• 2nd star: 1• 3rd star: 1put stars + 1 into stars• 1st star: 1• 2nd star: 2• 3rd star: 3
Two rules, three stars.

Example 1: The bug

  1. Tavi's rule says: put 1 in the box called stars.
  2. The first star lands, and the screen shows 1.
  3. A second star lands, but the screen still shows 1. The rule forgets what was in the box.

Example 2: The fix

  1. Take the old value out of the box, add one, and put the new value back.
  2. The box holds 2, so 2 + 1 = 3.
  3. The box now holds 3.

Example 3: Counting up

  1. The box starts at 0.
  2. The first star: 0 + 1 = 1. The second star: 1 + 1 = 2. The third star: 2 + 1 = 3.
  3. The screen counts up, one, then two, then three.

Example 4: A new player

  1. Between players, Mina puts 0 in the box called stars.
  2. The next player's first star: 0 + 1 = 1.
  3. Everyone starts fresh.

Words to know

variable
A box with a name that holds a value.
counter
A variable that counts up.
old value
What is in the box before the star lands.
new value
The old value plus one.

Watch out

Try it

  1. The box holds 4 and the fixed rule adds one. What does it hold now?
  2. The rule put 1 in stars runs three times. What does the box hold?
  3. What number does a counter start at?
  4. The box holds 2, and we add three. What does it hold?
Answers: 1. 5 · 2. 1 · 3. Zero · 4. 5
Try at homeWrite zero on a paper. Each time you do a jump, cross out the number and write the old number plus one. After ten jumps, say what the variable holds. Then try the bug: write one every time, and see what happens.
For grown-upsPlay the Mission: write zero on a paper. Each time your child does a jump, they cross out the number and write the old number plus one. After ten jumps, ask what the variable holds. Then try the bug: write one every time, and see what happens.
Open these notes on screenScan the code, or go to tutorpip.com.