Variables
After this: Give a named box a value, give it a new one, and say what it holds.
On this page · 8 sections · 4 checks
Picture a kitchen shelf with jars on it. One jar has a label that says "sugar". You do not need to look inside to know what it is for. When the sugar runs out, you refill the same jar. The label stays; what is inside changes.
The last lesson showed values: numbers, text, yes or no. A program needs to keep a value and use it again later. It does that with a labelled jar of its own.
A variable is a named box
A is a box with a name. It holds one value. Later you can put a different value in the box, and the old one is gone.
The first line of the program below creates a box named name and puts the text "Ana" in it.
The = sign does not mean "equals". It is an instruction: take the value on the right and put it in the box named on the left. Read name = "Ana" as "name becomes Ana". The trace says it the same way.
Watch a box get a value, then a new one
- Run the programPress Run it and step forward. Watch the box named name appear with Ana in it, and watch what happens to Ana when the third line runs.
name = "Ana"print("Hi " + name)name = "Ben"print("Hi " + name)run the program to see its boxes
nothing printed yetPress Run it, then step through the program one line at a time.
- lines
- 0
- prints
- 0
- calls
- 0
- The first line creates the box
nameand puts"Ana"in it. The box appears in the Variables panel. - The second line prints
"Hi " + name. When the computer meets the name of a box, it looks inside and uses the value it finds there. It printsHi Ana. - The third line puts
"Ben"in the box."Ana"is gone. A box holds one value. - The fourth line prints
Hi Ben.
Using the old value to make the new one
A box can be given a new value that is worked out from its old value.
apples = 3apples = apples + 2print(apples)run the program to see its boxes
nothing printed yetPress Run it, then step through the program one line at a time.
- lines
- 0
- prints
- 0
- calls
- 0
The second line, apples = apples + 2, looks wrong if you read = as "equals": a number is never equal to itself plus two. Read it as an instruction instead. The computer first works out the right side using the value in the box now: 3 + 2 is 5. Then it puts 5 in the box, and the old 3 is gone. The third line prints 5.
You will write this kind of line often: a box updated from its own value.
Run it
Add a third name of your own, with a print line after it, and run it again.
Naming a box
A name is made of letters, digits and underscores, with no spaces, and it cannot start with a digit. name, total and price_2 are fine; 2nd and my total are not. Capital letters matter: Name and name are two different boxes. Choose a name that says what the box holds.
Check yourself
A program runs city = "Rome" and then city = "Oslo". What does the box named city hold now?
What does this print?
count = 5count = count + 1print(count)int count = 5;count = count + 1;System.out.println(count);After the third line has run, what do the boxes a and b hold?
a = 2b = a + 3a = 10print(a)print(b)int a = 2;int b = a + 3;a = 10;System.out.println(a);System.out.println(b);Complete the second line so that the program prints 50: give the box price a value 10 more than it holds now.
price = 40____ # ← make price 10 more than it wasprint(price)int price = 40;____ // ← make price 10 more than it wasSystem.out.println(price);Where the two languages differ
Both languages make boxes the same way. They differ in one thing: whether you write down the kind of value a box holds. Here are the same four boxes in each language.
age = 30price = 3.5name = "Ana"done = Trueint age = 30;double price = 3.5;String name = "Ana";boolean done = true;What comes next
The next lesson is about the sums a program can do, and two places where the answer can surprise you.
- Say what a variable is: a named box that holds one value.
- Read = as an instruction, "becomes", not as "equals".
- Follow a box through a program: a new value replaces the old one.
- Work out a line like apples = apples + 2: the right side first, with the value in the box now.