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

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 (&).

Type
*VARNAME
Required
Yes
Length
10
Case
Uppercase

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.

Type
*INT4
Required
Yes
Case
Uppercase

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.

Type
*INT4
Required
Yes
Case
Uppercase

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.

Type
*INT4
Required
No
Default
1
Case
Uppercase

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.

IBM command documentation

Generated from os/cl/catalog/DOFOR.json and os/cl/help/DOFOR.json.