Enterprise Edition Home | Express Edition Home | Previous Page | Next Page   SPL Statements >

FOR

Use the FOR statement to initiate a controlled (definite) loop when you want to guarantee termination of the loop. The FOR statement uses expressions or range operators to specify a finite number of iterations for a loop.

Syntax

Read syntax diagramSkip visual syntax diagram                           .-,------------------------.
                           V                          |
>>-FOR--loop_var--+-IN--(----+-| Expression Range |-+-+--)-+---->
                  |          | .-,----------.       |      |
                  |          | V            |       |      |
                  |          '---expression-+-------'      |
                  '- =--| Expression Range |---------------'
 
                       (1)
>--| Statement Block |-------END FOR--+---+--------------------><
                                      '-;-'
 
Expression Range:
 
|--left_expression--TO--right_expression------------------------>
 
>--+----------------------+-------------------------------------|
   '-STEP--increment_expr-'
 
Notes:
  1. See Statement Block

Element Description Restrictions Syntax
expression Value to compare with loop_var Must match loop_var data type Expression
increment_expr Positive or negative value by which loop_var is incremented Must return an integer.
Cannot return 0.
Expression
left_expression Starting expression of a range Value must match SMALLINT or INT data type of loop_var Expression
loop_var Variable that determines how many times the loop executes Must be defined and in scope within this statement block Identifier
right_expression Ending expression in the range Same as for left_expression Expression

Usage

The database server evaluates all expressions before the FOR statement executes. If one or more of the expressions are variables whose values change during the loop, the change has no effect on the iterations of the loop.

You can use the output from a SELECT statement as the expression.

The FOR loop terminates when loop_var is equal to the values of each element in the expression list or range in succession, or when it encounters an EXIT FOR statement. An error is issued, however, if an assignment within the body of the FOR statement attempts to modify the value of loop_var.

The size of right_expression relative to left_expression determines if the range is stepped through by positive or negative increments.

Enterprise Edition Home | Express Edition Home | [ Top of Page | Previous Page | Next Page | Contents | Index ]