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 the Chap03 folder of Student 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 2525 for all three runners. The expected calculated average is 2525. Any value generated other than 2525 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 2525 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 58.358.3 instead of the expected value of 2525.

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 2525 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 00 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 33 statements preceding it, and the 33 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 Debug on the menu bar, navigate to Windows, 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 F11 OR select Debug -> Step Into.

  • Press F11. The statement executes. In the Watch or Locals window, runner1 updates to 2525. 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 2525.

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 58.33333333333333658.333333333333336.

  • Mathematical Error Analysis: Operator precedence causes division to evaluate before addition: average=runner1+runner2+runner33.0\text{average} = \text{runner1} + \text{runner2} + \frac{\text{runner3}}{3.0}

  • Correction Procedure:

    1. Select Debug -> Stop Debugging to halt the application.

    2. 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 2525 for each runner and click Calculate Average. The form displays the correct result: 25.025.0.

Step 19: Terminate Application

Click the Exit button to stop the program.

Debugging Windows and Controls Overview

  • Autos Window Scope: Current statement plus 33 statements prior and 33 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 BackColor property of a Label control named resultLabel to the color white and the ForeColor property to the color red.

  • Checkpoint 3.57: List the different values of a form’s BackgroundImageLayout property.

  • Checkpoint 3.58: When a GroupBox control is deleted, what happens to the controls that are inside?

  • Checkpoint 3.59: How are the TabIndex properties of the controls inside the group box organized?

  • Checkpoint 3.60: How is a Panel control different from a GroupBox control?