Debugging Examples in CS Thinking
Start with the recap, study the fully worked examples, then use the practice problems to check your understanding of Debugging.
This page combines explanation, solved examples, and follow-up practice so you can move from recognition to confident problem-solving in CS Thinking.
Concept Recap
The systematic process of finding, diagnosing, and correcting errors (bugs) in a program.
Detective work—observe the wrong output, form a hypothesis, test it, then fix what's wrong.
Read the full concept explanation →How to Use These Examples
- Read the first worked example with the solution open so the structure is clear.
- Try the practice problems before revealing each solution.
- Use the related concepts and background knowledge badges if you feel stuck.
What to Focus On
Core idea: Debugging is systematic: reproduce the bug, isolate the cause, apply a fix, then verify it works.
Common stuck point: The bug might not be where the error appears—trace backward.
Worked Examples
Example 1
mediumSolution
- 1 Step 1: Trace: i=1 (output 1), i=2 (output 2), i=3 (output 3), i=4 (output 4), i=5 (5 < 5 is false, loop ends).
- 2 Step 2: The loop outputs 1, 2, 3, 4 — it misses 5.
- 3 Step 3: Fix: change `WHILE i < 5` to `WHILE i <= 5`.
Answer
Example 2
mediumPractice Problems
Try these problems on your own first, then open the solution to compare your method.
Example 1
mediumExample 2
mediumBackground Knowledge
These ideas may be useful before you work through the harder examples.