Welcome to coding! Today, we will learn how to find and fix errors in simple algorithms using a process called debugging.
An algorithm is a step-by-step set of instructions designed to complete a task. When an algorithm contains a mistake, programmers call that mistake a bug. Debugging is the process of locating, analyzing, and removing those errors so your program runs correctly.

To debug effectively, you must first identify where the logic breaks down. For example, if a robot takes two steps forward instead of three, you compare the expected outcome with the actual result. Tracing each line of code step by step helps reveal where the mistake happens.
Once you isolate the bug, you remove the faulty instruction and replace it with the correct logic. Finally, you re-run the algorithm with sample inputs to verify that the bug is completely resolved.
Debug this algorithm that calculates total price for 3 items: 1. Set total = 0 2. Add item_1 to total 3. Add item_2 to total 4. Multiply total by 0 (Bug!) 5. Return total
- Identify the goal: The algorithm should add three items to calculate the correct sum.
- Trace the instructions: Step 4 multiplies the running total by 0, which clears the entire amount to 0.
- Isolate the bug: Step 4 contains an incorrect operator and missing step (it should add item_3 instead of multiplying by 0).
- Remove the error: Delete step 4 ('Multiply total by 0').
- Insert the correct logic: Replace it with 'Add item_3 to total'.
- Test the fix: Set item_1=5, item_2=5, item_3=5. Total becomes 15, confirming the bug is fixed.
