Hey there! Ready to become a code detective? Today, we will learn how to find and fix sneaky mistakes in code.
In coding, a mistake is called a bug. Debugging is the step-by-step process of finding and removing these errors so your program runs perfectly.
To debug, we read code like a recipe. We check each line in order to see where the instructions stop working or do something unexpected.
Once we spot the bug, we fix it by changing the wrong instruction. Watch how replacing the broken block makes our character reach the goal.
✏️ Worked example
A robot program is supposed to move 1 step right, then 1 step up, and then 1 step right to reach a star. Instead, the robot moves right, moves down, and gets stuck. Let's debug this algorithm.
Analyze the goal: The robot needs to go Right, Up, Right.
Read the current program lines: Line 1 says 'Move Right'. Line 2 says 'Move Down'. Line 3 says 'Move Right'.
Compare the program to the goal: Line 1 is correct (Right). Line 2 is incorrect because it says 'Down' instead of 'Up'.
Identify the bug: The error is in Line 2.
Fix the bug: Change Line 2 from 'Move Down' to 'Move Up'.
Test the code: Run the new sequence (Right, Up, Right) to make sure the robot successfully reaches the star.
Want the full interactive lesson — quiz, animations, and Finn cheering them on?