Using Variables to Store and Change Values
Byte the robot guide stands at a giant scoreboard in a game arcade, sliding numbered tiles into labeled slots on the board and swapping them out as scores change during a live match.
- Explain what a variable is and why programs need them.
- Identify the two core parts of a variable (its name and its current value) and recognize that every value has a type (integer, string, boolean, etc.).
- Predict what value a variable holds after a series of assignment statements.
- Compare reading a variable with updating a variable and describe how each affects the stored value.
- Identify real-world examples where a program would need to store and update a value.
Key terms
- Variable
- A named container holding a value a program can read and update
- Assigning
- Storing a value into a variable, replacing the old contents
- Updating
- Reading a variable's value, computing a new one, and storing it back
- Reading
- Looking at a variable's stored value without changing it
Reading Does Not Change It
Reading and writing are two very different actions on a variable. Reading simply looks at the stored value and leaves it untouched, so you can read the same variable many times in a row and get the same answer each time. Writing, which means assigning a new value, is the only action that changes what is stored. A program like print(score) twice prints the identical value both times, because neither print performs an assignment; only a line with the variable on the left side alters the box.
The Update Pattern
The most common way variables change is the update pattern, written like score = score + 5. The computer evaluates the right side first, reading the current value and computing a new total, then assigns that total back to the same container. The previous value is gone, replaced by the new one. This pattern explains how a running game keeps a score climbing: each time points are earned, the program reads the old score, adds to it, and stores the fresh total back in place.
Worked examples
Trace points = 8 then points = points + 4
- The first line stores 8 in points.
- The second line reads the current value 8.
- Compute 8 + 4, which is 12.
- Store 12 back in points, replacing the 8.
Answer: points holds 12
Decide if reading a variable twice changes it
- Read score, which holds 7, and get 7.
- No assignment happened, so the stored value is still 7.
- Read score again and get 7 once more.
- Reading never alters the stored value, so both reads match.
Answer: Both reads give 7; reading does not change the value
Activity
Drag each value tile into the correct variable box after each assignment statement runs, one step at a time.
Practice
Predict the value of gold after gold = 5 then gold = gold + 3.
Describe the difference between reading and updating a variable in your words.
Common mistakes to avoid
- Reading a variable uses up its valueReading only looks at the stored value and never changes or removes it.
- A variable is a fixed number that never changesA variable can be reassigned while the program runs, unlike a constant value.
Check your understanding
A program runs these two lines in order: points = 8 points = points + 4 What value does `points` hold after both lines run?
Which of the following best describes what a variable is in a program?
A student says: "If I read a variable twice in a row without any assignment in between, the second read will give me a different value than the first." Is the student correct?
Recap
A variable is a named container that a program can read and update. Reading looks at the value without changing it, while assigning replaces it. The update pattern reads the current value, computes a new one, and stores that result back in the same container.
Reflect
What real-world counter or score could you model with a variable that updates?