Do loops work well when you do not know in advance how many times you need to execute the statements in the loop. However, when you expect to execute the loop a specific number of times, a For...Next loop is a better choice. Unlike a Do loop, a For loop uses a variable called a counter that increases or decreases in value during each repetition of the loop. The syntax is as follows:
For counter [ As datatype ] = start To end [ Step step ] ' Statement block to be executed for each value of counter. Next [ counter ]
The variable counter must be of a numeric data type that supports the greater than or equal (>=), less than or equal (<=), and addition (+) operators; usually it is Integer. The iteration values start, end, and step are expressions that must evaluate to data types that widen to the type of counter. Usually they are also Integer. The optional step can be positive or negative. If it is omitted, it is taken to be 1.
If counter is not declared outside the loop, you can use the As clause to declare it as part of the For statement.
When execution of the For...Next loop begins, Visual Basic evaluates start, end, and step. It then assigns start to counter. Before it executes the statement block, it compares counter to end. If counter is already past the end value, the For loop terminates and control passes to the statement following the Next statement. Otherwise the statement block is executed.
Each time Visual Basic encounters the Next statement, it increments the counter by step and returns to the For statement. Again it compares the counter to end, and again it either executes the block or terminates the loop depending on the result. This process continues until the counter passes end or an Exit For statement is executed.
The loop does not terminate until counter has passed the end value. If counter is equal to end, the loop continues. The comparison that determines whether to execute the block is counter <= end if step is positive and counter >= end if step is negative.
You can optionally specify counter in the Next statement. This improves the readability of your program. You must specify the same variable as the one that appears in the For statement.
The following procedure presets all the elements of an array to 128. The For statement specifies the counter variable
I and its start and end values. The Next statement increments the counter by 1, because step is not supplied.
Sub Preset(ByRef A() As Integer) Dim I As Integer For I = 0 To UBound(A) A(I) = 128 Next I End Sub
The iteration values start, end, and step are evaluated only once, before the loop begins. If your statement block changes end or step, these changes do not affect the iteration of the loop. In the preceding example, the UBound function is called only when the For statement is first executed. If the statement block had subsequently changed the length of the array
A, the For loop would still use the original length as the end value, because it does not call UBound again.
Incrementing and Decrementing the Counter Variable
Using the Step keyword, you can increment or decrement the counter by the value you specify. In the following example, the counter variable
J is incremented by 2 each time the loop repeats. When the loop is finished,
Total is the sum of 2, 4, 6, 8, and 10.
Sub TwosTotal() Dim J, Total As Integer For J = 2 To 10 Step 2 Total = Total + J Next J MsgBox("The total is " & Total) End Sub
To decrease the counter variable, use a negative Step value. When you do this, you should specify an end value that is less than the start value. In the following example, the counter variable
N is decreased by 2 each time the loop repeats. When the loop is finished,
Total is the sum of 16, 14, 12, 10, 8, 6, and 4.
Sub NewTotal() Dim N, Total As Integer For N = 16 To 4 Step -2 Total = Total + N Next N MsgBox("The total is " & Total) End Sub
Exiting a For...Next Loop Before the Counter Passes Its End Value
You can exit a For...Next loop before the counter passes its end value by using the Exit For statement. For example, you might want to exit a loop if you detect a condition that makes it unnecessary or impossible to continue iterating, such as an erroneous value or a termination request. Also, if you catch an exception in a Try...Catch...Finally, you can use Exit For at the end of the Finally block.
Decision Structures | Nested Control Statements | For...Next Statements (Language Reference) | Do...Loop Statements | While Statement | For Each...Next Statements | Exit Statement | Data Types | Integer Data Type | UBound Function | Step