Do - Until

You can test the condition either at the top or the bottom of the loop.
This is executed at least once because the condition is at the bottom.


Condition At The Top

This might not be executed because the condition is at the top.
This loops until the variable is equal to the value 5 (four times).

Public Sub DoUntil1() 
Dim icount As Integer
   icount = 1
   Do Until (icount = 5)
'do something
      icount = icount + 1
   Loop
End Sub

Condition At The Bottom

This is executed at least once because the condition is at the bottom.
This loops until the variable is equal to the value 5 (four times).

Public Sub DoUntil2() 
Dim icount As Integer
   icount = 1
   Do
'do something
      icount = icount + 1
   Loop Until (icount = 5)
End Sub

Nested Do - Until

When used within nested loops Exit Do transfers control to the loop that is one nested level above the loop where the Exit Do occurs.

Public Sub DoUntil3() 
Dim iouter As Integer
Dim inner As Integer
   iouter = 1
   Do Until (iouter = 5)
      inner = 1
      Do Until (inner = 5)
'do something
         inner = inner + 1
      Loop
      iouter = iouter + 1
   Loop
End Sub

Exit Do

The Exit Do statement lets you exit the loop early.


© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext