Variables: Named Containers That Remember Values in Code
Byte, a friendly glowing robot guide, stands in a colorful game room holding a stack of labeled boxes, writing numbers on sticky notes and swapping them in and out of each box with a big grin
- Define a variable as a named container that stores a value a program can read and change
- Trace a simple program by hand to predict the value stored in a variable after each step
- Distinguish between the variable's name (the label) and the variable's value (what is stored inside)
- Write a variable assignment statement and explain what it does in plain words
Key terms
- Variable
- A named container that holds a value a program can read and change
- Name
- The unchanging label on the outside of a variable
- Value
- The current contents stored inside a variable
- Assignment statement
- A line that stores a value into a variable
Name Versus Value
A variable has two distinct parts that beginners often blur together. The name is a fixed label, like score, that never changes once chosen and simply tells you the purpose of the container. The value is whatever is stored inside right now, and it can change many times as the program runs. Because the name alone never reveals the current value, you cannot guess the contents just by reading the label; you must trace each assignment in order to know what the box holds at any moment.
Tracing With Boxes
Tracing means walking through code one line at a time while tracking each variable's value. A reliable habit is to draw a small box, write the variable name outside it, and write the current value inside. Each time you reach an assignment, erase the old value and write the new one. For an update like score = score + 10, you read the inside first, do the math, then replace the contents. This simple drawing turns confusing code into a clear, step-by-step story you can follow.
Worked examples
Trace lives = 3 then lives = lives - 1
- The first statement stores 3 in the box named lives.
- The second statement reads the current value 3 first.
- Compute 3 - 1, which is 2.
- Store 2 back into lives, replacing the 3.
Answer: lives holds 2
Trace coins starting at 10 with two additions of 5
- Start with coins holding 10.
- First update coins = coins + 5 reads 10 and stores 15.
- Second update coins = coins + 5 reads 15 and stores 20.
- Each assignment overwrites the previous total.
Answer: coins holds 20
Activity
Match each code statement to what it does to the variable named 'score'
Practice
Predict the value of points after points = 2 then points = points + 6.
Describe in your own words why a variable's name does not reveal its value.
Common mistakes to avoid
- A variable's value can never changeA variable can be reassigned anytime; a new assignment replaces the stored value with a new one.
- The variable name tells you the stored valueThe name only labels the container; you must trace assignments to find the current value.
Check your understanding
A program runs these two lines in order: lives = 3, then lives = lives - 1. What value is stored in lives after both lines run?
Which of the following best describes what the name of a variable tells you?
A program starts with: coins = 10. Then it runs: coins = coins + 5. Then it runs: coins = coins + 5 again. What value does coins hold now?
A student says: 'Once you set a variable to 7, the value is always 7 and can never change.' What is wrong with this?
Recap
A variable is a named container whose fixed name labels it and whose value can change as the program runs. To know the current value you trace each assignment in order, and drawing a box with the name outside and value inside makes tracing reliable.
Reflect
When tracing code, what mistakes do you think are easiest to make and how could you avoid them?