Variables — Pseudocode

A variable is a name that stores a value (a number or text). price ← 5 stores 5 in the variable price (the arrow ← can also be written <- or =). A variable…

In pseudocode, price ← 4 means: store 4 under the name price. If the computer runs price ← 4, then price ← 5, what does price hold now? Both numbers, or only one? Guess first. The answer is in the second section.

This lesson also covers one thing that often confuses new programmers: why swapping what two names hold needs a third name.

← stores a value in a name

The arrow ← stores a value (a number or text) in a name. The result is below the code:

price ← 5
PRINT price
PRINT "price"
5
price

A variable name may contain letters, digits and underscores (_), and must start with a letter. Spaces are not allowed, so join several words: icedTeaPrice or iced_tea_price. Later, in the programming language JavaScript, the line price ← 5 is written let price = 5. In Python, it is price = 5.

One name, one value

Here are the two lines from the question at the start of the lesson, plus one PRINT:

price ← 4
price ← 5
PRINT price
5

So the answer is: only one, 5. A variable holds only one value. When price ← 5 runs, 4 is thrown away and replaced by 5.

A variable can also be used to calculate its own new value:

stock ← 12
stock ← stock - 3
PRINT stock
9

In maths, stock = stock - 3 makes no sense. In a program, stock ← stock - 3 is a command, not an equation. The computer does it in two steps:

  1. It calculates the right side of the arrow with the old value of stock: 12 - 3.
  2. It stores the result, 9, in stock.

Copying the value, not linking the names

The right side of the arrow can also be the name of another variable:

price ← 5
oldPrice ← price
price ← 6
PRINT oldPrice, price
5 6

oldPrice ← price copies what price holds at that moment, 5, into oldPrice. After that line, the two variables are not connected. When price changes to 6, oldPrice stays 5.

Swapping two variables

You have two glasses: glass A holds tea, glass B holds coffee. How do you swap what is in them? You need a third, empty glass. In a program, each glass is one variable. Guess first: what is printed when the program swaps without a third glass?

glassA ← "tea"
glassB ← "coffee"
glassA ← glassB
glassB ← glassA
PRINT glassA, glassB
coffee coffee

The third line copies the coffee into glass A, and the tea is gone at that moment. Glass B still holds coffee, because ← copies and does not move. The fourth line only copies that coffee back into glass B. So what glass A holds must first be stored in a third variable:

glassA ← "tea"
glassB ← "coffee"
temp ← glassA
glassA ← glassB
glassB ← temp
PRINT glassA, glassB
coffee tea

The variable temp is the third glass: it holds the tea while glass A gets the coffee, and then the tea is copied into glass B. The swap takes three lines, and their order matters.

⚠️ Names the computer doesn't recognise

TL;DR: A variable is a name that stores a value (a number or text). price ← 5 stores 5 in the variable price (the arrow ← can also be written <- or =). A variable holds only one value: a new value replaces the old one. stock ← stock - 3 calculates the right side of the arrow first with the old value of stock, then stores the result in stock. For two variables a and b, a ← b copies what b holds at that moment into a, and after that the two are not connected. Swapping two variables needs a third one: temp ← a, a ← b, b ← temp. A variable name has no spaces, starts with a letter, and its capital and small letters must always be the same (Price and price are two different names).

⚡ The arrow stores a value

Write:

price ← 5
PRINT price
PRINT "price"

Meaning: Read price ← 5 as "price gets 5". A name that stores a value (a number or text), such as price, is called a variable. PRINT price prints what that variable holds, 5. PRINT "price" prints the word price, because it is in quotes. If ← is hard to type, use <- or =. All three are read the same way.

⚡ One name, one value

Guess first: price ← 4, then price ← 5, then PRINT price. What is printed?

Only 5. The 4 is thrown away when the new value comes in.

Meaning: A variable holds only one value. If stock holds 12, stock ← stock - 3 is allowed too. The right side of the arrow is calculated first with the old value of stock (12 - 3). Then the result, 9, is stored in stock.

⚡ Swapping without a third glass

Guess first: glassA holds tea, glassB holds coffee. To swap them, a program runs glassA ← glassB, then glassB ← glassA. What do the two variables hold now?

Both hold coffee (coffee coffee). As soon as the coffee is copied into glassA, the tea is gone.

Meaning: glassA ← glassB copies what glassB holds into glassA, and what glassA held before (the tea) is gone. To swap, the tea in glassA must first be stored in a third variable. It is like swapping the drinks in two real glasses: you need a third, empty glass.

⚡ Swapping with a third glass

Write:

glassA ← "tea"
glassB ← "coffee"
temp ← glassA
glassA ← glassB
glassB ← temp
PRINT glassA, glassB

Meaning: The variable temp is the third glass: it holds the tea while glassA gets the coffee, and then the tea is copied into glassB. The result is coffee tea. The swap takes three lines, and their order matters.

What you will learn