If you don't know how many repetitions you want, use a Do...Loop statement.

The Do...Loop statement repeats a block of code while a condition is true, or until a condition becomes true.

Repeat Code While a Condition is True

You use the While keyword to check a condition in a Do...Loop statement.

Do While i>10
  some code
Loop
 

If i equals 9, the code inside the loop above will never be executed.

Do
  some code
Loop While i>10
 

The code inside this loop will be executed at least one time, even if i is less than 10.

Repeat Code Until a Condition Becomes True

You use the Until keyword to check a condition in a Do...Loop statement.

Do Until i=10
  some code
Loop
 

If i equals 10, the code inside the loop will never be executed.

Do
  some code
Loop Until i=10
 

The code inside this loop will be executed at least one time, even if i is equal to 10.

Exit a Do...Loop

You can exit a Do...Loop statement with the Exit Do keyword.

Do Until i=10
  i=i-1
  If i<10 Then Exit Do
Loop
 

The code inside this loop will be executed as long as i is different from 10, and as long as i is greater than 10.



Practice Excercise Practice now