Debugging Your Code
Debugging means finding and fixing mistakes in your code so your program works the way you planned.
Reading is good — doing is better. Practice Debugging your code as an interactive lesson.
Try the lessonDefinition
When you write code, sometimes it does not do what you expected. A bug is a mistake in your code — it could be a typo, a step in the wrong order, or a missing piece. Debugging is the process of reading through your code carefully, figuring out what went wrong, and fixing it so your program runs correctly.
Remember the rule
Read, Run, Find, Fix — read your code, run it, find the bug, fix it, then run it again!
Key words
- Bug
- A mistake or error in your code that makes the program act the wrong way.
- Debug
- To find and fix the bugs in your code.
- Code
- Instructions you write to tell a computer what to do.
- Error message
- A note the computer gives you when it cannot understand or run part of your code.
- Loop
- A part of code that repeats the same steps over and over.
- Syntax
- The spelling and punctuation rules of a coding language — like grammar for code.
- Test
- Running your program to see if it works the way you wanted.
- Step-through
- Going through your code one line at a time to find where the problem is.
Worked examples
You write a Scratch program to move a cat 10 steps, but the cat does not move at all. What do you check first?
→ Check that the 'move 10 steps' block is actually connected to the 'when green flag clicked' block. If the blocks are not snapped together, the code will never run. · Blocks that are not connected are one of the most common bugs in Scratch.
Your program is supposed to say 'Hello, World!' but it says 'Helo, World!' instead. What is the bug?
→ There is a typo — the word 'Hello' is missing the second 'l'. Fix the spelling inside the text block and run it again. · Typos in text or code are called syntax bugs.
You want a character to repeat a dance move 5 times, but it only dances once. You used a repeat block. What might be wrong?
→ Check the number inside the repeat block. If it says 'repeat 1' instead of 'repeat 5', change the 1 to a 5 and test again.
Your quiz program always says the answer is wrong even when the player types the right answer. What should you check?
→ Check that the correct answer stored in your code matches exactly what the player types, including capital letters and spaces. If your code says 'Cat' but the player types 'cat', some programs treat them as different. · Capitalization mismatches are a sneaky bug that is easy to miss.
You run your program and see a red error message that says 'missing end block.' What does that mean?
→ Your code has an opening block or command but no closing block to finish it. Find where the loop or section starts and add the missing end block to close it.
A program that counts from 1 to 5 never stops — it keeps counting forever. What is the bug?
→ The loop does not have a stopping rule. Add a condition that says 'stop when the count reaches 5' so the loop knows when to end. · A loop that never stops is called an infinite loop — it is a very common beginner bug.
Common mistakes
- Forgetting to connect blocks together so the code never actually runs.
- Changing too many things at once when fixing bugs — change one thing, then test, so you know what worked.
- Ignoring the error message instead of reading it — error messages are clues that point you right to the problem.
- Giving up after the first try — debugging takes patience and most programmers fix several bugs before the program works.
- Not testing after each fix — always run your program again after you make a change to see if the bug is really gone.
FAQs
Why is a mistake in code called a 'bug'?
Long ago, a real moth got stuck inside an early computer and caused it to stop working. The engineers called it a bug, and the name stuck for any mistake in code ever since.
What should I do first when my code does not work?
Read your code slowly from the very beginning, one step at a time, and ask yourself: 'Is this telling the computer exactly what I want?' Most of the time you will spot the mistake just by reading carefully.
Is it normal to have lots of bugs?
Yes! Even professional programmers write bugs every single day. Debugging is a normal and important part of coding, not a sign that you are doing it wrong.
What if I cannot find the bug no matter what?
Try explaining your code out loud, step by step, as if you are teaching it to a friend or a stuffed animal. This is called rubber duck debugging, and it often helps you spot the mistake yourself.
Can the computer find bugs for me?
The computer can find some bugs and show you an error message with a clue. But many bugs will still run without an error — the program just does the wrong thing. That is why you always need to test your program yourself.
How do I know my code is finally fixed?
Run your program and check that it does exactly what you planned every time. Try it a few different ways to make sure. If it works correctly each time, your bug is fixed!
Want the whole picture for your child?
Every K–6 subject, an AI tutor that teaches step by step, unlimited practice, and a reward world.
Start a 3-day free trial