Idea 18of 23Builds onPredict before you run
Find the bug, fix the line
How do you fix code that gives the wrong answer?
Today you'll discover
- how to spot that code is wrong
- how to find the line
- how to check the fix
Think firstAli has $15 and saves $5 a week for 4 weeks
The code outputs $5, but Ali should have $35. Which line is wrong?
Decide first, then press Next. Compare the output with the goal, then read line by line.
Ali's savings code on the left: SET 15, REPEAT 4 TIMES with SET 5 inside, END, OUTPUT; a table shows its output $5 beside the goal $35. Then the fixed code on the right with ADD 5 in place of SET 5, the bug and the fix framed, and its output $35 marked as meeting the goal. The last step asks how to fix sticker code that gives 20 instead of 14.
See the whole ideathe big idea and the key word, in one place
The big idea
A bug is a line that makes code miss its goal; debug by finding and fixing that line.
Debugging, step by step
See the output miss the goal, trace the code, find the line, fix that line, and run it again to check.
- Output$5
- Goal$35
- BugSET 5
- FixADD 5
Easy to get wrongFix the code, not the answer. Changing the start to $35 hides the bug instead of fixing it.
Key words
- bug
- a mistake in code that makes it give the wrong result
- debug
- to find and fix the bugs in code
Ready to use it?
- Find the line that makes code miss its goal, and fix it.