Studyfin Studyfin ✨ Sign up free

Debugging

Grade 4 · Coding · Free lesson

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.

school

A mistake in our code is called a bug. Debugging means finding that error and fixing it so our program runs perfectly.

<code> step1: Walk forward step2: Jump step3: Turn right Debugging removes the bug!

To debug, we read each line of our code step by step. We look closely to see where the rules go wrong.

[Code Inspector] 1. Move East 2 steps 2. Move South -5 steps <-- ERROR! 3. Open the treasure chest
✏️ 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.

  1. Read the goal: The robot needs to move right exactly 3 times to reach the star.
  2. Look at step 1: The robot moves right. This step is correct.
  3. Look at step 2: The robot moves left. Wait! Moving left takes the robot backward. This is our bug!
  4. Look at step 3: The robot moves right. This step is fine, but we are still too far away.
  5. Remove the error: Change step 2 from 'Move left' to 'Move right'.
  6. Test the new code: 1. Move right. 2. Move right. 3. Move right. Now the robot reaches the star! We successfully debugged the code!
code
Want the full interactive lesson — quiz, animations, and Finn cheering them on?

More Coding lessons