Using the TO Keyword to Define a Range
The TO keyword implies a range operator. The range is defined by left_expression and right_expression, and the STEP increment_expr option implicitly sets the number of increments. If you use the TO keyword, loop_var must be an INT or SMALLINT data type.
FOR index_var IN (12 TO 21 STEP 2) -- statement block END FOR; FOR index_var = 12 TO 21 STEP 2 -- statement block END FOR;
If you omit the STEP option, the database server gives increment_expr the
value of -1
if right_expression is less than left_expression,
or +1
if right_expression is more than left_expression.
If increment_expr is specified, it must be negative if right_expression is
less than left_expression, or positive if right expression is
more than left_expression.
1
: FOR index IN (12 TO 21 STEP 1) -- statement block END FOR; FOR index = 12 TO 21 -- statement block END FOR;
The database server initializes the value of loop_var to the value of left_expression. In subsequent iterations, the server adds increment_expr to the value of loop_var and checks increment_expr to determine whether the value of loop_var is still between left_expression and right_expression. If so, the next iteration occurs. Otherwise, an exit from the loop takes place. Or, if you specify another range, the variable takes on the value of the first element in the next range.