5.4.2.3 For Statement

A <for-statement> executes a sequence of statements a specified number of times.

 for-statement = simple-for-statement / explicit-for-statement  
  
 simple-for-statement = for-clause EOS statement-block “Next” 
  
 explicit-for-statement = for-clause EOS statement-block 
 (“Next” / (nested-for-statement “,”)) bound-variable-expression 
 nested-for-statement = explicit-for-statement / explicit-for-each-statement 
 for-clause = “For” bound-variable-expression “=” start-value “To” end-value [step-clause] 
 start-value = expression 
 end-value = expression 
 step-clause = Step" step-increment  
 step-increment = expression 

Static Semantics.

  • If no <step-clause> is present, the <step-increment> value is the integer data value 1.

  • The <bound-variable-expression> within the <for-clause> of an <explicit-for-statement> MUST resolve to the same variable as the <bound-variable-expression> following the <statement-block>. The declared type of <bound-variable-expression> MUST be a numeric value type or Variant.

  • The declared type of <start-value>, <end-value>, and <step-increment> MUST be statically Let-coercible to Double.

Runtime Semantics.

  • The expressions <start-value>, <end-value>, and <step-increment> are evaluated once, in order, and prior to any of the following computations. If the value of <start-value>, <end-value>, and <step-increment> are not Let-coercible to Double, error 13 (Type mismatch) is raised immediately. Otherwise, proceed with the following algorithm using the original, uncoerced values.

  • Execution of the <for-statement> proceeds according to the following algorithm:

    1. If the data value of <step-increment> is zero or a positive number, and the value of <bound-variable-expression> is greater than the value of <end-value>, then execution of the <for-statement> immediately completes; otherwise, advance to Step 2.

    2. If the data value of <step-increment> is a negative number, and the value of <bound-variable-expression> is less than the value of <end-value>, execution of the <for-statement> immediately completes; otherwise, advance to Step 3.

    3. The <statement-block> is executed. If a <nested-for-statement> is present, it is then executed. Finally, the value of <bound-variable-expression> is added to the value of <step-increment> and Let-assigned back to <bound-variable-expression>. Execution then repeats at step 1.

  • If a <goto-statement> defined outside the <for-statement> causes a <statement> within <statement-block> to be executed, the expressions <start-value>, <end-value>, and <step-increment> are not evaluated. If execution of the <statement-block> completes and reaches the end of the <statement-block> without having evaluated <start-value>, <end-value> and <step-increment> during this execution of the enclosing procedure, an error is generated (number 92, “For loop not initialized”). This occurs even if <statement-block> contains an assignment expression that initializes <bound-variable-expression> explicitly. Otherwise, if the expressions <start-value>, <end-value>, and <step-increment> have already been evaluated, the algorithm continues at Step 3 according to the rules defined for execution of a <for-statement>.

  • When the <for-statement> has finished executing, the value of <bound-variable-expression> remains at the value it held as of the loop completion.