On this concept: Formula

Not sure which lesson to do first? Find your level and get a plan →

CS Thinking · Computational Thinking · Grade 3-5

Debugging

The systematic process of finding, diagnosing, and correcting errors in a program: reproduce the problem, isolate its cause by testing and inspection, apply a targeted fix, and verify the fix works without breaking anything else.

bug report→hypothesis→test→fix

Quick Answer

The systematic process of finding, diagnosing, and correcting errors in a program: reproduce the problem, isolate its cause by testing and inspection, apply a targeted fix, and verify the fix works without breaking anything else. You debug when a program you already have does the wrong thing: it prints a wrong answer, a sprite walks the wrong way, or it stops with an error. Instead of guessing, you make the problem happen again, find the line that causes it, change that line, and run the program again to be sure.

Why This Matters

Almost no program works the first time, so debugging is how programs get finished. A careful debugger can find one wrong line among hundreds by checking one part at a time. Someone who guesses may change five things, make the bug vanish for now, and add two new bugs without noticing.

Intuitive Explanation

Imagine a program that should count to 5 but prints 1, 2, 3, 4 and stops. It runs, and nothing crashes. It just does the wrong thing. Debugging starts by making the problem happen again on purpose: run it once more and see the same 1, 2, 3, 4. Now you know the bug is real, and you can watch it.

Next you make a guess and test the guess. Maybe the loop stops too soon. The loop says: repeat while the number is less than 5. When the number reaches 5, the loop quits before printing it. Change the rule to 'less than or equal to 5', run it again, and 5 appears. Then try it with other numbers, to check that the fix did not break anything else.

Debugging is not the same as testing. Testing means running a program to find out whether something is wrong. Debugging begins after that, when you already know something is wrong and you hunt for why. Changing random lines until the output looks right is guessing, not debugging. The bug may come back later, because you never found what caused it.

Think of it like this. A good doctor does not hand out random medicine. They ask what is wrong, guess a cause, run one test to check the guess, then treat the cause instead of only the cough. Debugging follows the same order. One difference helps you: a program can be run again in exactly the same way, so you can repeat a test as often as you like.

Core idea. Debugging is systematic: reproduce the bug, isolate the cause, apply a fix, then verify it works.


When to Use

Debug when a program already exists and does something wrong: a wrong answer, a crash, a character that moves the wrong way, a word spelled wrong on screen. The words bug and error, or 'it should do this but it does that', are signals. If there is no program yet and you are planning the steps, you need an Algorithm. If you only need to find out whether a program works, that is Testing.

Tip. Ask: Do you already have a program that does the wrong thing, and need to find and fix the cause?

How to Recognize It

Before you start debugging, check that there is a real program in front of you and that you know what it should have done.

  1. Is there a program that already runs but gives a wrong answer, does something odd, or stops?

    Yes means you are debugging. If no program has been written yet, you are still planning the Algorithm.

  2. Can you make the problem happen again, every time, with the same steps?

    Yes means you can test your guesses about the cause. No means first find the exact steps that cause it, or you will not be able to tell when it is fixed.

  3. Do you know which part of the program is causing the problem?

    Yes means change just that part. No means narrow it down first, for example by checking what the values are halfway through.

  4. Are you only checking whether the program works, with no known problem yet?

    Yes points to Testing, which finds out whether there is a bug. Debugging starts once you know there is one.

Debugging vs Testing vs Error Types

All three are about mistakes in programs, so they sound alike. Ask what you are doing with the mistake: hunting it down and fixing it, finding out whether one exists, or naming what kind it is.

Debugging

Meaning
Debugging is finding the cause of a known problem in a program, fixing it, and checking that the fix works.
Key test
You already know something is wrong, and you fix it.
Formula
bug report→hypothesis→test→fix
Example
A game adds 2 points instead of 1, so you find the line with + 2 and change it.

Testing

Meaning
Testing runs a program with inputs whose right answers you already know, to find out whether anything is wrong.
Key test
You do not yet know whether there is a bug.
Example
Trying a times-table app on 3×4, 0×7 and 10×10.

Error Types

