Tracing a Program by Hand
Following code line by line with a table of the variables, to say what it does and where it goes wrong. Errors that stop the program are found differently from errors that give a wrong answer.
What a learner can do afterwards
- Fill in a trace table for a loop and give the final output
- Find the line responsible for a wrong result
- Tell a syntax error from a logic error by what the computer does with each
1 · Read
Tracing means walking code line by line with a table of each variable. You write down what every name holds as you go. At the end you can say what the program prints before you run it.
A syntax error breaks the language rules, like a missing colon or a typo in a keyword. The computer cannot grasp your meaning, so it stops at once and points at the line. Fix it before the program can run at all.
A logic error is sneakier: the program runs start to finish with no complaints, yet the answer is wrong. The machine did exactly what you said, not what you meant. Trace by hand until the values drift off track.
Refuses to run plus a line number means a syntax slip. Runs fine but answers wrong means a logic slip. The symptom names the hunt.
Track every variable by hand to predict output, and let the symptom tell a syntax slip from a logic slip.
2 · Watch
Take it off screen
Where it sits
8 questions wait behind this lesson, each with its answer explained. Every answer feeds the sky: stars light as they are learned, and dim when it is time to come back.