If the computer runs price ← 4000, then price ← 5000, what does price hold now? Both numbers, or only one? Guess first, the answer is in the second section.
That small question holds nearly everything you need to know about variables. The rest is one thing that often confuses new programmers: why swapping what two names hold needs a third name.
← stores a value in a name
price ← 5000
PRINT price
PRINT "price"
5000
price
- Read
price ← 5000as "price gets 5000". The arrow points at the place where the value is kept. - A name that holds a value is called a variable. Picture a labelled jar in a food stall's display case: the label says
price, and it holds 5000. PRINT priceshows what it holds.PRINT "price"shows the word price, because it's in quotes.←hard to type?<-and=read the same. In the exercise editor,←is also in the row of symbols.
A variable name may hold letters, digits and underscores, and must start with a letter. No spaces, so write icedTeaPrice or iced_tea_price. In JavaScript later, that line becomes let price = 5000. In Python, price = 5000.
One name, one value
price ← 4000
price ← 5000
PRINT price
5000
That answers the question at the top. A variable holds only one value. When price ← 5000 runs, 4000 is thrown away and replaced by 5000. No history is kept.
A name can also be used to work out its own new value:
stock ← 12
stock ← stock - 3
PRINT stock
9
In maths, stock = stock - 3 makes no sense. Here it's an instruction, not an equation. The computer works out the right side first with the old value (12 - 3), and only then stores the result, 9, in stock.
Copying the value, not linking the names
price ← 5000
oldPrice ← price
price ← 6000
PRINT oldPrice, price
5000 6000
oldPrice ← price copies what price holds at that moment, 5000, into oldPrice. After that the two have nothing to do with each other. When price changes to 6000, oldPrice stays 5000.
Swapping two variables
You have two glasses: glass A holds tea, glass B holds coffee. How do you swap what's in them? You need a third, empty glass. Guess first what gets printed when there's no third glass:
glassA ← "tea"
glassB ← "coffee"
glassA ← glassB
glassB ← glassA
PRINT glassA, glassB
coffee coffee
The third line pours the coffee into glass A, and the tea is gone right then. The fourth line only copies that coffee back into glass B. Keep what glass A holds somewhere else first:
glassA ← "tea"
glassB ← "coffee"
temp ← glassA
glassA ← glassB
glassB ← temp
PRINT glassA, glassB
coffee tea
temp holds the tea while glass A is filled with coffee, then hands it to glass B. Three lines, and their order matters.
⚠️ Names the computer doesn't recognise
- A typo.
price ← 5000thenPRINT pircestops with "Line 2:pircehas never been given a value." Luckily the computer often guesses what you meant: "Did you meanprice?" - Capitals.
PRINT Priceafterprice ← 5000stops with "Line 2:Priceis notprice. Upper and lower case count." To the computer those are two names. - A space in a name.
iced tea price ← 5000stops with "Line 1: a name can't have spaces in it." and then "Join it up:iced_tea_priceoricedTeaPrice." - Used before it has a value. If your first line is
total ← total + 5000, the computer stops with "Line 1:totalis used before it has a value." Give it a value first, liketotal ← 0, then add to it. - The arrow the wrong way round.
5000 ← pricestops with "Line 1: the line should end after5000, but←follows." The variable's name always goes left of the arrow, the value on the right.
TL;DR: price ← 5000 stores 5000 in the variable price (<- and = work too). A variable holds only one value: a new value throws the old one away. stock ← stock - 3 works out the right side first with the old value, then stores the result. a ← b copies what b holds at that moment, it doesn't link the two. Swapping two variables needs a third one: temp ← a, a ← b, b ← temp. Names have no spaces, start with a letter, and capitals count.