What? It Wasn't Supposed To Do That! Finding Logic Errors

In this lesson, you will learn how find logic errors in your program.

In previous lessons you learned how to find and fix compiler errors and run-time errors. The third type of programming error, logic errors, can be the most difficult to discover. With logic errors you get no warning at all—the program will run but give incorrect results. You then have to read through your code and determine why.

Fortunately, the debugging tools in Visual Basic can help. Two debugging techniques, setting breakpoints and stepping through code, help you to inspect your code one line at a time as it executes to find the error.

You can set a breakpoint in the Code Editor on any executable line of code. When the program is run, breakpoints force it to stop and go into break mode when that line of code is reached. You can then get whatever information you may want about the state of the program at that point in time. You can check the value of any variable, test expressions in the Immediate window, or make changes to your code with Edit and Continue.

Once you are in break mode, you can also step through your code, executing one line at a time so that you can see how the code works. Pressing the F8 key causes the current line of code to execute and then stop at the next line. You can then inspect the values of variables to see how they change from one line to the next.

If the current line of code calls a function or Sub procedure elsewhere in your code, when you press F8, execution will step into that procedure. Once you have stepped through that procedure, you are sent to the line after the one that called the procedure. If you do not want to step through a procedure, you can press SHIFT+F8 to step over the procedure.

Try It!

To observe a logic error

  1. On the File menu, choose New Project.

  2. On the Templates pane in the New Project dialog box, click Windows Application.

  3. In the Name box, type LogicErrors and then click OK.

    A new Windows Forms project opens.

  4. From the Toolbox, drag two TextBox controls and a Button control onto the form.

  5. Double-click Button1 to open the Code Editor.

  6. In the Button1_Click event handler, add the following code.

    Dim minutes As Integer = CInt(Textbox1.Text)
    Dim miles As Double = CDbl(Textbox2.Text)
    Dim hours As Double = 0
    hours = minutes / 60
    MsgBox("Average speed " & GetMPH(hours, miles))
    
  7. Below the End Sub line, add the following function.

    Function GetMPH(ByVal miles As Double, ByVal hours As Double) _
    As String
        GetMPH = CStr(miles / hours)
    End Function
    
  8. Press F5 to run the program. In the first text box, enter 10 (to represent 10 minutes) and in the second text box, enter 5 (to represent 5 miles), and then click Button1.

    A message box is displayed with the message "Average speed 0.03333334"; however, if you are traveling five miles in ten minutes, the correct answer would be 30 mph.

    Keep the project open—in the next procedure, you will learn how to find the logic error.

Finding Logic Errors

In the last example, something is obviously wrong with the program logic. According to the result, you are traveling less than one mile per hour, not thirty miles per hour as you would expect—but where is the error?

In the next procedure, you will set a breakpoint and step through the code to find the error.

Try It!

To set a breakpoint and step through code

  1. In the Code Editor, find the line hours = minutes / 60, and then click in the left-hand margin next to that line of code.

    A red dot should appear in the margin, and the code should be highlighted in red, representing a breakpoint.

  2. Press F5 to run the program again. In the first text box, enter 10, and in the second text box, enter 5. Then click Button1.

    The program stops when it reaches the breakpoint. The line hours = minutes / 60 is highlighted in yellow.

    Inspect the values of the variables by holding the mouse over them—the value of hours should be 0, and the value of minutes should be 10.

  3. Press F8 to execute the line hours = minutes / 60, and step to the next line.

    Inspect the values of the variables in the line MsgBox("Average speed " & GetMPH(hours, miles))—the value of hours should now be 0.166666672, and the value of miles should be 5.0.

  4. Press F8 again to execute the current line.

    Notice that the execution steps down to the line Function GetMPH.

    Inspect the values of the variables on this line—you should notice that the value of miles is now 0.166666672, and the value of hours is now 5.0, the opposite of what they were on the previous line. You've found the error.

    Keep the project open—in the next procedure, you will learn how to fix the logic error.

Fixing Logic Errors

In the preceding procedure, the values for the variables miles and hours switched places. Can you spot the cause?

If you look at the line MsgBox("Average speed " & GetMPH(hours, miles)), you will see that the GetMPH function is being passed two arguments, hours and miles, in that order. If you look at the function declaration Function GetMPH(ByVal miles As Double, ByVal hours As Double)..., you will notice that the arguments are listed as miles first and hours second.

An error in logic occurred because the arguments were passed in the wrong order, resulting in an incorrect calculation. If the arguments had been of different types, you would have seen a run-time error; because the arguments were the same type, no run-time error occurred. It was a simple mistake, but the resulting bug was difficult to find.

In the next procedure, you will set a breakpoint and step through the code to find the error.

Try It!

To fix the logic error

  1. In the Code Editor, change the line MsgBox("Average speed " & GetMPH(hours, miles)) to read as follows:

    MsgBox("Average speed " & GetMPH(miles, hours))
    
  2. Clear the breakpoint by clicking the red dot in the left-hand margin.

  3. Press F5 to run the program. In the first text box, enter 10, and in the second text box, enter 5. Then click Button1.

    This time the message box should display the correct result, "Average speed 30".

    It may appear that the program is fixed, but there is another even harder-to-find logic error. If you want to try and find it, keep the project open—you will use it again in the lesson Another Bug: Something Is Still Wrong.

Next Steps

In this lesson, you learned how to find and fix a logic error. At this point, you can go on to the next lesson on using comments, or you can try your hand at finding another logic error in Another Bug: Something Is Still Wrong.

Next Lesson: Making Notes in Your Programs: Using Comments

See Also

Tasks

It Doesn't Work! Finding and Eliminating Run-Time Errors

Know Your Bugs: Three Kinds of Programming Errors

Finding the Errors: Introduction to Visual Basic Debugging