Unlike the other two loops, the For. . .Next loop requires that you know the number of times that the statements in the loop will be executed. The For. . .Next loop has the following syntax:
For counter = start To end [Step increment]
statements
Next [counter]
Code language: VB.NET (vbnet)
The keywords in the square brackets are optional. The arguments counter, start, end, and increment are all numeric. The loop is executed as many times as required for the counter to reach (or exceed) the end value.
In executing a For. . .Next loop, Visual Basic does the following:
- Sets counter equal to start.
- Tests to see whether counter is greater than end. If so, it exits the loop without executing the statements in the loop’s body, not even once. If increment is negative, Visual Basic tests to see whether counter is less than end. If it is, it exits the loop.
- Executes the statements in the block.
- Increases counter by the amount specified with the increment argument, following the Step keyword. If the increment argument isn’t specified, counter is increased by 1. If Step is a negative value, counter is decreased accordingly.
- Continues with step 3.
The For. . .Next loop in Listing 3.4 scans all the elements of the numeric array data and calculates their average.
Listing 3.4: Iterating an Array with a For. . .Next Loop
Dim i As Integer, total As Double
For i = 0 To data.GetUpperBound(0)
total = total + data(i)
Next i
Debug.WriteLine (total / Data.Length)
Code language: VB.NET (vbnet)
The single most important thing to keep in mind when working with For. . .Next loops is that the loop’s ending value is set at the beginning of the loop. Changing the value of the end variable in the loop’s body won’t have any effect. For example, the following loop will be executed 10 times, not 100 times:
Dim endValue As Integer = 10
Dim i as Integer
For i = 0 To endValue
endValue = 100
{ more statements }
Next i
Code language: VB.NET (vbnet)
You can, however, adjust the value of the counter from within the loop. The following is an example of an endless (or infinite) loop:
For i = 0 To 10
Debug.WriteLine(i)
i = i - 1
Next i
Code language: VB.NET (vbnet)
This loop never ends because the loop’s counter, in effect, is never increased. (If you try this, press Ctrl + Break to interrupt the endless loop.)
The increment argument can be either positive or negative. If start is greater than end, the value of increment must be negative. If not, the loop’s body won’t be executed, not even once. VB 2008 allows you to declare the counter in the For statement. The counter variable ceases to exist when the program bails out of the loop:
For i As Integer = 1 to 10
Debug.WriteLine(i.ToString)
Next
Debug.WriteLine(i.ToString)
Code language: VB.NET (vbnet)
The i variable is used as the loop’s counter and it’s not visible outside the loop. The last statement won’t even compile; the editor will underline it with a wiggly line and will generate the error message Name ‘i’ is not declared.
Manipulating the Loop’s Counter
Manipulating the counter of a For. . .Next loop is strongly discouraged. This practice will most likely lead to bugs such as infinite loops, overflows, and so on. If the number of repetitions of a loop isn’t known in advance, use a Do. . .Loop or a While. . .End While structure. To jump out of a For. . .Next loop prematurely, use the Next For statement.