Home » VB 6.0 Tutorial » VB 6 While Loop

VB 6 While Loop

Like many programming languages out there, VB 6 also has loop control statements. In this article, you will learn about While loop which helps in repeating a block of code until loop is terminated.

Syntax: While Loop

The general structure of while loop is given below.

While condition
   [expression or statements]
Wend

The condition is very important in while loop because that is what decides when the loop will be terminated.

Consider the example below.

Dim index As Integer
index = -1;
While index < 10 
   MsgBox (index + 2)
Wend

In the example above, we have put a condition for while loop that the loop should continue until index variable is equal to or greater than 10.

Example Program:

Dim Sum As Integer, i As Integer
i = 0;

While i < 10 
   Sum = Sum + i
   i = i + i
Wend
MsgBox ("The Value of Sum is " + Sum)

Output – While Loop

The program loop through and add the value of i to Sum which is incremented by 1 at each iteration.

Output – Sum While Loop