The Do While Loops

Visual Basic supports several versions of the Do statement. The Do While loop is perhaps the most common looping statement that you'll put in Visual Basic programs. Do While works with comparison expressions just as the If statement does. Therefore, the six comparison operators that you learned about in the previous lesson work as expected here. Rather than control the one-time execution of a single block of code, however, the comparison expression controls the looping statements.

Like the If statement (covered in Hour 7, "Making Decisions") that ends with an End If statement, a loop will always be a multiline statement that includes an obvious beginning and ending of the loop. Here is the format of the Do While loop:

Do While (comparison test)

Block of one or more Visual Basic statements

Loop

The block of code continues looping as long as comparison test is true. Whether you insert one or several lines of code for the block doesn't matter. It's vital, however, that the block of code somehow change a variable used in comparison test. The block of code keeps repeating as long as the Do While loop's comparison test continues to stay true. Eventually, comparison test must become false or your program will enter an infinite loop and the user will have to break the program's execution through an inelegant means, such as pressing the Ctrl+Break key combination.

note

An infinite loop is a loop that never terminates.

caution

Guard against infinite loops and always make sure your loops can terminate properly. Even if you provide an Exit command button or a File[hr]|[hr]Exit menu option in your application, the program will often ignore the user's exit command if the program enters an infinite loop.

The Do While loop continues executing a block of Visual Basic statements as long as comparison test is true. As soon as comparison test becomes false, the loop terminates.

sidebar

The Loop's Termination

As long as comparison test is true, the block of code in the body of the loop continues executing. When comparison test becomes false, the loop terminates. After the loop terminates, Visual Basic begins program execution at the statement following the Loop statement because Loop signals the end of the loop.

As soon as Do While's comparison test becomes false, the loop terminates and doesn't execute even one more time. The Do While's comparison test appears at the top of the loop. Therefore, if comparison test is false the first time the loop begins, the body of the loop will never execute.

Listing 8.1 contains a section of an event procedure that contains a Do While loop that asks the user for an age. If the user enters an age less than 10 or more than 99, the program beeps at the error and displays another input box asking for the age. The program continues looping, asking for the age, as long as the user enters an age that's out of range.

Listing 8.1. The Do While loop executes as long as comparison test is true.

1: Dim strAge As String

2: Dim intAge As Integer

3: Dim intPress As Integer

4:

5: ' Get the age in a string variable

6: strAge = InputBox("How old are you?", "Age Ask")

7: ' Check for the Cancel command button

8: If (strAge = "") Then

9: End ' Terminates the application

10: End If

11:

12: ' Cancel was not pressed, so convert Age to integer

13: ' The Val() function converts strings to integers

14: intAge = Val(strAge)

15:

16: ' Loop if the age is not in the correct range

17: Do While ((intAge < 10) Or (intAge > 99))

18: ' The user's age is out of range

19: intPress = MsgBox("Your age must be between " & _

20: "10 and 99", vbExclamation, "Error!")

21: strAge = InputBox("How old are you?", "Age Ask")

22:

23: ' Check for the Cancel command button

24: If (strAge = "") Then

25: End ' Terminate the program

26: End If

27: intAge = Val(strAge)

28: Loop

Listing 8.1 does nothing with MsgBox()'s return value stored in intPress. The user simply presses Enter to close the message box, so checking for intPress's value would not help this particular section of code.

note

Listing 8.1 uses the built-in Val() function. Val() accepts a string argument and converts that string to a number (assuming that the string holds the correct digits for a number). The InputBox() function returns a string so the value the user enters into the input box must convert to an integer before you store the value in the integer variable named intAge.

The code contains some redundancy. For example, two lines contain almost the same InputBox() function, and the same check for a Cancel button press appears twice in the program. There are other looping statements that you'll learn about later in this lesson; those statements can help simplify this code by removing some of the redundancy.

Perhaps the most important thing to note about the Do While loop in Listing 8.1 is that the body of the loop provides a way for comparison test to terminate. The code contains an intAge variable that the body of the loop reassigns each time the loop's block of code executes. Therefore, assuming that the user enters a different value for the age, the loop will test against a different set of comparison values, the comparison test will fail (which would mean that the age is inside the range), and the program will stop looping. If the loop body did nothing with the comparison test variable, the loop would continue forever.

Top Home