Domain: PROCEDURE
Command description
The FOR command initiates a FOR block, which subsequently ends with the /END-FOR command. The entire construct is a FOR loop. The user defines a control variable <composed-name 1..255> =; the number of loop passes and the value of the control variable depend on the assignment to the right of the equal sign (expression, list variable (*LIST), counter):
*LIST(...)
The number of loop passes is determined by the number of variable elements. With each loop pass, the control variable is assigned the value of the next list element, the elements of the list variable being processed in ascending order.*COUNTER(...)
The number of loop passes is determined by the initial value (FROM=), the final value (TO=) and the increment (INCREMENT=). The control value always contains the current loop value.<text 0..1800 with-low>
The number of loop passes is determined by the number of elements in the value list. With each loop pass, the control variable is assigned the next expression (string, arithmetic, Boolean, ... expression), working from left to right.
If a mixed list containing <text 0..1800 with-low>, *LIST(...) and *COUNTER(...) values is specified, it will be processed from left to right.
Expression replacement (&...) in any of the FOR operands takes place only upon entering the FOR loop and not with each loop pass.
Note
The operands of the FOR command are evaluated by SDF-P and are to be entered as shown in the following. The SDF alias rules apply to the operands. SDF functions that output information on possible operand values or correction dialogs are not available at the operand level. SDF only provides one input field with “# =” in interactive dialogs.
See also section “Defining loops” for more information on FOR blocks.
Format
FOR |
= list-poss(2000): *LIST(...) / *COUNTER (...) / <text 0..1800 with-low expr> *LIST(...) LIST-NAME = <composed-name 1..255> *COUNTER(...) FROM = <text 0..1800 arithm-expr> ,TO = *UNLIMITED / <text 0..1800 arithm-expr> ,INCREMENT = 1 / <text 0..1800 arithm-expr> ,CONDITION = *NONE / <text 0..1800 with-low bool-expr> |
Operands
<composed-name 1..255> =
Designates the control variable. The control variable must have the same type as the elements to the right of the equals sign, or it must be convertible.
<composed-name 1..255> = *LIST(...)
A list variable is assigned to the control variable. The number of loop passes is determined by the number of variable elements. With each loop pass, the control variable is assigned the value of the next list element, the elements of the list variable being processed in ascending order.
If the control variable and the list variable are of type “structure” and contain elements with identical names, then the control variable elements are overwritten by the list variable elements with the same names (see the operand WRITE-MODE = *REPLACE of the SET-VARIABLE command ("SET-VARIABLE Assign value to variable "), section “Lists” (Complex variables ), and chapter “Expressions”).
LIST-NAME=<composed-name 1..255>
Name of the list variable.
<composed-name 1..255> = *COUNTER (...)
The number of loop passes is determined by the initial value (FROM=), the final value (TO=) and the increment (INCREMENT=). These values cannot be modified once execution has started. The control value always contains the current loop value. The value of the control variable can be modified by direct assignment during the loop pass. The control variable must have the type INTEGER, or it must be convertible. See "Expression types " for more information about arithmetic expressions.
FROM = <text 0..1800 arithm-expr>
Initial value of the control variable (numeric or arithmetic expression).
TO= *UNLIMITED / <text 0..1800 arithm-expr>
Final value of the control variable (numeric or arithmetic expression).
*UNLIMITED means: the final value is 231-1 if a value > 0 was specified for the increment, or -231 for an increment value < 0. Exceeding the limit values results in an error (branch to error handling).
INCREMENT = 1 / <text 0..1800 arithm-expr>
Increment by which the current loop value is increased with each loop pass (numeric or arithmetic expression). The (positive/negative) sign determines whether the increment is positive or negative. Incrementing must, however, lead up to the final value, otherwise the loop pass will not be executed.
If the increment changes its sign during the loop pass, the loop pass is aborted.
Note
INCREMENT = 0 will cause loop passes to be executed until the specified condition (Operand CONDITION=...) is no longer met or until EXIT-BLOCK is issued.
<composed-name 1..255> = <text 0..1800 with-low expr>
Any type of expression is permitted here (string, arithmetic, Boolean expression, ...).
The number of loop passes is determined by the number of elements in the value list. With each loop pass, the control variable is assigned the next expression, working from left to right.
CONDITION = *NONE / <text 0..1800 bool-expr>
Defines the condition checked at the beginning of each loop pass. The FOR loop is executed if the condition is “TRUE”. The FOR loop is aborted if the condition is “FALSE”. *NONE means: no condition is evaluated, the loop is always executed. See section“Boolean constants” for more information about Boolean expressions.
Command return codes
(SC2) | SC1 | Maincode | Meaning |
0 | CMD0001 | No error | |
1 | CMD0202 | Syntax error | |
1 | SDP0118 | Command in false context | |
1 | SDP0223 | Incorrect environment | |
3 | CMD2203 | Incorrect syntax file | |
32 | CMD0221 | System error (internal error) | |
64 | SDP0091 | Semantic error | |
130 | SDP0099 | No further address space available |
Example 1
/DECLARE-VARIABLE A,MULTIPLE-ELEMENTS=*LIST /DECLARE-VARIABLE I /SET-VARIABLE A=1436,WRITE-MODE=*EXTEND /A=1455,WRITE-MODE=*EXTEND /A=1577,WRITE-MODE=*EXTEND /FOR I=*LIST(A) /CANCEL-JOB JOB-ID=*TSN(&I) /END-FOR
The FOR loop issues the following commands:
/CANCEL-JOB JOB-ID=*TSN(1436) /CANCEL-JOB JOB-ID=*TSN(1455) /CANCEL-JOB JOB-ID=*TSN(1577)
Example 2
/FOR I=(5,7,12,2,3),CONDITION=(I<10) /SHOW-VARIABLE I /END-FOR
Output:I = 5
I = 7
List elements 12, 2, 3 are not evaluated.
Example 3
Calculate and output the prime numbers from the range 2 up to the specified number.
/ DECL-VAR N(TYPE=*INTEGER) / DECL-VAR PRIMARY-NUMBERS(TYPE=*INTEGER),MULT-ELEM=*LIST / WR-TEXT 'Please enter integer >= 2!' / READ-VAR N,INPUT=*TERMINAL / PRIMARY-NUMBERS# = 2 /LOOP1: FOR I = *COUNTER(FROM=3,TO=N,INCREMENT=2) /LOOP2: FOR J = *LIST(LIST-NAME=PRIMARY-NUMBERS) / CYCLE LOOP1,COND=(I MOD J == 0) / END-FOR LOOP2 / PRIMARY-NUMBERS = I ,MODE=*EXTEND / END-FOR LOOP1 / WR-TEXT 'Prime numbers from the range 2 to &(N):' / SHOW-VAR PRIMARY-NUMBERS,INF=*PAR(NAME=*NONE) / EXIT-PROC
Example 4
Processing a list variable and including an index.
/DECL-VAR LANGUAGES,MULT-ELEM=*LIST /LANGUAGES = 'German', WR-MODE=*EXTEND /LANGUAGES = 'English', WR-MODE=*EXTEND /LANGUAGES = 'French', WR-MODE=*EXTEND /LANGUAGES = 'Spanish', WR-MODE=*EXTEND / /FOR I = *COUNTER(FROM=1,TO=SIZE('LANGUAGES')) / WR-TEXT '&(LANGUAGES#I) is the &(I)th language' /END-FOR
Output
German is the 1st language English is the 2nd language French is the 3rd language Spanish is the 4th language