Labeled Boxes: How Programs Remember With Variables
Byte, a friendly glowing robot, stands beside a wall of labeled storage boxes in a bright workshop, sliding new values into each box while a screen displays the variable names and their live values updating in real time.
- Define a variable as a named place that stores a value a program can reuse
- Create variables with clear names and assign starting values to them
- Update a variable so it holds a new value, replacing the old one
- Classify common data as the types number, string (text), or boolean (true/false)
Key terms
- Variable
- A named place that stores a value a program can reuse
- Assignment
- Storing a value into a variable, replacing any old value
- String
- A value made of text, written inside quotation marks
- Boolean
- A value that is either true or false
- Data type
- The category of a value that decides what operations are allowed
Assigning and Updating
Creating a variable means assigning it a starting value, like score = 0, which makes a named box and drops a value inside. Updating means assigning again, and the new value completely replaces the old one because a box holds only one value at a time. A pattern like score = score + 10 first reads the current contents, computes a new total, then stores that total back. Reading the right side before writing the left side is the key to tracing these update statements correctly.
Why Types Matter
Every value has a type that tells the program what it can do with it. Numbers like 42 support arithmetic such as adding and subtracting. Strings are text in quotes, like "Maya", and you can join them but not do number arithmetic on them. Booleans are true or false and drive yes/no decisions. Crucially, 42 and "42" are different: one is a number you can add, the other is text the program treats as characters, so the same-looking value behaves very differently depending on its type.
Worked examples
Trace score = 5 then score = 12
- The first statement stores 5 in the box named score.
- The second statement assigns a brand new value, 12.
- Assignment replaces the contents, so 5 is discarded.
- The box now holds only 12.
Answer: score holds 12
Trace score = score + 10 while score holds 30
- Read the right side first using the current value 30.
- Compute 30 + 10, which is 40.
- Store the result 40 back into score.
- The old value 30 is replaced by 40.
Answer: score holds 40
Activity
Match each stored value to the correct data type it belongs to
Practice
Predict the value of lives after lives = 4 then lives = lives - 1.
Label each as number, string, or boolean: 7, "yes", true.
Common mistakes to avoid
- A variable holds two values at onceA variable holds only one value at a time, and each assignment replaces the previous one.
- The number 42 and the text 42 are identicalThe number 42 supports arithmetic, while "42" is text the program cannot add like a number.
Check your understanding
A program runs score = 5, then later runs score = 12. What value is stored in score now?
Which line correctly stores text (a string) in a variable?
The line score = score + 10 runs while score holds 30. What does it do?
Why does it matter that 42 is a number type but "42" is a string type?
Recap
A variable is a named box that stores one value at a time, and assignment replaces its contents. Update patterns read the current value before writing back. Each value has a type, such as number, string, or boolean, which decides what operations are allowed.
Reflect
How would you describe the difference between a variable's name and its value?