Module 2 · Values and variables
Variables are labeled storage
Create a local variable, distinguish every part of an assignment, and update its value.
What you will learn
- Create a local variable
- Identify an identifier, assignment operator, and value
- Reassign and update a number
- variable
- A named place that refers to a value.
- assignment
- Giving a value to a variable with the equals operator.
- identifier
- A name used to refer to something in code.
Loading saved progress…
Understand
Read the idea and follow a worked example.
Why this matters
A game may need to remember that a player has 25 coins. The number 25 is a value. A variable gives that value a useful name.
The mental model
Think of coins as a label attached to the current number. local creates the variable for this script scope, coins is its name, = assigns, and 25 is the stored number.
Reassignment changes which value the name refers to. Updating from the previous value uses that current value in a new calculation.
New words and symbols
--!strict asks Studio to check your code for type mistakes. It is an instruction for the editor and produces no Output message.
print(...) calls the built-in print function. Put the values to show inside its parentheses; separate multiple values with commas. Read the result in Studio's Output panel.
local is read as “local”: it creates a name that is available only in the current code scope.
+= is read as “plus equals”: it reads the current value, adds, then stores the result back.
= is read as “is assigned”: it evaluates the right side and stores that value in the name on the left.
A tiny example
--!strictlocal coins = 25print(coins)coins = 30coins += 5print(coins)Line by line
- 3
local creates a variable; coins is the identifier; = assigns; 25 is the number value.
- 4
The variable's current value is sent to Output.
- 6
Reassignment replaces 25 with 30.
- 7
+= adds 5 to the current value and stores the result.
- 8
The second print reads the updated value.
Read the code aloud
Read one statement at a time: name the value being read, what the operation does, and where its result goes. Keep identifiers exactly as written so you can find them again in the code.
Pause at calls, conditions, and loops. Explain when the body runs before moving to the next statement; use the line notes above when a symbol is unfamiliar.
Execution trace
Line 3 creates coins with the value 25; line 4 prints 25.
Line 6 replaces the current value with 30. Line 7 reads 30, adds 5, and stores 35.
Line 8 prints 35. The first Output entry remains 25 because an old print entry does not update when a variable changes.
State before and after
Before: coins has not been declared by this run.
After: coins ends at 35 and Output records 25, then 35.
Predict before running
What will the second print call display after both updates?
Try it
Mark each step after you have tried it.
Common mistake
local coins = "25"This stores text rather than a number. Arithmetic belongs on numeric values, so remove the quotes when the value represents a coin count.
Checkpoint
Submit once you have considered every question. Retries are unlimited.
Recap
- local creates a local variable.
- The equals operator assigns a value to a name.
- Reassignment and compound updates change the current value.
Next: Next, you will focus on strings and the details that make text values reliable.
Studio Companion
Not connected
Connect once. Compatible challenges will prepare automatically as you reach them.