B2.1.4 · Programming fundamentals
Debugging techniques
By the end you can
Apply dry running, trace tables, breakpoints, and print debugging to isolate syntax, runtime, and logic errors.
Programming bugs are inevitable. is the systematic process of finding, isolating, and fixing defects in software. To debug effectively, an engineer must distinguish between the three primary categories of computer errors.
Syntax, runtime, and logic errors#
- Syntax errors: Violations of the grammatical rules of the programming language. Caught by the parser or compiler before the program can run. Examples include unclosed quotes, missing brackets, or misspelling
whlieinstead ofwhile. - Runtime errors: Occur when the program instructs the CPU or interpreter to perform an impossible or illegal operation during execution. The operating system or runtime halts the program and throws an exception (such as index out of range or division by zero).
- Logic errors: The most difficult to identify. The code compiles cleanly and executes without crashing, but computes incorrect results. Examples include using
+instead of-, wrong operator precedence, or off-by-one loop conditions (< instead of ≤).
Systematic debugging techniques#
Rather than guessing changes randomly, the IB syllabus highlights four formal debugging strategies:
- Dry running with trace tables: Stepping through code manually on paper with a table listing each variable as columns and loop iterations as rows. Essential for Paper 1 and Paper 2 exam questions.
- Print / logging statements: Inserting temporary print statements (e.g.
print("DEBUG: x =", x)) before and after suspicious routines to observe actual variable values. - IDE Breakpoints and stepping: Pausing execution at a critical line to examine the active stack frame and stepping line-by-line through branches.
- Boundary and edge-case testing: Testing the algorithm with extreme inputs (empty strings, zero, negative numbers, maximum array indices) where logic flaws most frequently manifest.
Check yourself
Debugging techniques
Quick questions, made fresh every time and marked here. Do a few until they feel easy.
Exam practice
Paper 1-style questions
3 questions · 9 marks
Write your answer as you would in the exam, then show the mark points and tick each one your answer made. These are our own questions in the style of the exam, not past-paper questions.
1.Distinguish between syntax errors, runtime errors, and logic errors, giving one example of each.
[3]Distinguish: Make the differences between two things clear.
2.Explain the purpose of dry running an algorithm using a trace table.
[2]Explain: Give reasons: say what happens AND why, or how one thing causes another.
3.Describe two features provided by an Integrated Development Environment (IDE) to assist with debugging, and explain how each aids fault localisation.
[4]Describe: A detailed account: say what happens, step by step if it is a process.
In a nutshell
- Syntax errors violate the grammatical rules of the language and are caught before compilation or execution.
- Runtime errors occur during execution when an illegal operation is requested, raising an exception or crash.
- Logic errors compile and run cleanly but produce incorrect results due to flawed algorithm design or operator precedence.
- Debugging techniques include dry running with trace tables, print statements, IDE breakpoints, stepping and boundary testing.
