FOR / NEXT

FOR / NEXT

Top  Previous  Next

 

The FOR / NEXT statement defines a group of statements to be executed with an iterative control variable.

 

 

Format

 

FOR var = start.expr TO limit.expr {STEP step.expr}

{WHILE expr}

{UNTIL expr}

     ...statement(s)...

NEXT {var}

 

 

FOR var = value1, value2, value3...

{WHILE expr}

{UNTIL expr}

     ...statement(s)...

NEXT {var}

 

 

FOR EACH var IN string {DELIMITER delim}

{WHILE expr}

{UNTIL expr}

     ...statement(s)...

NEXT {var}

 

where

 

varis the loop control variable.

 

start.exprevaluates to the value to be placed in var for the first iteration of the loop.

 

limit.exprevaluates to the value beyond which var must not pass.

 

step.exprevaluates to the value by which var should be incremented between iterations of the loop. If omitted, step.expr defaults to one.

 

value1,value2...are values to be assigned to var.

 

stringis an expression that evaluates to a mark character delimited list of values to be assigned to var.

 

delimis the delimiting character separating entries in the list. If omitted, any mark character is treated as the delimiter.

 

statement(s)are statement(s) to be executed within the loop.

 

 

The first form of the FOR / NEXT statement executes statement(s) for values of var from start.expr to limit.expr in increments of step.expr. If step.expr is positive, the loop continues while the value of var is less than or equal to limit.expr.  If step.expr is negative, the loop continues while the value of var is greater than or equal to limit.expr. The value of var on leaving the loop is the last value for which the loop was executed or start.expr if the initial value was already out of range.

 

The value of var should not be changed within the loop as this may lead to unexpected results.

 

Use of non-integer values for start.expr, limit.expr and step.expr is not recommended where rounding errors in incrementing var and the loop termination comparison may lead to unexpected effects.

 

For best performance, limit.expr and step.expr should be constants or simple variable references as they are evaluated for every iteration of the loop. In the unlikely case that limit.expr or step.expr use the value of var, note that these expressions are evaluated before the first cycle of the loop assigns the start value to var.

 

 

The second form of the FOR / NEXT statement executes the loop for each of the supplied values of var in turn. The value of var on leaving the loop will be the final value for which the loop was executed. The values to be assigned to var may be constants or expressions.

 

 

The third form of the FOR / NEXT statement executes the loop for each delimited value in string. The optional DELIMITER clause specifies the character that separates items in the list. If omitted, any mark character will be treated as the delimiter. This form of FOR / NEXT is broadly equivalent to using the REMOVE or REMOVEF statement to extract successive values from string. The value of string is copied to an internal variable at the start of the loop and hence changing it inside the loop will have no effect. The value of var on leaving the loop will be the final value for which the loop was executed.

 

 

If var is present in the NEXT statement it must be the same var as in the FOR statement at the head of the loop. Use of var in the NEXT statement aids program readability and is checked by the compiler for correct matching of FOR and NEXT statements.

 

The WHILE, UNTIL and EXIT statements can be used with the FOR / NEXT loop to provide another loop termination control. The CONTINUE statement causes a jump to the start of the next iteration. Any number of these may be included in the loop.

 

FOR / NEXT loops may be nested to any depth. Jumping to a label within a FOR / NEXT loop from outside may have undefined effects.

 

The FOR / NEXT construct is semantically different in the various multivalue database environments. Some, including QM, test the end condition of the loop before storing the updated control variable such that a loop

FOR I = 1 TO 10

would exit with I set to 10. Others store the value before testing the end condition such that this same loop would exit with I set to 11.

 

The FOR.STORE.BEFORE.TEST option of the $MODE compiler directive can be used to modify the behaviour of FOR / NEXT constructs to store the new value of the control variable before testing for the end condition. For ease of maintenance and portability, it is recommended that programs should not rely on the value of the control variable after the loop has terminated.

 

 

Examples

 

FOR I = 1 TO 10 STEP 2

  DISPLAY I

NEXT I

 

This program fragment displays the values 1, 3, 5, 7 and 9. The final value of I on leaving the loop is 9.

 

 

FOR I = 1 TO 20

UNTIL A(I) < 0

  DISPLAY A(I)

NEXT I

 

This program fragment displays elements of matrix A. The loop terminates if an element is found with a negative value.

 

 

FOR I = 1, 7, 22

  DISPLAY I

NEXT I

 

This program fragment displays the numbers 1, 7 and 22.

 

 

  FOR EACH X IN SALES.REC<S.ITEM>

  DISPLAY X

NEXT X

 

This program fragment walks though the values in SALES.REC<S.ITEM>, displaying each value. Note that the actual mark character separating the items in the list is not significant and cannot be determined from within the loop.

 

 

See also:

CONTINUE, EXIT, LOOP/REPEAT, WHILE, UNTIL