Before the bell
- The I Do form in the I Do deck. All three sections. If you are here, this should be done.
- The We Do: Trace Tables: Running Code with Your Fingers. Read each page with the class. Work every Try It in your notebook, then check it. Nothing to turn in.
- Bench Basics: Trace Tables. About 30 minutes. It opens later in class on the Bench Basics slide. Write your credit code in your notebook.
- The exit ticket. The last 10 minutes of class, a locked form on a school Chromebook. It asks the same kinds of questions as the Try Its and the bench.
Trace Tables: Running Code with Your Fingers
This section shows how to build a trace table from nothing: first the header row, then one row for each line that runs. It ends with longer code and with checking a table that someone else filled in.
Building the Header
A trace table starts with its header row, the row of column names at the top. The header comes from the code itself. A box needs a column only if the code stores a value in it, and every box is stored before it is used, so the names to look for are the names on the left of an arrow.
How to build the header row:
- Write line as the first column.
- Scan the code from top to bottom. Each time a name appears on the left of an arrow for the first time, add a column for it.
- If any line says show, add a shown column at the right.
- Do not add a column for a number, for the arrow, or for the word show.
Example 1. Build the header row for these lines.
1x ← 7 2y ← x - 2 3z ← x + y 4show z
line x y z shown
Three names are stored: x on line 1, y on line 2 and z on line 3. Line 4 shows a value, so the table also needs a shown column. The name x appears again on lines 2 and 3, but it already has its column.
Example 2. Build the header row for these lines.
1total ← 0 2price ← 4 3total ← total + price 4total ← total + price
line total price
The name total is stored three times, but it is one box, so it gets one column. Nothing is shown, so there is no shown column.
Work each one in your notebook first, then check it.
Try It 1. Which header row does a trace table for this code need?
1p ← 3 2q ← p * 4 3show q
Try It 2. Not counting the line column, how many box columns does a trace table for this code need?
1k ← 1 2k ← k + 1 3m ← k 4k ← m * 3
One Row for Each Line That Runs
Each row of a trace table records one line that runs. The values in a row are what every box holds right after that line runs, and a box that has nothing stored in it yet gets a dash. A box that does not change on a line copies its value down from the row above.
In every snippet so far, each line runs exactly once, from top to bottom, so the table has one row for each line of code. That will change. Later in the course, some lines will run more than once, and each time a line runs it gets a new row. The rule that stays true is one row for each line that runs.
Example 3. Fill in the table for the code in Example 1.
line x y z shown 1 7 - - 2 7 5 - 3 7 5 12 4 7 5 12 12
Line 2 works out 7 - 2 and stores 5 in y. Line 3 works out 7 + 5 and stores 12 in z. Line 4 changes no box, so the row copies every value down and records 12 in the shown column.
Example 4. Fill in the table for the code in Example 2.
line total price 1 0 - 2 0 4 3 4 4 4 8 4
Lines 3 and 4 are written the same way, but each one runs on its own and gets its own row. Line 3 works out 0 + 4, and line 4 works out 4 + 4.
Work each one in your notebook first, then check it.
Try It 3. How many rows, not counting the header row, does a trace table for this code need?
1r ← 1 2s ← r + 1 3r ← s + 1 4s ← r + 1 5show s
Try It 4. What does row 3 of the trace table hold?
1a ← 4 2b ← a - 1 3a ← a + b
When the Code Gets Longer
Two boxes and three lines fit in the head. Four boxes that each change are a different job, and this is where values get dropped. The table keeps every value on the page, so nothing has to be remembered.
Example 5. Trace these eight lines.
1a ← 2 2b ← a * 3 3c ← b - a 4a ← c + b 5d ← a - c 6b ← d * 2 7c ← c + a 8show c
line a b c d shown 1 2 - - - 2 2 6 - - 3 2 6 4 - 4 10 6 4 - 5 10 6 4 6 6 10 12 4 6 7 10 12 14 6 8 10 12 14 6 14
A reader working in the head often uses 2 for a on line 7, because a held 2 for the first three lines. That gives 6 for c instead of 14. In the table, row 4 shows that a is 10, so the reader looks the value up instead of remembering it.
Work each one in your notebook first, then check it.
Try It 5. Build the table in your notebook. What does line 7 show?
1x ← 1 2y ← x + 4 3z ← y * 2 4x ← z - y 5y ← x + z 6z ← z - x 7show y
Try It 6. Build the whole trace table, header row first, for these lines.
1a ← 6 2b ← a - 2 3c ← a + b 4a ← c - b 5b ← b * 3 6show a
Check your answer
Header: line, a, b, c, shown. Line 1: a 6. Line 2: a 6, b 4. Line 3: a 6, b 4, c 10. Line 4: a 6, b 4, c 10 (10 - 4 stores 6 again). Line 5: a 6, b 12, c 10. Line 6: a 6, b 12, c 10, shown 6.
Checking a Trace Table
To check a trace table that someone else filled in, work down the rows in order. For each row, use the values in the row above it and work out the line again. The first row that does not match is the first wrong row. Every row after it may be wrong too, because it was built on a wrong value.
Example 6. A student traced these lines. Find the first wrong row.
1m ← 5 2n ← m + 1 3m ← n * 2 4n ← m - n
line m n the student wrote 1 5 - m is 5 2 5 6 n is 6 3 10 6 m is 10 4 10 4 n is 4
Row 3 is the first wrong row. Line 3 multiplies n, which holds 6, so m should be 12. The student multiplied the old m, 5, instead. Row 4 is wrong only because it used the wrong m. With m holding 12, line 4 works out 12 - 6 and stores 6 in n.
Sources: Store It reading and B06 bench, class site (2026).
Work each one in your notebook first, then check it.
Try It 7. A student traced these lines and wrote: line 1, g is 2. Line 2, h is 7. Line 3, g is 5. Line 4, h is 14. Which row is the first wrong one?
1g ← 2 2h ← g + 5 3g ← h - g 4h ← g * h
Try It 8. In the same student's table, what mistake made row 4 wrong?
1g ← 2 2h ← g + 5 3g ← h - g 4h ← g * h
Trace Tables
Open the bench and work the jobs in order. They get harder as you go. Write your credit code in your notebook, then paste it into the credit-code form.
Bench Basics opens during your class period.
Open it on a school Chromebook. Locked mode does not work on a lab computer.
Open the exit ticketThe exit ticket opens 10 minutes before the bell.