Computer ScienceChapter 3: Programming Fundamentals
3.5 Debug The Code
Debugging:
Explanation level
This topic explains debugging—the process of locating and fixing errors (bugs) in code—and how to use debugging tools in Visual Studio.
Definition
Debugging: Debugging refers to locating an error or a bug in the code.
Core Concept
Debugging is like being a detective for your code. When your program doesn't work as expected, you need to find out why. A bug is an error in the code that causes incorrect behavior. Debugging is the process of finding that bug, understanding why it happens, and fixing it. Just as a detective gathers clues and follows leads, a programmer uses debugging tools—breakpoints, step-by-step execution, and variable inspection—to track down and eliminate bugs.
Detailed Explanation
What is a Bug?
A bug is an error, flaw, or fault in a computer program that causes it to produce an incorrect or unexpected result, or to behave in unintended ways.
- Syntax Errors: Mistakes in the grammar of the language (e.g., missing semicolon, misspelled keyword).
- Logical Errors: The code runs but produces wrong results (e.g., using `+` instead of `-`).
- Runtime Errors: Errors that occur while the program is running (e.g., dividing by zero, accessing undefined variable).
Debugging in Visual Studio
In Visual Studio .NET, select Run from the menu and choose Run Debugging. Thereafter, you will get the Debugging menu with some buttons and a debug console.
Key Debugging Tools:
| Tool | Purpose |
|---|---|
| Pause/Continue | Pauses or resumes the execution of the program |
| Step Into | Goes through the code line by line, as the program would normally execute |
| Step Over | Executes the current line and moves to the next line (does not go inside functions) |
| Step Out | Finishes the current function and returns to the calling code |
| Restart | Restarts the debugging session |
| Stop | Stops the debugging session |
Breakpoints
To emphasize lines where you want to check the values of variables, you can assign a breakpoint.
- A breakpoint is shown as a red dot at the start of a line in Visual Studio.
- You can assign multiple breakpoints for the sake of traceability.
- While the code is executed in debug mode, the execution halts at every breakpoint such that the behavior of code according to the values can be analyzed.
- This way, it becomes easier to trace a bug and fix it accordingly.
- Click in the left margin next to the line of code.
- A red dot appears, indicating a breakpoint.
- Run the program in debug mode.
- The program pauses at the breakpoint.
- Inspect variables, step through code, and find the bug.
Debugging Menu Options
Toggle Breakpoint (F9): Adds or removes a breakpoint on the current line.
New Breakpoint: Creates a new breakpoint with specific conditions.
Enable All Breakpoints: Enables all breakpoints.
Disable All Breakpoints: Disables all breakpoints temporarily.
Remove All Breakpoints: Deletes all breakpoints.
Install Additional Debuggers: Allows installing debuggers for other languages.
Debug Console
- Print variable values.
- Check the flow of execution.
- Identify where errors occur.
Steps to Debug Code
- Identify the Bug: Notice that the program is not working correctly.
- Set Breakpoints: Place breakpoints at suspected lines.
- Run in Debug Mode: Start the program with debugging enabled.
- Inspect Variables: Check the values of variables at each breakpoint.
- Step Through Code: Use Step Into/Step Over to follow execution.
- Find the Bug: Locate the line causing the error.
- Fix the Bug: Correct the code.
- Re-run: Test again to ensure the bug is fixed.
Example: Debugging a Simple Program
Buggy Code: ```javascript var a = 5; var b = 10; var sum = a - b; // Bug: should be a + b document.write("Sum is: " + sum); ```
Expected Output: Sum is: 15 Actual Output: Sum is: -5
- Set a breakpoint on the line `var sum = a - b;`.
- Run in debug mode.
- When execution pauses, check the values of `a` and `b` (5 and 10).
- Step over the line and check `sum` (-5).
- Notice the bug: `-` should be `+`.
- Fix the code: `var sum = a + b;`.
- Re-run: Output is now "Sum is: 15".
Common Debugging Techniques
| Technique | Description |
|---|---|
| Breakpoints | Pause execution at specific lines |
| Step Into | Go line by line into functions |
| Step Over | Go line by line, skipping function internals |
| Watch Variables | Monitor specific variable values |
| Console Logging | Use `console.log()` to print values |
| Code Review | Read code carefully to spot errors |
Intermediate Level
Debugging is the process of locating and fixing bugs in code. Bugs can be syntax errors (grammar mistakes), logical errors (wrong results), or runtime errors (crashes during execution). Visual Studio provides tools like breakpoints (pause execution at specific lines), Step Into (go line by line into functions), Step Over (skip function internals), and debug console (view output). Breakpoints are shown as red dots. You can set multiple breakpoints to trace the flow. The debugger halts at each breakpoint, allowing you to inspect variables and find the bug. After fixing, re-run to verify.
Important Points
- - Debugging = locating and fixing bugs.
- - Bug = error in code.
- - Types of bugs: Syntax, Logical, Runtime.
- - Visual Studio debugging: Run → Run Debugging.
- - Breakpoints: red dots; pause execution.
- - Step Into: line by line, into functions.
- - Step Over: line by line, skipping function internals.
- - Debug Console: shows output during debugging.
- - Toggle Breakpoint: F9.
Remember
- - Debugging: Find and fix bugs.
- - Breakpoint: Red dot; pauses execution.
- - Step Into: Go into functions.
- - Step Over: Skip function internals.
- - Debug Console: View output.
- - Toggle Breakpoint: F9.
- - Types of Bugs: Syntax, Logical, Runtime.
Common Mistakes
- - Not using breakpoints: Debugging without pausing is like finding a needle in a haystack.
- - Confusing Step Into and Step Over: Step Into enters functions; Step Over skips them.
- - Forgetting to remove breakpoints: Can slow down development.
- - Ignoring the debug console: It provides valuable output.
- - Fixing the symptom, not the cause: Always find the root cause.
Quick Revision
Debugging is the process of locating and fixing bugs in code. Bugs can be syntax errors, logical errors, or runtime errors. Visual Studio provides debugging tools: breakpoints (red dots that pause execution), Step Into (go line by line into functions), Step Over (skip function internals), and debug console (view output). Breakpoints are set by clicking the left margin, toggled with F9. The debugger halts at each breakpoint, allowing you to inspect variables and find the bug. After fixing, re-run to verify.
60-Second Revision
- Breakpoint: Red dot; pauses.
- Step Into: Into functions.
- Step Over: Skip functions.
- Debug Console: Output.
Did you understand this topic?
Your answer is saved with your learning record. It does not mark the topic as mastered.
Quick revision
Revision Zone
Review the key facts, formulas and checks before you move on.3.5 Debug The Code
Sticky Notes · Quick Revision
Key Point 1
- Debugging: Find and fix bugs.
Key Point 2
- Breakpoint: Red dot; pauses.
Key Point 3
- Step Into: Into functions.
Key Point 4
- Step Over: Skip functions.
Key Point 5
- Debug Console: Output.
Key Point 6
- Toggle Breakpoint: F9.
Key Point 7
- Bugs: Syntax, Logical, Runtime.
Same supplied content as this Notes page (shared canonical source).