Variables & Scorekeeper Lab

Explore variables as numbered jars. Run add, subtract, and reset operations one step at a time to see how each operation changes a variable's value. Then design your own scoring game.

Guided Experiment: Variables Investigation

What happens to a variable when you add, subtract, or reset it? Write a prediction before running the first program.

Write your hypothesis in the Lab Report panel, then click Next.

Controls

Explore the three tabs to learn how variables work, watch a basketball score program run, and design your own scoring game.

What Is a Variable?

A variable is like a jar that holds a number. You can add to it, subtract from it, or reset it to 0. Run the blocks below one at a time to see how each operation changes the jar's value.

0
Player 1
0
Player 2

Program (0 of 5 steps run)

1.Add 10 to Player 12.Add 5 to Player 23.Add 20 to Player 14.Subtract 3 from Player 25.Reset Player 1 to 0

Data Table

(0 rows)
#
0 / 500
0 / 500
0 / 500

Reference Guide

What Is a Variable?

A variable is a named storage location that holds a value. Think of it like a labeled jar that can hold exactly one number at a time.

The variable has a name (like "Player 1" or "Score") and a current value (like 0, 15, or 42). You can read the value, change it, or reset it.

Key idea: a variable's value can change over time, but the name stays the same. That is what makes it a "variable."

The Three Operations

Three operations can change a variable's value:

  • Add - increases the value by a given amount
  • Subtract - decreases the value (but never below 0)
  • Reset - sets the value back to 0 no matter what it was

Each operation takes the current value, applies a change, and stores the new value. The jar is updated with each step.

Reading a Program

A program is a list of blocks (operations) that run in order, one at a time. Each block says what to do and which variable to change.

To read a program, start at block 1, apply it, then move to block 2, and so on. The variable's value after each block depends on what it was before.

Try this: before clicking "Run Next Block," predict what the new value will be. Check if you were right!

Variables in Real Games

Every video game and board game app uses variables to track scores, lives, coins, health points, and more.

When you score a point, the game adds 1 to a "score" variable. When you lose a life, it subtracts 1 from a "lives" variable. When a new level starts, it resets the "time" variable to 60.

  • Score - starts at 0, goes up when you earn points
  • Lives - starts at 3, goes down when you get hit
  • Health - reset to full at the start of each round