Find a course

Module 1: What Is an Algorithm?Lesson 3 of 78 min

Bugs and Debugging

Every coder makes mistakes. Learn what bugs are, the three most common kinds, and how to hunt them down and fix them step by step.

Step 1 of 12Learn

What is a bug?

A bug is a mistake in an algorithm that makes it do the wrong thing.

In 1947, people working on an early computer found a real moth stuck inside it, making it fail. They taped the moth in their notebook and joked that they had found a "bug." The word stuck around.

Finding and fixing mistakes is called debugging. Every programmer in the world does it, every single day. Making bugs is normal. Fixing them is the skill.

Step 2 of 12Practice · True or false

Good programmers never make bugs.

Step 3 of 12Timeline

A short history of bugs and algorithms

  1. About 820
    Al-Khwarizmi

    A scholar in Baghdad writes step-by-step ways to solve math problems.

  2. 1843
    Ada Lovelace

    She writes an algorithm for a machine that was never finished. Many call it the first computer program.

  3. 1947
    The moth

    A real moth is found inside a computer and taped into a notebook as the first "bug".

  4. Today
    Debugging tools

    Programmers use special tools that run code one step at a time.

Step 4 of 12Tap to reveal

Three common kinds of bugs

  1. Wrong order

    All the steps are there, but two are swapped. Like pouring milk before you get out the bowl.

  2. Missing step

    A step was forgotten. Like a cake recipe that never says to turn on the oven.

  3. Wrong step

    A step says the wrong thing. Like "turn left" when you needed to turn right.

Step 5 of 12Learn

How to hunt a bug

Debugging is like being a detective. Here is a plan that works:

  1. Run the steps and watch what happens.
  2. Find the first step where things go wrong.
  3. Fix that one step.
  4. Test again to make sure it works now.

Try not to change many things at once. Change one thing, then test. That way you know which change fixed it.

Step 6 of 12Practice · Put in order

Put the debugging steps in order.

Drag the items, or use the arrow buttons, to put them in order.

  1. Test again
  2. Fix that step
  3. Find where it goes wrong
  4. Run the steps and watch

Step 7 of 12Diagram

The bug hunt loop

The bug hunt loopCycle diagram: Run → Find → Fix → Test.DebuggingRunFindFixTest

Tap a shape with to see more.

Run

Do the steps and watch.

Find

Spot the first step that goes wrong.

Fix

Change just that one step.

Test

Run again. Still wrong? Go around once more.

Step 8 of 12Practice · What would you do?

What would you do?

Mia's cake recipe says: mix flour, sugar and eggs; pour into a pan; take the cake out after 30 minutes. Her cake comes out as cold, gooey batter. What kind of bug is it?

Step 9 of 12Practice · Spot the mistake

Leo wants his robot to walk 2 squares forward, turn right, then walk 1 square to reach the star. The star is to the right. Tap the buggy step.

Step 10 of 12Note

Step 11 of 12Practice · Match the pairs

Match each bug to its kind.

Tap an answer, then tap the place where it belongs (or drag it there).

  1. Recipe never says "turn on the oven"
  2. Shoes go on before socks
  3. "Add 20 cups of salt" instead of 2

Step 12 of 12Practice · Fill in the blanks

Fill in the blanks

Finding and fixing mistakes in an algorithm is called .

Word bank