ECMA-334 C# Language Specification

15.8.3: The for statement

The for statement evaluates a sequence of initialization expressions and then, while a condition is true, repeatedly executes an embedded statement and evaluates a sequence of iteration expressions.

for-statement
for ( for-initializeropt ; for-conditionopt ; for-iteratoropt ) embedded-statement
for-initializer
local-variable-declaration
statement-expression-list
for-condition
boolean-expression
for-iterator
statement-expression-list
statement-expression-list
statement-expression
statement-expression-list , statement-expression

The for-initializer, if present, consists of either a local-variable-declaration (15.5.1) or a list of statement-expressions (15.6) separated by commas. The scope of a local variable declared by a for-initializer starts at the local-variable-declarator for the variable and extends to the end of the embedded statement. The scope includes the for-condition and the for-iterator.

The for-condition, if present, must be a boolean-expression (14.16).

The for-iterator, if present, consists of a list of statement-expressions (15.6) separated by commas.

A for statement is executed as follows:

Within the embedded statement of a for statement, a break statement (15.9.1) may be used to transfer control to the end point of the for statement (thus ending iteration of the embedded statement), and a continue statement (15.9.2) may be used to transfer control to the end point of the embedded statement (thus executing another iteration of the for statement).

The embedded statement of a for statement is reachable if one of the following is true:

The end point of a for statement is reachable if at least one of the following is true: