Lesson 2.9
Trace a branching program
Part 2: DecisionsReading, about 4 min2 exercises in this lesson, in PythonNo Java exercises in this lesson yet.
Tracing a program with branches
By the end of this you will be able to trace a program with branches: say which lines run, keep a table of the variables and the output, and tell a separate if from an else-if chain.
This lesson has no new code. It is about reading code that you already know how to write, the way a trace question asks you to.
Which lines run
A program with branches does not run every line. Each time you reach a condition, work it out with the values the variables hold right now. If it is true, the block under it runs. If it is false, the block is skipped, and lines that are skipped change nothing.
Keep a table with one column for each variable and one for the output. Add a row only when a line changes a variable or prints something. The tables below also have a note column, where each condition you check is written down as true or false. That column is only for you, the reader: a trace question asks for the variables and the output. But the notes tell you which lines run.
Two separate ifs
x = 12
if x > 10:
x = x - 5
if x > 5:
x = x * 2
print(x)public static void main(String[] args) {
int x = 12;
if (x > 10) {
x = x - 5;
}
if (x > 5) {
x = x * 2;
}
System.out.println(x);
}x | output | note |
|---|---|---|
| 12 | ||
| 7 | x > 10 is true | |
| 14 | x > 5 is true, with x now 7 | |
| 14 |
The two ifs are separate, so both conditions are checked, one after the other. The second one uses the new value of x, which the first block has just changed.
The same lines as an else-if chain
Now join the two tests into one chain. In Python the second test becomes elif. In Java it becomes else if.
x = 12
if x > 10:
x = x - 5
elif x > 5:
x = x * 2
print(x)public static void main(String[] args) {
int x = 12;
if (x > 10) {
x = x - 5;
} else if (x > 5) {
x = x * 2;
}
System.out.println(x);
}x | output | note |
|---|---|---|
| 12 | ||
| 7 | x > 10 is true | |
| 7 |
In a chain, the first true branch wins and the rest of the chain is skipped. The test x > 5 is never checked at all, even though x is 7 by then. So this program prints 7, and the one with two separate ifs prints 14.
Try each condition both ways
One trace shows one path through the program. To be sure you know what it does, trace it again with starting values that make each condition true once and false once.
- Start with
xas 8. In both programsx > 10is false, thenx > 5is true, so both print16. - Start with
xas 3. Both conditions are false, nothing changes, and both print3.
The two programs differ only when the first condition is true. That is the one case where a separate if still checks its condition and a chain does not.
The trace questions below show their code in Python. Every rule on this page works the same way in Java.
A common mistake: using the value a variable had at the start. A condition is worked out with the value the variable holds on that line, after every change above it.
Now try it yourself on the questions below.
Exercise 2.9.1 · Trace itTrace: two ifs
Complete the trace table for this Python code. [2]
Line 1: age = 14Line 2: ticket = "child"Line 3: if age >= 12:Line 4, indented 1 level: ticket = "adult"Line 5: if ticket == "child":Line 6, indented 1 level: price = 5Line 7: else:Line 8, indented 1 level: price = 10Line 9: print(price)
Fill in one row each time a line changes age, ticket or price, 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 | age | ticket | price | output |
|---|---|---|---|---|
| 1 | ||||
| 2 | ||||
| 3 | ||||
| 4 | ||||
| 5 |
Exercise 2.9.2 · Trace itTrace: elif update
Complete the trace table for this Python code. Then answer the question below the table. [3]
Line 1: temp = 18Line 2: if temp < 10:Line 3, indented 1 level: temp = temp + 5Line 4: elif temp < 20:Line 5, indented 1 level: temp = temp + 3Line 6: elif temp < 30:Line 7, indented 1 level: temp = temp - 2Line 8: print(temp)
Fill in one row each time a line changes temp, 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 | temp | output |
|---|---|---|
| 1 | ||
| 2 | ||
| 3 |