Name
For Keyword
Syntax
forVariable:=Expr1toExpr2doStatementforVariable:=Expr1downtoExpr2doStatement
Description
The for keyword introduces a
for loop. A for loop evaluates
the expressions that specify the limits of the loop, then performs
the loop body repeatedly, assigning a new value to the loop control
variable before each loop iteration. The loop control variable must
be a local variable.
The types of Variable,
Expr1, and
Expr2 must match and must be ordinal
types: integer, character, or enumerated. Integer values outside the
range of the Integer type are not supported.
Instead, Delphi silently maps the Expr1
and Expr2 expressions into the range of
Integer, giving results you might not expect.
Tips and Tricks
Delphi optimizes the loop by computing the number of iterations before starting the loop. Changing the limits of the loop (
Expr1andExpr2) inside the loop does not affect the number of times the loop executes.Because the
forloop works only with values that fit in anInteger, if you want to iterate overCardinalorInt64values, you must use awhileloop.After the loop terminates normally, the value of the loop control variable is not defined. If the loop exits because of a
Breakorgotostatement, the loop control variable retains its last value.
See Also
| Do Keyword, Integer Type, Repeat Keyword, While Keyword |
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access