Hi ...In this article i am going to describe about the loop, what we are oftenly used in VB.NET
In vb.net intially there are two basic kinds of loop
■ While loops, also called Do loops
■ For loops, including For Next and For Each
A While loop is the simplest form of loop it makes a block of code repeat for as long as a particular condition is true. Here’s an example:
Sub Page_Load(ByVal s As Object, ByVal e As EventArgs)
Dim counter As Integer = 0
Do While t1 <= 10
messageLabel.Text = counter.ToString()
t1 += 1
Loop
End Sub
In this examble when the page load its dislplay the number 10,,which will increment to 1, then 2, all the way to 10.
The other form of the While loop, called a Do While loop
Exanble for Do while loop,In the do while loop the condition has met at the end of the code....
Sub Page_Load(s As Object, e As EventArgs)
Dim counter As Integer = 0
Do
messageLabel.Text = counter.ToString()
counter += 1
Loop While counter <= 10
End Sub
FOR LOOP:
A For loop is similar to a While loop, but we typically use it when we know in advance
how many times we need it to execute.....
Dim i As Integer
For i = 1 To dropdownList.Items.Count
messageLabel.Text = i.ToString()
Next
It will diplay the items present in the dropdownlist box...
And other type of forloop is FOR EACH
There is a main difference betwen FOR and FOR EACH loop
in for loop its start with predefined intial value and end with maximum value so its represent mostly integer but For each loop variable represents the object from the collection ,it may be integer,string or date.
For an examble we can looping in array it contains the string value, loop contine upto the last item in the arrya....
Examble:
For Each item In arrayName
messageLabel.Text = item
Next
Let me discuss one another examble to clearly understand the concept of FOR EACH
For Each num As String In ComboBox1.Items
ListBox1.Items.Add(num.ToString)
Next
Let us see in this examble. we are loading the combobox item into listbox using FOR EACH loop,This loop will continue upto the item present in combobox...
i hope this article would help you to get clear idea about loops...