informix
Informix Guide to SQL: Syntax
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 establish a finite number of iterations for a loop.

Syntax

Element Purpose Restrictions Syntax
expression Numeric or character value against which loop_var is compared to determine if the loop should be executed The data type of expression must match the data type of loop_var. You can use the output of a SELECT statement as an expression. Expression, p. 4-73
increment_expr Positive or negative value by which loop_var is incremented The default is +1 or -1, depending on left_expression and right_expression. The increment expression cannot evaluate to 0. Expression, p. 4-73
left_expression Starting expression of a range The value of left_expression must match the data type of loop_var. It must be either INT or SMALLINT. Expression, p. 4-73
loop_var Value of this variable determines how many times the loop executes You must have already defined this variable, and the variable must be valid within this statement block. If you are using loop_var with a range of values and the TO keyword, you must define loop_var explicitly as either INT or SMALLINT. Identifier, p. 4-205
right_expression Ending expression in the range The size of right_expression relative to left_expression determines if the range is stepped through positively or negatively. The value of right_expression must match the data type of loop_var. It must be either INT or SMALLINT. Expression, p. 4-73

Usage

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

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.

The database server generates an error if an assignment within the body of the FOR statement attempts to modify the value of loop_var.

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. The following example shows two equivalent FOR statements. Each uses the TO keyword to define a range. The first statement uses the IN keyword, and the second statement uses an equal sign (=). Each statement causes the loop to execute five times.

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. The two statements in the following example are equivalent. In the first statement, the STEP increment is explicit. In the second statement, the STEP increment is implicitly 1.

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.

Specifying Two or More Ranges in a Single FOR Statement

The following example shows a statement that traverses a loop forward and backward and uses different increment values for each direction:

Using an Expression List as the Range

The database server initializes the value of loop_var to the value of the first expression specified. In subsequent iterations, loop_var takes on the value of the next expression. When the server has evaluated the last expression in the list and used it, the loop stops.

The expressions in the IN list do not have to be numeric values, as long as you do not use range operators in the IN list. The following example uses a character expression list:

The following FOR statement shows the use of a numeric expression list:

Mixing Range and Expression Lists in the Same FOR Statement

If loop_var is an INT or SMALLINT value, you can mix ranges and expression lists in the same FOR statement. The following example shows a mixture that uses an integer variable. Values in the expression list include the value that is returned from a SELECT statement, a sum of an integer variable and a constant, the values that are returned from an SPL function named p_get_int, and integer constants.


Informix Guide to SQL: Syntax, Version 9.2
Copyright © 1999, Informix Software, Inc. All rights reserved