May 12, 2021 ASP.NET
Statements are repeated in a loop.
Loop statements allow us to execute a statement or group of statements multiple times.
If you need to repeat the same statement, you can set a loop.
If you know how many times you want to loop, you can use for loops. This type of loop is especially useful when counting up or down:
If you're using a collection or array, you'll often use the for each loop.
A collection is a similar set of objects, and the for each loop can traverse the collection until it is complete.
In the following example, traversing ASP.NET request.ServerVariables collection.
The while loop is a generic loop.
The while loop starts with the while keyword, followed by parentheses, in which you can specify how long the loop will last, followed by a block of code that repeats.
While loops typically set an incremental or decreasing variable to count.
In the following example, the operator adds 1 to the value of variable i for each loop.
When you want to store multiple similar variables but don't want to create a separate variable for each variable, you can use arrays to store: