
Think together · First, a short story
The girl asks a scorekeeper to increase a score from 3 to 5, but the screen shows 1. The robot suggests finding the first step that differs.
Lay out the process
- Expected3 plus 2 is 5
- ActualThe program subtracted
- LocateCheck the score update
- FixRetest after the change
A bug is a flaw in a program that can produce a wrong result or prevent it from running.
When debugging, write down what you expect first, then observe each step. The sooner you find where behavior diverges, the easier it is to understand the cause.
01What exactly is a bug?
A bug is generally a flaw or error in a program that makes it behave in an unexpected way.
02What kinds of errors can programs have?
Compilation error
The code does not follow language rules.
Runtime error
An exception or invalid operation occurs while running.
Logic error
The program runs, but gives the wrong answer.
Boundary issue
It fails only on special inputs.
03What is a compilation error?
int x = 10
std::cout << x;The first line is missing a semicolon, so the compiler reports an error. The reported line number is a clue; sometimes the real cause is on the line above.
04What is a runtime error?
The program has started, but something goes wrong while it runs—for example, an invalid memory access or insufficient resources.
05Why are logic errors hardest to find?
if (score > 60) {
std::cout << "Pass";
}If the rule says that a score of 60 also passes, this should use >= 60. The program compiles and runs, but the rule is implemented incorrectly.
06What happens during debugging?
Debugging is not changing code at random; it is using evidence to narrow down the problem step by step.
07How can you reduce bugs?
Work in small steps
Add only a little logic at a time.
Test early
Do not wait until everything is written.
Test boundaries
Consider 0, 1, maximum values, and more.
Read error messages
They often contain useful clues.
What have you learned?
- A bug is a flaw or error in a program.
- Common issues include compilation errors, runtime errors, logic errors, and boundary cases.
- Successful compilation does not guarantee that a program is correct or safe.
- Debug by reproducing, observing, locating, fixing, and verifying.
- Good testing habits can significantly reduce bugs.
Your turn to try
After fixing an error, should you stop after looking at the code, or run it again? Why?
Ready? Open the explanation
Run the failing example again and add related boundary cases. This confirms the fix and can reveal new problems introduced by the change.