Changes

Visual Basic For Loops

4 bytes removed, 19:11, 9 April 2009
no edit summary
Computers are incredibly good at repeating the same task over and over very quickly. One of the key functions of a programming language such as Visual Basic is to make it easy for a developer to program a computer to perform these repetitive tasks. Visual Basic provides a number of language structures which which that tell a program to perform a task repeatedly, either a specific number of times, or until certain conditions are met. In this chapter of Visual Basic essentials we will look at each of these in detail.
== Creating a Visual Basic For Loop ==
'''Next''' ''counterVariable''<br>
The easiest way to gain an understanding of For loops is to see an example. The following code excerpt declares a variable to act as the loop counter and the then performs a loop 5 times, displaying the value of the counter on each loop iteration:
<pre>
== Early Exit of a For Loop ==
it It is often necessary to exit a For loop before it has completed the specified number of loop iterations. This is achieved using the Visual Basic ''Exit For'' statement. This is typically used in conjunction with a conditional ''If'' statement. The Following code example causes the loop to exit if the counter is equal to 3:
<pre>
== Continuing a For Loop ==
The ''Exit For'' command causes the For loop to exit entirely. Another option is to skip the remaining statements in a loop under certain circumstances, let but to continue to loop. This is achieved using the ''Continue For'' statement. The following example skips the code in the body of the For statement if counter is 3, and continues the loop at the next iteration:
<pre>