Home > Articles > Programming > Windows Programming

  • Print
  • + Share This
Like this article? We recommend

Like this article? We recommend

Using Breakpoints

A breakpoint is a location in your code where the program will pause (break). While the program is paused, you can take various actions to help find a bug, as I’ll describe shortly. To set a breakpoint (or remove an existing one) in Visual Studio, place the editing cursor on the line of code and press F9. While you’re editing a program, lines with a breakpoint display with a colored background and a small circle icon in the margin of the editing window. When a running program encounters a breakpoint, the editing window is displayed with the line of code highlighted, as shown in Figure 1.

Figure 01

Figure 1 Breakpoint lines are highlighted in the editor.

Once you’ve set a breakpoint, you can modify it to be conditional. This means that the breakpoint will cause the program to pause only under certain conditions. To make a breakpoint conditional, right-click the line of code with the breakpoint and select Breakpoint from the context menu. Then select either Condition or Hit Count to get a few more options (other items on the context menu are beyond the scope of this article):

  • If you selected Breakpoint > Condition, Visual Studio opens the Breakpoint Condition dialog box shown in Figure 2. You enter the condition where indicated. What exactly is a condition? It’s related to the state of the variables in your program. For example, if you have a variable named Count that’s in scope at the breakpoint location, and you want to pause the program only if the value of Count is greater than 100, you would enter the following for the condition:
    Count > 100

    Select the Is True option in the Breakpoint Condition dialog box if you want to pause whenever the condition is true. Select the Has Changed option to pause only when the condition has changed (from true to false, or vice versa).

    Figure 2

    Figure 2 Specifying a condition for a breakpoint.

  • If you selected Breakpoint > Hit Count, Visual Studio opens the Breakpoint Hit Count dialog box shown in Figure 3. Here you can specify that the program stops at this breakpoint only if the hit count fits one of the following conditions:
    • Equal to a particular value
    • A multiple of a particular value
    • Greater than or equal to a particular value

    The hit count is the number of times that execution has passed this line of code since the program started.

    Figure 3

    Figure 3 Specifying a hit count for a breakpoint.

    Be aware that the program stops before executing the line of code containing the breakpoint. The line with the breakpoint is executed only when the program continues.

  • + Share This
  • 🔖 Save To Your Account