Looping is required when we need to process something repetitively until a certain condition is met. For example, we can design a programme that adds a series of numbers until the sum exceeds a certain value. We can also write a programme that promts the user to enter data repeatedly until he or she enters the word ‘Finish’. In Visual Basic 2012, there are three types of Loops, they are the For…..Next loop, the Do loop. and the While…..End while loop
For counter=1 to 10
* The program will enter number 1 to 10 into the list box.
For counter=1 to 100 step 10
* The program will calculate the sum of the numbers as follows:
sum = 1000
For counter = 100 To 5 Step -5
sum – = counter
*Notice that increment can be negative.
The program will compute the
subtraction as follow:
For n=1 to 10
If n>6 then
ListBox1.Items.Add ( n)
The process will stop when n is greater than 6.
* The above example will keep on adding until counter >1000.
The above example can be rewritten as
Loop until counter>1000
Dim sum, n As Integer
ListBox1.Items.Add(“n” & vbTab & “Sum”)
n += 1
sum += n
ListBox1.Items.Add(n & vbTab & sum)
If n = 100 Then