Lesson 1.5
A variable holds one value; assigning again replaces it
Part 1: Programs that talkReading, about 4 min3 exercises in this lesson, in Python1 exercise in this lesson, in Java
Java answers can't run on Spine yet. You can still read Java, trace it, and put an answer's lines in order.
A variable holds one value at a time
By the end of this you will be able to save a value in a variable, use it later, and trace what happens when one is replaced.
So far, a value was used straight away: the line the user typed went straight into a print line, and was then gone. A variable gives a value a name so you can use it more than once, or use it later.
Saving a value
In Python, write the name, an equals sign, and the value. In Java, the first time you use a name you say what kind of value it holds.
floor = 3
name = "Library"
print("Floor " + str(floor) + ": " + name)public static void main(String[] args) {
int floor = 3;
String name = "Library";
System.out.println(
"Floor " + floor + ": " + name);
}When it runs:
Floor 3: Library
A variable keeps its value until you change it. The name floor holds 3 from the moment it is set until the program ends or another line sets it to something else.
Replacing a value
Assigning to the same name again replaces what was there. The old value is gone.
x = 3 print(x) x = 7 print(x)
public static void main(String[] args) {
int x = 3;
System.out.println(x);
x = 7;
System.out.println(x);
}When it runs:
3 7
After x = 7, the 3 is gone. A variable holds one value at a time.
Using the current value to compute the next one
A line like x = x + 1 looks strange in maths, but in code it means: take the current value of x, add 1, and store the result back in x. The right side is worked out first, using whatever x holds right now, and only then does x change.
If x is 2, then x = x + 1 makes x become 3 (because 2 + 1 is 3). The old 2 is gone, replaced by 3. The same idea works with two variables: a = a + b means take the current a, add the current b, and store the result in a. The value of b does not change.
Tracing what happens
A trace table follows each variable's value, line by line, to find what the program prints. Write one column for each variable and one for the output. Each row is a line that changes something.
For example:
x = 5 print(x) x = 2 print(x) x = x + 1 print(x)
public static void main(String[] args) {
int x = 5;
System.out.println(x);
x = 2;
System.out.println(x);
x = x + 1;
System.out.println(x);
}| What the line does | x | output |
|---|---|---|
sets x to 5 | 5 | |
prints x | 5 | |
sets x to 2 | 2 | |
prints x | 2 | |
x = x + 1 | 3 | |
prints x | 3 |
On the line x = x + 1, x is 2, so x + 1 is 3, and x becomes 3. Read the table top to bottom. When x changes, write its new value. When a line prints, write what it prints in the output column. This is how a trace table question in the exam works: you follow the code and fill in the table.
A common mistake: using the original value instead of the current one. If you used the first x (5) in x = x + 1, you would get 6, not 3. Always use the value x holds on that line.
Now try it yourself on the questions below.
Exercise 1.5.1 · Trace itTrace: replace value
Complete the trace table for this Python code. [2]
Line 1: x = 5Line 2: print(x)Line 3: x = 2Line 4: print(x)Line 5: x = x + 1Line 6: print(x)
Fill in one row each time a line changes x, or prints something. The table already has the right number of rows. In each row, write only what that line changes; you can leave the other cells empty or copy their values down. Write values the way Python shows them: 3 and 3.0 are different, and text goes in quotes. In the output column, write exactly what appears on screen, with no quotes around it.
| Row | x | output |
|---|---|---|
| 1 | ||
| 2 | ||
| 3 | ||
| 4 | ||
| 5 | ||
| 6 |
Exercise 1.5.2 · Trace itTrace: two variables
Complete the trace table for this Python code. [2]
Line 1: a = 1Line 2: b = 10Line 3: a = a + bLine 4: print(a)Line 5: b = a - bLine 6: print(b)
Fill in one row each time a line changes a or b, or prints something. The table already has the right number of rows. In each row, write only what that line changes; you can leave the other cells empty or copy their values down. Write values the way Python shows them: 3 and 3.0 are different, and text goes in quotes. In the output column, write exactly what appears on screen, with no quotes around it.
| Row | a | b | output |
|---|---|---|---|
| 1 | |||
| 2 | |||
| 3 | |||
| 4 | |||
| 5 | |||
| 6 |
Exercise 1.5.31.5.1 · Write itRepeat line
Write a program that reads one line typed by the user and prints it twice, on two lines.
- Each of the two lines is the typed line, exactly as it was typed.
- Print nothing else. Spaces at the end of a line are ignored.
Your program is run several times, with a different line typed each time. Only what it prints is checked.
In Python, save the line in a variable so you can print it twice. In Java, the page starts you with a class, its main method and a Scanner called scanner: read the line with scanner.nextLine() and save it in a String variable.
Running Java here comes later. You can put this answer's lines in order instead. You can still write your Java answer (it stays while this page is open) and compare it with our worked solution. Or try this exercise in Python.
Examples
Your program is run 4 times: the example below, and 3 hidden ones that check the rules above. Only what it prints is checked.
the typed line, printed twice (rules 1 and 2)
You type
- typed: Hello again
It prints
Hello again Hello again