Computer Science
Grade 6
20 min
Changing Variable Values
Changing Variable Values
Tutorial Preview
1
Introduction & Learning Objectives
Learning Objectives
Explain why a variable's value needs to change in a program.
Use the assignment operator (=) to update an existing variable's value.
Update a variable's value using its own current value (e.g., score = score + 10).
Trace the value of a variable as it changes through a sequence of code.
Predict the final output of a program where variables are updated multiple times.
Differentiate between initializing a variable and updating a variable.
How does a video game keep track of your score as it goes up and up? 🎮 Let's find out how variables can change!
You've learned that variables are like boxes for storing information. In this lesson, we'll learn the superpower of changing what's inside those boxes after they've alread...
2
Key Concepts & Vocabulary
TermDefinitionExample
VariableA named container in a program that holds a value which can change.In `score = 100`, 'score' is the variable.
Assignment Operator (=)The single equals sign symbol used to give a variable a value. It tells the computer to 'assign' the value on the right to the variable on the left.The `=` in `player_name = 'Alex'` is the assignment operator.
InitializationThe very first time a variable is created and given a value.`lives = 3` is the initialization of the 'lives' variable.
Reassignment (Updating)Changing the value of a variable that already exists. The old value is replaced with a new one.If we have `score = 50`, and then we write `score = 75`, we have reassigned 'score'.
IncrementingIncreasing the value of a nu...
3
Core Syntax & Patterns
Reassignment Syntax
variable_name = new_value
Use this to give a variable a completely new value. The computer finds the variable on the left and replaces its current value with the value on the right.
Self-Referencing Update
variable_name = variable_name + value
This pattern is used to modify a variable based on its own current value. The computer first calculates the entire right side of the equals sign using the variable's old value, and then updates the variable with the result.
Shorthand Operators
variable_name += value (or -=, *=, /=)
This is a shortcut for the Self-Referencing Update. `score += 10` does the exact same thing as `score = score + 10`, but it's shorter to type.
4 more steps in this tutorial
Sign up free to access the complete tutorial with worked examples and practice.
Sign Up Free to ContinueSample Practice Questions
Challenging
Carefully trace the value of all three variables. What is the final value of `p3`?
`p1 = 1`
`p2 = 2`
`p3 = 3`
`p1 = p2 + p3`
`p2 = p1 - p3`
`p3 = p1 - p2`
A.1
B.3
C.2
D.5
Challenging
A variable `x` holds a secret number. After the following two operations, its final value is 8. What was the original value of `x`?
`x = x + 10`
`x = x / 2`
Final value of `x` is 8.
A.6
B.18
C.4
D.16
Challenging
A variable `power` starts at 2. A piece of code, `power = power * 2`, is run three times in a row. What is the final value of `power`?
A.12
B.6
C.16
D.8
Want to practice and check your answers?
Sign up to access all questions with instant feedback, explanations, and progress tracking.
Start Practicing Free