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 |
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.
For index As Integer = 1 To 5 Debug.Write(index.ToString & " ") Next Debug.WriteLine("") ' Output: 1 2 3 4 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.
For number As Double = 2 To 0 Step -0.25 Debug.Write(number.ToString & " ") Next Debug.WriteLine("") ' Output: 2 1.75 1.5 1.25 1 0.75 0.5 0.25 0
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.
For indexA = 1 To 3 ' Create a new StringBuilder, which is used ' to efficiently build strings. Dim sb As New System.Text.StringBuilder() ' Append to the StringBuilder every third number ' from 20 to 1 descending. For indexB = 20 To 1 Step -3 sb.Append(indexB.ToString) sb.Append(" ") Next indexB ' Display the line. Debug.WriteLine(sb.ToString) Next indexA ' Output: ' 20 17 14 11 8 5 2 ' 20 17 14 11 8 5 2 ' 20 17 14 11 8 5 2
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.
For index As Integer = 1 To 100000 ' If index is between 5 and 7, continue ' with the next iteration. If index >= 5 And index <= 8 Then Continue For End If ' Display the index. Debug.Write(index.ToString & " ") ' If index is 10, exit the loop. If index = 10 Then Exit For End If Next Debug.WriteLine("") ' Output: 1 2 3 4 9 10
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
Forstatement. 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…Nextloop. This determination depends on whether
datatype is present and whether
counter is already defined.
|Is ||Is ||Result (whether |
|No||Yes||No, because |
|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