Visual Basic has the ability to allow you repeat a sequence of codes as many times as long as the processor and memory could support. This is generally called looping . Looping is required when we need to process something repeatedly until a certain condition is reached.
For example, we can design a program that adds a series of numbers to a ComboBox until a specific number is reached.
In this article am going to explain Visual Basic For Next Loop, Do While Loop and Do Until Loop, While Wend Loop,For Each Next Loop and Examples
Depending upon the position of a control statement in a program, a loop is classified into two types:
- Entry controlled loop
- Exit controlled loop
In an entry controlled loop, a condition is checked before executing the body of a loop. It is also called as a pre-checking loop.
In an exit controlled loop, a condition is checked after executing the body of a loop. It is also called as a post-checking loop.
The For-Next loop can be used to execute a block of code for a specific number of times. The “VBScript For loop" specifies the counter variable and its start and end values. The Next statement increases the counter variable by one.
For counter = startNumber To endNumber (Step increment)
'code to execute
Next
To exit a For.....Next Loop, you can place the Exit For statement within the loop; and it is normally used together with the If...Then...statement.
A program to add numbers between 1 to 20 to Dropdownlist control
Dim i As Integer
For i = 1 To 20
DropDownList1.Items.Add (i)
Next
By default, the loop counter variable is incremented by one. If you want to increase or decrease the counter variable by the value you specified, then you can use For....Step....Next loop. Suppose in the above code, you want to increment the counter by 3, then your code would be like this:
For i = startNum To endNum Step number
'code goes here
Next
Dim i As Integer
For i = 1 To 20 Step 3
DropDownList1.Items.Add (i)
Next
In the above you have incremented the counter. To decrement, try
Dim i As Integer
For i = 1 To 20 Step -3
DropDownList1.Items.Add (i)
Next
Furthermore, you can also terminate your your For-Next statement at anytime you dim fit. Take for instance the example we used above, if there should be any need for you to stop the loop once the counter reaches 10, then you add "If statement" to exit the loop.
Example is
Dim i As Integer
For i = 1 To 20
DropDownList1.Items.Add (i)
If i = 10 Then
Exit For
End If
Next
If you want to repeat a block of code for each item in a collection or for each element of an array, then you need to use For-Each-Next loop.
For Each item In Items
'code to execute
Next
Dim cars(3)
cars(0)="Toyota"
cars(1)="Camri"
cars(2)="Range Rover"
For Each car In cars
MsgBox (car)
Next
While...Wend loop is similar to Do While loop though not used commonly. As Do While is more structured than While.....Wend loop, is usually mostly used by programmers.
While VariableName
'code to execute
Wend
Dim i As Integer
i = 1
While i
i < 3
MsgBox ("Welcome")
i = i +1
Wend
At times, you might not know the number of times you need to execute a particular block of code, then you will be using Do While loops.
Take for example, you want to display a "Welcome, how are you?" message to any one that launched your application while the counter variable "i" is less than 10. In such case, Do While loop will be perfect choice to use.
Do While condition
'code to execute
Loop
Dim i As Integer
i = 2
Do While i < 10
MsgBox("Welcome! How are you?")
i = i + 2
Loop
Do Until loop is also used when you do not know the number of times your block of code would be executed.
Do Until condition
'code to execute
Loop
The example given in "Do While" statement can also be written as;
Dim i As Integer
i = 2
Do Until x=10
MsgBox ("Welcome! How are you?")
i = i + 2
Loop
Or
Dim i As Integer
i = 2
Do
MsgBox ("Welcome! How are you?")
i = i + 2
Loop Until i = 10