Debugging: finding the error in the code
Be able to trace a program's execution step by step to identify and correct logical errors.
Prerequisites
- ABasic programming with blocksrequired
Everyday explanation
Programs almost always contain errors on the first run. Such an error is called a bug. The process of finding the error is called debugging.
The method is to avoid guessing. Instead, you trace the program line by line, as if you were the processor yourself, and document the value of each variable. The error lies where the actual execution deviates from the intended logic.
Interactive
Identify the error: A routine should perform a movement sequence that forms a square.
repeat 4 times:
forward
left
left
Trace the execution: forward, turn 90 degrees, turn 90 degrees again. The object is now facing the opposite direction. The next command "forward" moves it backwards. The result is a back-and-forth oscillation, not a square. The error is a redundant left command. The solution is to remove only that command.
Document: What was the goal? What actually happened? Where does the result differ from the expectation?
Mastery means
- Traces a program's execution step by step and identifies where the logic breaks
- Makes minimal corrections to fix the error without altering the rest of the code
Sign in to do the exercises and build your mastery up.
Sources
- CS Unplugged (CC BY-SA 4.0) — CC BY-SA 4.0
- Scratch Wiki (CC BY-SA 4.0) — CC BY-SA 4.0