VB 6 Do Loop

Loops are very important when you want to repeat some code block. In previous example, we saw how while loop works. In this article, you will learn about Do loop.

There are two types of Do loop in VB 6 that you are going to see. Syntax for each type is given below.

Do [While | Until <condition>]

   [Statements]
   [Exit Do]
   [Statements]

Loop

In the example above the Do loop will check for condition before executing the code block. The second type is quite opposite of the above.

Do 

   [Statements]
   [Exit Do]
   [Statements]

Loop [While | Until <condition>]

The loop is tested only after the code block is executed. This guarantees that the loop will executed at least once. That is the major difference between two types.

Example Program: Do Loop

In the given example, we will create a variable called Sum and add a number to it repeatedly until the loop is terminated.

Private Sub Command1_Click()

Dim sum As Integer, i As Integer

sum = 0
i = 0

Do Until i = 100

sum = sum + i

i = i + 1

Loop

MsgBox ("The Sum is" & " " & sum)

End Sub

Output – Do Loop

Output - Do Loop
Output – Do Loop
Skip to content