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.

Advertisements

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.

Advertisements

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.

Figure 1 – Output – Sum While Loop
Advertisements

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Exit mobile version