A bug is an error in a program that makes it behave differently from what it should. Some bugs stop a program from starting, some make it crash while it runs, and some let it run but give wrong results without any warning.
Three common types are syntax errors, runtime errors and logic errors. A syntax error breaks the grammar of the language, so the program cannot be built or started. A runtime error happens while the program runs, such as dividing by zero. A logic error runs without crashing but produces wrong results.
Debugging is a systematic search. A good debugger reproduces the problem, reads the error message, narrows down where the fault is, forms a hypothesis about the cause, tests a fix, and checks related cases.
Inside your account
What an account adds to the pages you are reading.
Lessons
Topic explanations with worked examples, split into units.
A bug can be reproduced only when you know the exact input and steps that cause it.
An error message often names the file and line where the problem was detected. That is a starting point, not always the cause.
Print statements or a step-by-step debugger show the values of variables as the program runs.
Testing a fix with several inputs, including edge cases, helps prevent new bugs.
Writing down the expected result before running the program makes wrong results easier to notice.
Example
Question: a program computes an average as total divided by count, using whole numbers. For a total of 7 and a count of 2, what appears in languages such as C# or Java? Whole-number division gives 3, not 3.5. The fix is to use a decimal type or convert one of the values first.
Question: the same program crashes when the count is 0. Why? Dividing by zero has no defined result, so the program should check the count before dividing, and stop with a clear message if it is 0.
Common mistakes
Changing code at random until the error disappears. This can hide the cause and create new bugs.
Ignoring the first error message. Later errors are often caused by the first one.
Fixing the symptom but not the cause. The result may look correct for one input and still fail for others.
Skipping the test after a fix. Every repair must be checked again.
Summary
Bugs are program errors that cause wrong or unexpected behavior. Debugging means reproducing the problem, reading the messages, isolating the cause, testing a fix and checking related cases. A patient, step-by-step method works better than guessing.
Frequently asked questions
What is the difference between a syntax error and a logic error?
A syntax error breaks the language rules, so the program will not run until it is fixed. A logic error follows the rules and runs, but the program does not do what it was meant to do.
Is a bug always caused by the code I just wrote?
Not always. Bugs can come from unusual input, from data, from another part of the program, or from the environment. Check recent changes first, but test your assumptions too.
What is a breakpoint?
A breakpoint is a marked line where a debugger pauses the program, so you can inspect variable values before the next line runs.