ASP.NET Loops

Loop is used to doing something again and again. for example, if you want to type your name 100 times. you dont have to use response.Write 100 times instead you can type once and put it in a loop. There are many types of loop for example, for loop, while loop, for each loop. All these loop perform same action. But for loop is used when we know how many times the loop is going to run. While on the other hand can be used, when we are not sure about the number of execution.

For Loop: This loop requires a start number and an end one. It increments the value of its counter automatically.
'Set the loop to execute 3 times (1, 2, 3)
'Counter variable will have its value incremented by one each time
Dim Counter as Integer
For Counter = 1 To 3
          Response.Write("The value of loop Counter is " & Counter & "<br>"
Next

While Loop: This loop will execute as long as the condition give is true. It does not increment the value of its counter automatically. WE MUST DO.
 ‘Set counter as 1
Counter = 1
'Set the loop to execute 3 times (1, 2, 3)
While Counter <= 3
          Response.Write("The value of loop Counter is " & Counter & "<br>")
'Increment the value of variable by one
Counter = Counter + 1
End While
<%
            Dim i as integer = 1
            While (i<=100)
                        response.Write(i)
                        response.Write(". Shahid")
                        response.Write("<BR>")
                        i= i + 1
            end while
           
%>

 

For Each Loop: This loop moves through an array collection.
 ‘Create variable to store the value of each array elements
Dim Cat As String
‘Create array
Dim MyCats(1) As String
MyCats(0) = “Tom”
MyCats(1) = “Jerry”
'Loop through the array collection
For Each Cat In MyCats
          Response.Write(Cat & "<br>")
Next
A practical Example:
Dim st as string
For each st in student
response.Write(st & "<BR>")
Next