Computer Science · Computational Thinking

Variables

Names that hold a value a program can change.

What's really going on

A variable is a named place where a program stores a value it needs to remember, like a score, a player’s name, or the number of lives left. The name stays the same, but the value can change as the program runs. When a game adds points to your score, it’s updating a variable. Choosing clear names for variables makes a program much easier for people to understand.

For educators

Background

A variable is a named storage location that holds a value, which can be read and changed while a program runs. Variables have a type (such as a number, text, or true/false), and their values change through assignment. Assignment in most languages (x = x + 1) is an instruction to update a value, not a mathematical equation, which can confuse students coming from algebra. Tracing tables, where students track each variable’s value step by step, are a strong unplugged tool.

Misconceptions

  • The equals sign means “is equal to,” like in math. In most programming languages it means “store this value.”
  • A variable can hold several values at once, or remembers its old values. A simple variable holds one value; assigning a new one replaces the old.
  • The variable’s name affects what it does. Names are for people; a program behaves the same whether a variable is called score or x.