Do Loop structure

Do-Loop structure, unlike For-Next loops, does not have built-in counter, for this purpose we have to declare one integer variable. Its value is increased in the loop body before or after the given statements, and checking the conditions on which it comes out of the loop is performed, in the case of the Do-While loop, at the beginning and in the case of the Do-Until loop at the end. Let’s see how to use these loops!

The Do-Loop structure syntax is:

Do { While | Until } condition
[STATEMENTS]
[Continue Do]
[STATEMENTS]
[Exit Do]
[STATEMENTS]
Loop 

or

Do
[STATEMENTS]
[Continue Do]
[STATEMENTS]
[Exit Do]
[STATEMENTS]
Loop  { While | Until } condition

If we want to repeat the “I like Excel” five times, we need to write the code:

Dim i As Integer
i = 1
Do While (i < 6)
    MsgBox “Ja like Excel”
    i = i + 1
Loop

As you can see in this loop, the condition is checked immediately at the beginning, and if satisfied, given statements are being executed; if not, the loop is left. If there is a need to check the condition at the end it is necessary to use the Do-Until loop that has the same syntax, but otherwise checks the fulfillment of the conditions. If we modify the previous code, we get:

Dim i As Integer
i = 1
Do Until (i > 5)
    MsgBox “Ja like Excel”
    i = i + 1
Loop