The Other Do Loops

Another pair of Do loops work almost exactly like the two previous loops. Do...Loop While and Do...Loop Until look very much like their counterparts that you learned earlier. But these new loop formats check their comparison tests at the bottom of the loop rather than at the top.

If a loop begins with a single Do statement, the loop ends with either Loop While or Loop Until. Here is the format of Do...Loop While:

Do

Block of one or more Visual Basic statements

Loop Until (comparison test)

That Do looks lonely by itself, doesn't it? The purpose of the Do is to signal the beginning of the loop. The loop continues until the Loop Until statement. The comparison test appears at the bottom of the loop if you use the Do...Loop While loop statement. The body of the loop always executes at least once. The body of the loop executes more than once as long as the comparison test stays true. There is a corresponding Do...Loop Until statement that checks for a false condition at the bottom of the loop's body.

Notice that the Do...Loop While loop's comparison test appears at the bottom of the loop instead of at the top of the loop. You'll use the Do...Loop While loop when you want the body of the loop to execute at least one time. Often, by placing comparison test at the bottom of the loop, you can eliminate redundant code that otherwise might be required if you used Do While.

To complete the loop statements, Visual Basic also supports a Do...Loop Until statement. Like the Do...Loop While, the Do...Loop Until statement tests comparison test at the bottom of the loop. Therefore, the body of the loop executes at least once no matter what comparison test turns out to be. The loop continues as long as the comparison test result stays false.

Listing 8.3 contains the age-checking procedure that's much shorter than the previous versions. comparison test appears at the bottom of the loop, so the extra InputBox() function call isn't needed.

Listing 8.3. Using the Do...Loop While to check the comparison at the bottom of the loop.

1: Dim strAge As String

2: Dim intAge As Integer

3: Dim intPress As Integer

4:

5: Do

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

7: ' Check for the Cancel command button

8: If (strAge = "") Then

9: End ' Terminate program

10: End If

11: intAge = Val(strAge)

12:

13: If ((intAge < 10) Or (intAge > 99)) Then

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

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

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

17: End If

18: Loop While ((intAge < 10) Or (intAge > 99))

The loop begins almost immediately. The loop's body will always execute at least once, so InputBox() appears right inside the loop. By placing the InputBox() function inside the loop, you eliminate the need to put this function in the code twice (once before the loop and once inside the loop, as was necessary using the previous looping statements in Listings 8.1 and 8.2).

note

In this simple application of the looping statements that you've seen here, Do...Loop While required less code than the Do While and Do Until loops. By changing the Do...Loop While's comparison test, a Do Until would also work. These last two loops will not, in every case, produce less code as they do here. The logic of the application determines which loop works best.

Top Home