DOFOR command

The DOFOR command lets you process a group of commands a specified number of times.

The Do For (DOFOR) command specifies a variable, its initial value, an increment or decrement amount, and a terminal value condition. The format of the Do For (DOFOR) command is:

DOFOR VAR(integer-variable) FROM(initial-value) TO(end-value) BY(integer-constant)

When processing of a DOFOR group is begun, the integer-variable specified on the VAR parameter is initialized to the initial-value specified on the FROM parameter. The value of the integer-variable is compared to the end-value as specified on the TO parameter. When the integer-constant on the BY parameter is positive, the comparison checks for integer-variable greater than the end-value. If the integer-constant on the BY parameter is negative, the comparison checks for integer-variable less than the end-value.

If the condition is not true, the body of the DOFOR group is processed. When the ENDDO is reached, the integer-constant from the BY parameter is added to the integer-value and the condition is evaluated again.

The following is an example of conditional processing with a Do For (DOFOR) command.

CHGVAR &INT2 0
DOFOR VAR(&INT) FROM(2) TO(4) BY(1)
  .
  .
  .
  CHGVAR &INT2 (&INT2 + &INT)
ENDDO
/* &INT2 = 9 after running the DOFOR group 3 times */

When the DOFOR group is processed, &INT is initialized to 2 and the value of &INT is checked to see if it is greater than 4. It is not, so the body of the group is processed. On the second iteration of the group, one is added to &INT and the check is repeated. It is less than 4, so the DOFOR group is processed again. On reaching the ENDDO the second time, the value of &INT is again incremented by 1. &INT now has a value of 4. Since &INT is still less than or equal to 4, the DOFOR group is processed again. On reaching the ENDDO the third time, the value of &INT is again incremented by 1. This time, the value is 5, and processing continues with the command following the ENDDO.

The LEAVE command may be used to exit the DOFOR group and resume processing following the ENDDO. The ITERATE command may be used to skip the remaining commands in the group, increment the controlling variable, and evaluate the end-value condition immediately.

Related information
CL command finder
Do For (DOFOR) command