Chapter 3 Readings: 3.12 Using the Debugger to Locate Logic Errors
Debugging and Logic Errors in Visual Studio
The Visual Studio debugger provides tools to pause application execution and step through statements one at a time. Following each statement execution, variable contents and control property values can be inspected.
Definition and Characteristics of Logic Errors
Logic Error Definition: A mistake that does not prevent an application from running, but causes the application to produce incorrect results.
Common Examples:
Mathematical calculations written incorrectly.
Assigning a value to the wrong variable.
Assigning the incorrect value to a variable.
Detection Difficulty: Logic errors are notoriously difficult to locate and require active detective work by the programmer. Debugging tools in Visual Studio automate and simplify this diagnostic process.
Key Debugging Concepts
Breakpoint: A specific line selected by the programmer in the source code. When execution reaches a breakpoint, the application pauses and enters break mode.
Break Mode: A paused state during application execution wherein variable contents and control property values can be freely examined.
Single-Stepping: The process of executing code statements one at a time under explicit human control after pausing at a breakpoint. This isolates the precise statement causing runtime logic failures.
Tutorial 3.6: Single-Stepping through an Application's Code at Runtime
Overview and Test Setup
Application Name:
Average Race Times(located in theChap03folder ofStudent Sample Programs).Application Functionality: Prompts the user to enter finishing times for three runners and calculates their average time upon clicking a button.
Test Verification Strategy: Enter an identical value of for all three runners. The expected calculated average is . Any value generated other than confirms the existence of a logic error.
Step-by-Step Procedure
Step 1: Open Project
Start Visual Studio. Open the project named Average Race Times located in the Chap03 folder of the Student Sample Programs directory.
Step 2: Run Application
Run the application to display the main application form.
Step 3: Enter Test Data
Enter the value as the finishing time for all three runners.
Step 4: Test Calculation
Click the Calculate Average button. The application displays an incorrect calculated result of instead of the expected value of .
Step 5: Exit Application
Click the Exit button to halt application execution.
Step 6: Locate Pause Line
Open the code editor (View -> Code). Locate the target line within the calculateButton_Click event handler: runner1 = double.Parse(runner1TextBox.Text);
Step 7: Set Breakpoint
Set a breakpoint on the target line using one of the following methods:
Mouse Method: Click in the left margin of the code editor directly adjacent to the statement line. A red dot appears in the margin and the line highlights.
Keyboard Method: Position the cursor on the target line and press
F9.
Step 8: Re-run with Breakpoint
Run the application. When the form appears, enter for each runner's time.
Step 9: Trigger Break Mode
Click Calculate Average. Execution pauses upon reaching the breakpoint, entering break mode and bringing up the code editor:
The breakpoint line is displayed with yellow highlighting, and a small yellow arrow appears in the left margin.
This marks the execution point: the line of code scheduled to execute next (it has not executed yet).
Note: If highlighting or arrows display in colors other than yellow, system color scheme options have been customized.
Step 10: Inspect Control Property Values
Hover the mouse pointer over runner1TextBox.Text in the highlighted statement. A tool-tip box displays its current value: "25".
Step 11: Inspect Variable Values Before Execution
Hover the mouse pointer over the variable name runner1. A tool-tip box displays because the statement has not yet executed.
Step 12: Utilize Inspection Windows
Visual Studio includes specialized windows for variable inspection:
Autos Window: Lists variables appearing in the current statement, the statements preceding it, and the statements following it. Displays current values and data types.
Locals Window: Lists all variables active in the current procedure, alongside their current values and data types.
Watch Window: Displays specified variables explicitly added by the programmer. Multiple Watch windows can be opened simultaneously.
Immediate Window: Accepts keyboard debugging commands; used primarily by advanced programmers.
Window Access: Select
Debugon the menu bar, navigate toWindows, and choose the desired window (e.g.,Locals).
Step 13: Execute First Single-Step
Execute statements sequentially using the Step Into command (distinct from Step Over, covered in Chapter 6):
Step Into Methods: Press
F11OR selectDebug->Step Into.Press
F11. The statement executes. In theWatchorLocalswindow,runner1updates to . The next statement highlights as the execution point.
Step 14: Step Through Input Statements
Press F11 two additional times. The Locals window confirms variables runner1, runner2, and runner3 all store values of .
Step 15: Step to Calculation Statement
The execution point highlights the mathematical formula line: average = runner1 + runner2 + runner3 / 3.0; Press F11 to execute this line.
Step 16: Identify and Correct Logic Error
The Locals window reports that variable average holds .
Mathematical Error Analysis: Operator precedence causes division to evaluate before addition:
Correction Procedure:
Select
Debug->Stop Debuggingto halt the application.Enclose the additions in parentheses in the code editor:
average = (runner1 + runner2 + runner3) / 3.0;
Step 17: Clear Breakpoint
Clear the breakpoint using one of three techniques:
Click the red breakpoint dot in the left margin of the code editor.
Press
Control + Shift + F9.Select
Debug->Delete All Breakpoints.
Step 18: Verify Fix
Run the application again. Enter for each runner and click Calculate Average. The form displays the correct result: .
Step 19: Terminate Application
Click the Exit button to stop the program.
Debugging Windows and Controls Overview
Autos Window Scope: Current statement plus statements prior and statements after.
Locals Window Scope: All variables defined within the scope of the current procedure.
Watch Window Scope: Custom user-selected variables across execution.
Toolbar Commands: Visual Studio provides dedicated toolbar buttons for common debugging commands.
Checkpoint Questions
Checkpoint 3.51: What happens if you press the Enter key while a Button control has the focus?
Checkpoint 3.52: How do you display a form in tab order selection mode? How do you exit tab order selection mode?
Checkpoint 3.53: Write a programming statement that gives the focus to a TextBox control named
numberTextBox.Checkpoint 3.54: How do you assign an access key to a Button control?
Checkpoint 3.55: How do you display an ampersand (
&) character on a Button control?Checkpoint 3.56: Write the code that will change the
BackColorproperty of a Label control namedresultLabelto the color white and theForeColorproperty to the color red.Checkpoint 3.57: List the different values of a form’s
BackgroundImageLayoutproperty.Checkpoint 3.58: When a
GroupBoxcontrol is deleted, what happens to the controls that are inside?Checkpoint 3.59: How are the
TabIndexproperties of the controls inside the group box organized?Checkpoint 3.60: How is a
Panelcontrol different from aGroupBoxcontrol?