Command reference / CL
DOFOR
Do For
Runs a group of CL commands zero or more times. The loop control variable (VAR) is set to FROM and tested against TO, with the test direction depending on the sign of BY. At the matching ENDDO, VAR is stepped by BY and retested; the loop repeats while the test passes.
Allowed environments: *BPGM, *IPGM.
Use within a CL program.
Positional parameters: 3.
Restrictions
- Valid only in a CL program or ILE CL procedure.
- Up to 25 levels of nested DO, DOWHILE, DOUNTIL, and DOFOR commands are allowed.
Parameters
VAR
CL variable name
Specifies the CL variable that controls the loop. It must be type *INT or *UINT, and its name must start with an ampersand (&).
FROM
From value
Specifies the initial value assigned to the loop control variable, once, before the first pass. It can be an integer constant, an *INT or *UINT variable, or an integer expression.
TO
To value
Specifies the final value the control variable is compared against. With a negative BY the loop ends when VAR is less than TO; with a positive or zero BY it ends when VAR is greater than TO.
BY
By value
Specifies the amount added to the control variable after each iteration. It must be an integer constant and can be positive, negative, or zero. Default is 1.
Examples
DOFOR VAR(&INT) FROM(1) TO(10)
The group runs 10 times. &INT starts at 1 and is incremented by 1 (the BY default) each pass; after the tenth pass it is 11 and control passes to the command after ENDDO. Changing &INT inside the loop can change the number of passes.
DOFOR VAR(&INT) FROM(&START) TO(&END) BY(-5)
With &START set to 100 and &END set to 0, the group runs 21 times. &INT is decremented by 5 each pass until it is less than 0. Changing &START inside the loop has no effect on the loop, since it is only used to initialize &INT.
Generated from os/cl/catalog/DOFOR.json and os/cl/help/DOFOR.json.