Meaning
Error types sort mistakes into groups, such as code the computer cannot read and logic that gives wrong answers.
Key test
You name what kind of mistake it is.
Example
Leaving out a closing bracket is a syntax error, a mistake in how the code is written.

Formula & Notation

bug report→hypothesis→test→fix
Debugging applies the scientific method to code: observe unexpected behavior, hypothesize a cause, design a test to confirm or reject the hypothesis, and iterate until the root cause is identified and corrected.

Worked Examples

Example 1 — Fix a countdown that skips 1

Easy

This program should print 3, 2, 1, Go: set n = 3; repeat while n > 1: print n, then n = n - 1; after the loop, print Go. It prints 3, 2, Go. Find the bug and fix it.
Show the steps and the answer
  1. Run it again and write down the output: 3, 2, Go. The 1 is missing.

    Seeing the same wrong output twice shows the bug is real, and it tells you what to look for: the missing 1.

  2. Trace n: n = 3 prints 3, n = 2 prints 2, then n = 1 and the check 1 > 1 is false, so the loop stops.

    Following the value of n shows the exact moment the program goes wrong.

  3. Guess the cause: the loop should still run when n is 1, so the check should be n > 0.

    The guess points to one line. Changing only that line keeps the rest of the program the same.

  4. Run the fixed program: 3, 2, 1, Go. Then start at n = 5: it prints 5, 4, 3, 2, 1, Go.

    A second starting number shows the fix works in general, not only for 3.

Answer: Change the loop check from n > 1 to n > 0. The program now prints 3, 2, 1, Go.

Takeaway: Follow the variable step by step until the output goes wrong. That step tells you which line to fix.

Example 2 — No wrong answer yet: test first

Standard

Ava's program says whether a number is even: if its last digit is 2, 4, 6 or 8, print 'even'; otherwise print 'odd'. She tried 4 (it printed 'even') and 7 (it printed 'odd'). Ben says, "Start debugging." Is there a bug to debug yet? What should Ava do?
Show the steps and the answer
  1. Look at what is known: both runs gave the right answer.

    Debugging needs a wrong answer to explain. Two right answers do not show a bug.

  2. Test first: pick numbers unlike 4 and 7, such as 10, 0 and 26.

    This is testing, finding out whether anything is wrong. Numbers with an unusual last digit are the most likely to catch a mistake.

  3. Run 10: its last digit is 0, which is not in the list, so the program prints 'odd'.

    Now there is a real wrong answer, because 10 is even. Debugging starts here.

  4. Fix the rule: make the list 0, 2, 4, 6, 8, then run 10, 0, 26 and 7 again.

    Re-running the old tests along with the new ones shows the fix did not break the answer for 7.

Answer: Not yet. Ava should test more numbers first. The test 10 → 'odd' finds a bug, and adding 0 to the list of even last digits fixes it.

Takeaway: Testing finds out whether something is wrong. Debugging starts once you have a wrong answer to explain.

Example 3 — Find the mistake: hiding the symptom

Application

A game should give 10 points for each star. After 3 stars the screen shows 40. The code says score = 10 at the start and score = score + 10 for each star. Sam changes the display line to show score - 10. Now the screen shows 30, and Sam says, "Fixed." Find the mistake and correct it.
Show the steps and the answer
  1. Trace the score: it starts at 10, then 20, 30, 40 after three stars.

    The extra 10 is there before any star is collected, so the cause is the starting value, not the display.

  2. See what Sam's change does: the screen shows score - 10 = 30, but score itself is still 40.

    Sam hid the symptom. Every other part of the game that reads score still gets the wrong number.

  3. Find where that matters: the game says 'You win' at 50 points, which now happens after 4 stars instead of 5.

    10 + 4 × 10 = 50, so the player wins one star early. The real cause is still causing trouble.

  4. Fix the cause: set score = 0 at the start and remove the - 10 from the display.

    Now the stored score and the score on screen are the same number.

  5. Check: after 3 stars the screen shows 30, and 'You win' appears after 5 stars, at 50.

    Checking both the screen and the win rule shows the whole game now uses the right score.

Answer: Start the score at 0 and show score itself. After 3 stars it shows 30, and the win at 50 points comes after 5 stars.

