Logical Errors

There is one more type of error that we should discuss, since it is the most insidious type of all. A logical error can be defined as the production of an unexpected and incorrect result. As far as Excel is concerned, there is no error, because Excel has no way of knowing what we intend. (Thus, a logical error is not a run-time error, in the traditional sense, even though it does occur at run time.)

To illustrate, the following code purports to compute the average of some numbers:

Dim x(3) As Integer

Ave = (x(0) + x(1) + x(2) + x(3)) / 3 MsgBox "Average is: " & Ave

The result is the message box shown in Figure 4-7. Unfortunately, it is incorrect. The penultimate line in the preceding program should be:

Note the 4 in the denominator, since there are 4 numbers to average. The correct average is 4.25. Of course, Excel will not complain because it has no way of knowing whether we really want to divide by 3.

0 0

Post a comment