Wyo VB Lecture Notes
Objective #1: Use While loops.
intCounter = -1
intTotal = 0
intExam = 0
While (intExam <> -99)
intTotal = intTotal + intExam
intCounter = intCounter + 1
intExam = InputBox("Enter your next exam score (-99 to quit):")
End While
intExamAverage = intTotal / intCounter
Note that in the example above the user is prompted
by the InputBox to enter -99 to quit. Notice the use of an accumulator statement (intTotal
= intTotal + intExam)
as well as the use of a counter statement (intCounter = intCounter + 1). The loop allows the user to enter all of his or her exam scores.
After the loop finishes iterating, the exam average is computed and stored in intExamAverage. Do you know why the variable intCounter is
initialized to -1 instead of 0? Can
you find the bug that would cause a potential run-time error in the example above? Can you rewrite the code to fix this logical error?