Debugging by Testing Each Step
When something in your code or instructions does not work, you fix it by checking one step at a time until you find the problem.
Reading is good — doing is better. Practice Debugging by Testing Each Step as an interactive lesson.
Try the lessonDefinition
Debugging means finding and fixing mistakes in a set of instructions (called code or an algorithm). Testing each step means you run or check your instructions one piece at a time, in order, so you can spot exactly where things go wrong instead of guessing.
Remember the rule
Check one step, see what happens, fix it if wrong, then move to the next step.
Key words
- Bug
- A mistake in a set of instructions that makes it do the wrong thing.
- Debug
- To find and fix a bug so the instructions work correctly.
- Step
- One single action in a list of instructions.
- Algorithm
- A set of instructions written in order to complete a task.
- Test
- To try out a step to see if it does what you expected.
- Error
- When something goes wrong or gives the wrong result.
- Expected result
- What you think should happen when a step runs correctly.
- Actual result
- What really happens when you run the step.
Worked examples
You write these directions for a robot: 1. Move forward 3 steps. 2. Turn right. 3. Move forward 2 steps. 4. Pick up the ball. The robot ends up in the wrong spot and misses the ball. How do you debug it?
→ Test step 1 alone: does the robot move forward exactly 3 steps? Yes, that looks right. Test step 2: does the robot turn right? Yes. Test step 3: does the robot move forward 2 steps? No! It only moves 1 step. Fix step 3 to say 'Move forward 2 steps' with the correct number in the program. Now test step 4 and it works. · By testing one step at a time you found the bug was in step 3, not steps 1, 2, or 4.
A Scratch project is supposed to make a cat walk across the screen and then say 'Hello!' but the cat never says anything. There are 3 blocks: Move 100 steps, Wait 1 second, Say Hello for 2 seconds. How do you debug?
→ Click just the Move block: the cat moves — good. Click just the Wait block: it pauses — good. Click just the Say block by itself: nothing appears on screen. That block is the problem. Check it and notice the Say block was accidentally set to 0 seconds instead of 2. Change it to 2 seconds and test again. Now the cat says Hello. · Testing the Say block alone proved the other two blocks were fine and saved time.
You are giving a friend directions to your desk: 1. Walk to the door. 2. Turn left. 3. Walk 4 desks forward. 4. Sit down. Your friend ends up at the wrong desk. How do you find the bug?
→ Watch your friend do step 1: they walk to the door correctly. Step 2: they turn left correctly. Step 3: they walk 4 desks but land at desk 5 because the starting count was wrong — the bug is here. Fix step 3 to say 'Walk 3 desks forward.' Test again and your friend reaches the right desk.
A simple adding program should do: 1. Start with 5. 2. Add 3. 3. Add 2. 4. Show the answer. It shows 11 instead of 10. Where is the bug?
→ Test step 1: start value is 5 — correct. Test step 2: 5 plus 3 equals 8 — correct. Test step 3: 8 plus 2 equals 10 — correct. Test step 4: the display shows 11. The bug is in step 4. The show-answer block has a typo and adds 1 extra before displaying. Fix the display block and it shows 10. · Even if all the math steps are right, the last step can still have a bug.
Your Scratch sprite is supposed to turn blue, wait 1 second, and then grow bigger, but it grows bigger right away without waiting. What do you test?
→ Test the change-color block alone: it turns blue — good. Test the wait block alone: does it actually pause? No! The wait is set to 0 seconds instead of 1. Fix the wait to 1 second. Test again and the sprite waits before growing.
Common mistakes
- Changing many steps at once instead of one at a time, so you cannot tell which fix actually worked.
- Skipping the test after each fix and then not knowing if the step is really correct.
- Giving up and starting over completely before testing each step — the bug is usually just in one small place.
- Forgetting to check the very first step and assuming the beginning is always right.
- Not comparing what actually happened to what was supposed to happen, so you miss the clue that points to the bug.
FAQs
Why can't I just look at all my code at once to find the bug?
When instructions are long, it is very hard to spot one small mistake by reading everything. Testing one step at a time is like a treasure hunt — you follow the clues in order until you find exactly where the problem hides.
Do I have to test every single step every time?
You test steps one at a time until you find the step where the actual result does not match the expected result. Once you find and fix the bug, you run all the steps together to make sure everything works.
What if fixing one step breaks a different step?
That is why you test again from the beginning after every fix. A good rule is: fix one thing, then test all the steps again in order.
Is debugging a sign that I did something wrong?
No! Every programmer, even professional ones, finds bugs. Debugging is a normal and important skill. Finding a bug and fixing it means you are thinking like a real coder.
How do I know what the expected result should be?
Before you run a step, ask yourself: if this step is correct, what should I see happen? Write it down or say it out loud. Then run the step and compare.
What if I cannot figure out which step has the bug?
Start at step 1 and test very carefully. Draw a small table with two columns: Expected and Actual. Fill it in for each step. The first row where Expected and Actual are different is where your bug lives.
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