| Start | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| For...Next
Any integer expression can be used to parameterize the loop. It is permitted, but not required, for the control variable (in this case Being able to define the Start and End integers allows loops to be created that directly reference other objects, such as: This would then loop through every row in It is also possible to define how the control variable must increment. This outputs: 1 3 5 7 9 It is also possible to decrement the control variable (count down). This outputs: 10 9 8 7 6 5 4 3 2 1 You should not attempt to use (read or update) the control variable outside the loop. |
||||||||||||||||||||
| For Each...Next loop for looping through collection of items You can use a An example of looping through a DataTable's Rows property would look like this: An important thing to note is that the collection must not be modified while in a Collection was modified; enumeration operation may not execute. |
||||||||||||||||||||
| Short Circuiting Any loop may be terminated or continued early at any point by using the Exiting You can stop any loop by exiting early. To do this, you can use the keyword
Exiting a loop early is a great way to boost performance by only looping the necessary number of times to satisfy the application's needs. Below is example where the loop will exit once it finds the number Continuing Along with exiting early, you can also decide that you need to just move on to the next loop iteration. This is easily done by using the
Here's an example of preventing even numbers from being added to the sum. Usage Advice There are two alternative techniques that can be used instead of using You can declare a new Boolean variable, initializing it to one value and conditionally setting it to the other value inside the loop; you then use a conditional statement (e.g. One of the objections to this technique is that it may be inefficient. For example, if in the above example You can use the This technique can sometimes be the neatest way to jump out of the loop and avoid one or more statements that are executed just after the natural end of the loop. You should consider all of the alternatives, and use whichever one best fits your requirements, considering such things as efficiency, speed of writing the code, and readability (thus maintainability). Do not be put off using |
||||||||||||||||||||
| While loop to iterate while some condition is true A This outputs: 1 2 3 4 5 6 7 8 9 10 Warning: A |
||||||||||||||||||||
| Do...Loop Use or 0 1 2 3 4 5 6 7 8 9 or 0 1 2 3 4 5 6 7 8 9
1 3 5 7 9 You can terminate the loop with 0 1 2 3 4 5 6 7 8 9 |
||||||||||||||||||||
Nested Loop The Structure of a For Next nested loop is : Example : |
Read Article
https://develop.horje.com/learn/1434/reference