CL Command Reference - DOFOR
CL Command List > DOFOR Reference
Description:
The Do For (DOFOR) command processes a group of CL commands zero or more times based on the values specified for the command.
The loop control CL variable (VAR parameter) is set to the initial value (FROM parameter) and compared to the loop termination value (TO parameter). If the loop increment value (BY parameter) is positive or zero and the control variable is less than or equal the termination value, the commands between the DOFOR and matching ENDDO command are processed. If the loop increment value is negative and the control variable is greater than or equal the termination value, the commands between the DOFOR and matching ENDDO command are processed.
When control reaches the ENDDO command, the loop control variable is adjusted by the loop increment value and compared to the loop termination value. If the control variable is greater than the termination value (if BY is positive or zero) or less than the termination value (if BY is negative), control goes to the command following the ENDDO command. Otherwise, control goes to the first command following the DOFOR statement (the top of the loop).
Restrictions:
. This command is valid only in CL procedures.
. Up to 25 levels of nested DO, DOWHILE, DOUNTIL, and DOFOR commands are allowed.
Examples:
Example 1: DOFOR Command Group Fixed Number of Times
DCL VAR(&INT) TYPE(*INT) LEN(2)
:
DOFOR VAR(&INT) FROM(1) TO(10)
: (group of CL commands)
ENDDO
The group of commands between the DOFOR and ENDDO will be processed 10 times. CL variable &INT will be set to the initial value of 1 and compared to the loop termination value of 10. After each loop iteration, &INT will be incremented by 1 (the default for the BY parameter). After the tenth loop iteration, &INT will have a value of 11 and control will go the command that follows the ENDDO statement.
Note: If the value of CL variable &INT is changed within the group of CL commands in the DOFOR loop, the loop could be processed more or less than 10 times.
Example 2: DOFOR Using Variables for FROM and TO
DCL VAR(&INT) TYPE(*INT) LEN(2)
DCL VAR(&START) TYPE(*INT) LEN(2)
DCL VAR(&END) TYPE(*INT) LEN(2)
:
CHGVAR VAR(&START) VALUE(100)
CHGVAR VAR(&END) VALUE(0)
:
DOFOR VAR(&INT) FROM(&START) TO(&END) BY(-5)
: (group of CL commands)
ENDDO
The group of commands between the DOFOR and ENDDO will be processed 21 times. CL variable &INT will be set to the initial value of 100 and compared to the loop termination value of 0. Because the increment value is negative, the loop is processed until &INT is less than 0. After each loop iteration, &INT will be decremented by 5 and compared to the TO value. After the twenty-first loop iteration, &INT will have a value of -5 and control will go the command that follows the ENDDO statement.
Note: If the values of CL variables &INT or &END are changed within the group of CL commands in the DOFOR loop, the loop could be processed more or less than 21 times. Changing the value of CL variable &START inside the loop will not affect the loop behavior since &START is only used to set the loop control variable (&INT) prior to the first loop iteration.