LightMySky
به انگلیسی

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.

No account needed. Progress saves in this browser.

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.

Try it together

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.

Good to know

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.

More to read

How the Computer Tells You Something Is Wrong

Every programmer makes mistakes. That's normal. The trick is knowing what kind of mistake you made, because the computer treats them differently.

A syntax error happens when your code breaks the rules of the language, like a missing colon or a typo in a keyword. The computer can't even understand what you meant, so it stops right away and points at the line number where it got confused. You have to fix it before the program will run at all.

A logic error is sneakier. The program runs from start to finish with no complaints, but it gives you the wrong answer. The computer isn't confused about the grammar, it's just doing exactly what you told it, even if what you told it wasn't what you meant. To catch a logic error, you usually have to trace through the code by hand, checking what each variable holds line by line, until you spot where the values go off track.

So when something breaks: if the program refuses to run and shows an error message with a line number, that's a syntax error. If it runs fine but the output is wrong, that's a logic error, and it's on you to trace the code to find where it went wrong.

2 · Watch

به انگلیسی

Take it off screen

Print a worksheetA4 with an answer key page for grown-ups. No screen, no internet.

Where it sits

Then practise

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.

Spotted a problem on this page? Tell us
Tracing a Program by Hand · Computing, ages 15 to 16 · LightMySky