Where allowed to run:
|
Parameters Examples Error messages |
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:
Top |
Keyword | Description | Choices | Notes |
---|---|---|---|
VAR | CL variable name | CL variable name | Required, Positional 1 |
FROM | From value | Integer | Required, Positional 2 |
TO | To value | Integer | Required, Positional 3 |
BY | By value | Integer, 1 | Optional |
Top |
Specifies the CL variable used to control the DOFOR loop. The variable must be of type *INT or *UINT. The name must start with an ampersand (&).
This is a required parameter.
Top |
Specifies the initial value of the CL variable used to control the DOFOR loop. The value must be specified as an integer constant, a CL variable declared as type *INT or *UINT, or an expression which results in an integer value. The initial value is assigned to the loop control CL variable (VAR parameter) only once, prior to processing the group of CL commands between the DOFOR command and the corresponding ENDDO command.
This is a required parameter.
Top |
Specifies the final value to compare to the control variable (VAR parameter) to control the DOFOR loop. The value must be specified as an integer constant, a CL variable declared as type *INT or *UINT, or an expression which results in an integer value. The loop control CL variable (VAR parameter) will be compared to this final value before processing the group of CL commands between the DOFOR and corresponding ENDDO statement, and after each loop iteration.
This is a required parameter.
Top |
Specifies the amount to add to the loop control variable (VAR parameter) after each iteration of the loop. The value must be specified as an integer constant; the value can be positive or negative or zero.
Top |
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.
Top |
None
Top |