Export (0) Print
Expand All

10.9.2 For...Next Statements

Visual Studio .NET 2003

A For...Next statement loops based on a set of bounds. A For statement specifies a loop control variable, a lower bound expression, an upper bound expression, and an optional step value expression.

The loop control variable is specified either through an identifier followed by an As clause or an expression. In the case of an identifier, the identifier defines a new local variable of the type specified in the As clause, scoped to the entire For loop. In the case of an expression, the expression must be classified as a variable.

The loop control variable of a For statement must be of a primitive numeric type (Byte, Short, Integer, Long, Decimal, Single, Double) or Object, and the bound and step expressions must be implicitly convertible to the type of the loop control. Enumerated types are treated as their underlying type, which allows use of enumerated types in For loops. The bounds and step expressions must be classified as values.

A loop control variable cannot be used by another enclosing For...Next statement. A For statement must be closed by a matching Next statement. A Next statement without a variable matches the innermost open For statement. A Next statement with one or more loop control variables will, from left to right, match the For loops that match each variable. If a variable matches a For loop that is not the most nested loop at that point, a compile-time error results.

At the beginning of the loop, the three expressions are evaluated in textual order and the lower bound expression is assigned to the control value. If the step value is omitted, it is implicitly the literal 1. If the For loop variable is of type Object, at least one of the expressions at run time must be of a primitive numeric type, and all three expressions must be convertible to the widest numeric type among them. There is one exception to this rule. If the three expressions are typed as String, then all three expressions are converted to Double.

At the beginning of each loop, the control variable is compared to see if it is greater than the end point if the step expression is positive, or less than the end point if the step expression is negative. If it is, the For loop terminates; otherwise the loop block executes. At the Next statement, the step value is added to the control variable and execution returns to the top of the loop.

It is not valid to branch into a For loop from outside the loop.

ForStatement ::=
   For LoopControlVariable = Expression To Expression [ Step Expression ]
      StatementTerminator 
   [ Block ]
   Next [ NextExpressionList ] StatementTerminator
LoopControlVariable ::=
   Identifier As TypeName |
   Expression
NextExpressionList ::=
   Expression |
   NextExpressionList , Expression

See Also

10.9.1 While...End While and Do...Loop Statements | 10.9.3 For Each...Next Statements | 10.9 Loop Statements | For...Next Statement (Visual Basic Language Reference) | For...Next Statements (Visual Basic Language Concepts)

Show:
© 2014 Microsoft