For...Next Statement (Visual Basic)
For...Next Statement (Visual Basic)
Repeats a group of statements a specified number of times.
For counter [ As datatype ] = start To end [ Step step ] [ statements ] [ Continue For ] [ statements ] [ Exit For ] [ statements ] Next [ counter ]
||Required in the
||Optional. Data type of
||Required. Numeric expression. The initial value of
||Required. Numeric expression. The final value of
||Optional. Numeric expression. The amount by which
||Optional. One or more statements between
||Optional. Transfers control to the next loop iteration.|
||Optional. Transfers control out of the
||Required. Terminates the definition of the
Tokeyword is used in this statement to specify the range for the counter. You can also use this keyword in the Select...Case Statement and in array declarations. For more information about array declarations, see Dim Statement.
You use a
Next structure when you want to repeat a set of statements a set number of times.
In the following example, the
index variable starts with a value of 1 and is incremented with each iteration of the loop, ending after the value of
index reaches 5.
In the following example, the
number variable starts at 2 and is reduced by 0.25 on each iteration of the loop, ending after the value of
number reaches 0. The
Step argument of
-.25 reduces the value by 0.25 on each iteration of the loop.
[!TIP] A While...End While Statement or Do...Loop Statement works well when you don't know in advance how many times to run the statements in the loop. However, when you expect to run the loop a specific number of times, a
Nextloop is a better choice. You determine the number of iterations when you first enter the loop.
You can nest
For loops by putting one loop within another. The following example demonstrates nested
Next structures that have different step values. The outer loop creates a string for every iteration of the loop. The inner loop decrements a loop counter variable for every iteration of the loop.
When nesting loops, each loop must have a unique
You can also nest different kinds control structures within each other. For more information, see Nested Control Structures.
Exit For and Continue For
Exit For statement immediately exits the
Next loop and transfers control to the statement that follows the
Continue For statement transfers control immediately to the next iteration of the loop. For more information, see Continue Statement.
The following example illustrates the use of the
Continue For and
Exit For statements.
You can put any number of
Exit For statements in a
Next loop. When used within nested
Exit For exits the innermost loop and transfers control to the next higher level of nesting.
Exit For is often used after you evaluate some condition (for example, in an
Else structure). You might want to use
Exit For for the following conditions:
Continuing to iterate is unnecessary or impossible. An erroneous value or a termination request might create this condition.
Finallystatement catches an exception. You might use
Exit Forat the end of the
You have an endless loop, which is a loop that could run a large or even infinite number of times. If you detect such a condition, you can use
Exit Forto escape the loop. For more information, see Do...Loop Statement.
Next loop starts, Visual Basic evaluates
step. Visual Basic evaluates these values only at this time and then assigns
counter. Before the statement block runs, Visual Basic compares
counter is already larger than the
end value (or smaller if
step is negative), the
For loop ends and control passes to the statement that follows the
Next statement. Otherwise, the statement block runs.
Each time Visual Basic encounters the
Next statement, it increments
step and returns to the
For statement. Again it compares
end, and again it either runs the block or exits the loop, depending on the result. This process continues until
end or an
Exit For statement is encountered.
The loop doesn't stop until
counter has passed
counter is equal to
end, the loop continues. The comparison that determines whether to run the block is
step is positive and
step is negative.
If you change the value of
counter while inside a loop, your code might be more difficult to read and debug. Changing the value of
step doesn't affect the iteration values that were determined when the loop was first entered.
If you nest loops, the compiler signals an error if it encounters the
Next statement of an outer nesting level before the
Next statement of an inner level. However, the compiler can detect this overlapping error only if you specify
counter in every
The value of
step can be either positive or negative. This parameter determines loop processing according to the following table:
|Step value||Loop executes if|
|Positive or zero||
The default value of
step is 1.
The following table indicates whether
counter defines a new local variable that’s scoped to the entire
For…Next loop. This determination depends on whether
datatype is present and whether
counter is already defined.
|No||No||Yes. The data type is inferred from the
|Yes||Yes||Yes, but only if the existing
The data type of
counter determines the type of the iteration, which must be one of the following types:
An enumeration that you declare by using an Enum Statement.
Tthat has the following operators, where
Bis a type that can be used in a
Public Shared Operator >= (op1 As T, op2 As T) As B
Public Shared Operator <= (op1 As T, op2 As T) As B
Public Shared Operator - (op1 As T, op2 As T) As T
Public Shared Operator + (op1 As T, op2 As T) As T
You can optionally specify the
counter variable in the
Next statement. This syntax improves the readability of your program, especially if you have nested
For loops. You must specify the variable that appears in the corresponding
step expressions can evaluate to any data type that widens to the type of
counter. If you use a user-defined type for
counter, you might have to define the
CType conversion operator to convert the types of
step to the type of
The following example removes all elements from a generic list. Instead of a For Each...Next Statement, the example shows a
Next statement that iterates in descending order. The example uses this technique because the
removeAt method causes elements after the removed element to have a lower index value.
The following example iterates through an enumeration that's declared by using an Enum Statement.
In the following example, the statement parameters use a class that has operator overloads for the