Hi there! I am Studyfin, your coding buddy. Today, we are going to become code detectives and learn how to fix broken code!
An algorithm is a list of steps to finish a task. When we write these steps in code, we must be very careful. Sometimes, our steps have errors.
A mistake in our code is called a bug. Debugging means finding that error and fixing it so our program runs perfectly.
To debug, we read each line of our code step by step. We look closely to see where the rules go wrong.
✏️ Worked example
Let us help a robot walk to a gold star. The star is 3 steps to the right. The current algorithm says: 1. Move right. 2. Move left. 3. Move right. The robot is not reaching the star! Let us debug this algorithm.
Read the goal: The robot needs to move right exactly 3 times to reach the star.
Look at step 1: The robot moves right. This step is correct.
Look at step 2: The robot moves left. Wait! Moving left takes the robot backward. This is our bug!
Look at step 3: The robot moves right. This step is fine, but we are still too far away.
Remove the error: Change step 2 from 'Move left' to 'Move right'.
Test the new code: 1. Move right. 2. Move right. 3. Move right. Now the robot reaches the star! We successfully debugged the code!
Want the full interactive lesson — quiz, animations, and Finn cheering them on?