Takeaway: Covering up a wrong number on screen is not a fix. Find the line that makes the number wrong and change that line.

Where You'll See It

  • Finding why your bike won't go

    You notice it won't move (the bug), guess the chain slipped, check it, fix it, then ride to confirm; that's reproduce, hypothesize, test, fix, verify.

  • Figuring out why the TV won't turn on

    You isolate the cause step by step (is it plugged in? batteries in the remote?) instead of randomly pressing buttons, just like systematic debugging.

  • Tracking down a smell in the fridge

    You narrow down which shelf and which container is the source before tossing anything, the same isolate-the-cause approach used to find a bug.

Common Mistakes

  1. The slip: Debugging means changing things randomly until the error goes away.

    Why it's tempting: Sometimes a random change happens to work, which makes guessing feel effective.

    Instead: Debug systematically: reproduce the bug, isolate the cause, apply a targeted fix, then verify; random changes often hide the bug instead of fixing it.

  2. The slip: If the wrong output stops showing, the bug is fixed.

    Why it's tempting: The symptom is gone, so the problem feels solved.

    Instead: Hiding a symptom is not fixing the root cause; if you don't fix the real cause, the bug reappears in a new form later.

  3. The slip: Once I patch the bug, I'm done and don't need to test more.

    Why it's tempting: The original problem is gone, so further testing seems unnecessary.

    Instead: Always test the fix (and re-test the rest) because a change can introduce new bugs while fixing the first one.


Mini Practice

Try these on your own. Tap Reveal when you want to check.

  1. Robot steps should draw a square: "Go forward 3, turn right. Repeat 3 times." The robot draws only 3 sides. Find the bug and fix it.

    Hint: How many sides does a square have?

  2. A program should show the total of 4 and 6, but it shows 46. The line is `print("4" + "6")`. What is the bug?

    Hint: What do the quote marks do to the numbers?

  3. Steps to find the cost of 3 pencils at 5 cents each: "Step 1: price is 5. Step 2: total is price plus 3. Step 3: say the total." It says 8 instead of 15. Which step is wrong?

    Hint: Check each step against what you would do on paper.

  4. A game should give a star when the score is 10 or more. Tests show: score 12 gives a star, score 9 gives none, score 10 gives none. Which test found the bug, and what is the likely mistake?

    Hint: Which result does not match the rule?

  5. You try a calculator program on 10 ÷ 2, 9 ÷ 3 and 5 ÷ 0 to see whether anything goes wrong. Is that testing or debugging?

    Hint: Do you already know there is a bug, or are you looking for one?

  6. Ella's program shows "Helo" instead of "Hello". She retypes the whole program, and now it shows "Hlelo". What should she have done instead?

    Hint: How many lines actually print that word?

Want the full set?

50 practice questions for this concept — free to try, every one with a complete worked solution showing the why, not just the answer.

Frequently Asked Questions

Why is a mistake in a program called a bug?

Engineers called faults in machines bugs long before computers existed. A famous story comes from 1947, when people working on the Harvard Mark II computer found a real moth stuck inside it and taped it into their logbook, joking that it was the first actual bug ever found. The moth did not invent the word, but it made the story famous.

What is rubber duck debugging?

It means explaining your program, line by line, out loud to something that cannot answer, like a rubber duck or a toy. Saying exactly what each line should do makes you slow down, and very often you hear yourself say something that does not match the code. Then you have found the bug without anyone's help. Talking it through with a friend works too.

Is it bad if my program has bugs?

No. Every programmer writes bugs, including experts who have been coding for many years. Large programs have so many parts that some mistakes always slip in. Being good at programming does not mean never making bugs; it means finding and fixing them calmly. Each bug you fix also teaches you one more mistake to watch for next time.

Learning Path

← Before

Algorithm
Debugging

You are here

First know what an Algorithm is: the steps a program should follow. Debugging adds a way to find and fix the step that went wrong. Next come Testing (choosing inputs that catch bugs early), Error Types (telling a typing slip from a wrong idea) and Edge Cases (the unusual inputs where bugs like to hide).

See Also