A loop is used to execute repeatedly a sequence of commands, subject to a condition. Loops are formed using command blocks. A distinction is made between three types of loop block which are named after the initiating command: FOR blocks, WHILE blocks and REPEAT blocks.
FOR block
In the FOR block, which is also called a FOR loop, a command sequence is executed for each of the values which is assigned to a control variable.
The FOR block begins with the FOR command and ends with the END-FOR command. The FOR command assigns one of the following to a control variable
a counter,
the value of a list variable,
an expression,
or the values of the elements in a list of expressions, list variables and/or counters.
/FOR variable = counter / list-variable / expression [,CONDITION = condition] [command sequence] /END-FOR |
The user defines a control variable; the number of loop passes and the value of the control variable are determined by the specification to the right of the equals sign (expression, listvariable, counter):
counter
The number of loop passes is determined by the initial value, the final value and the increment. The control value always contains the current loop value.list-variable
The number of loop passes is determined by the number of list 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.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 (string, arithmetic, Boolean, ... expression), working from left to right.condition
As an option, a condition can be specified in the form of a logical expression. Each loop pass will then be preceded by a check of the defined condition. If the condition is no longer met, the loop is terminated.
If a mixed list of counter, list variable and expression 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.
The control variable can be used in the FOR block to modify or replace commands by variable replacement, for instance.
The FOR block can have a tag.
Example 1
/DECLARE-VARIABLE A, MULTIPLE-ELEMENTS=*LIST ————————————————————————— (1) /DECLARE-VARIABLE L —————————————————————————————————————————————————— (2) /SET-VARIABLE A=1436,WRITE-MODE=*EXTEND —————————————————————————————— (3) /A=1455,WRITE-MODE=*EXTEND ——————————————————————————————————————————— (4) /A=1577,WRITE-MODE=*EXTEND ——————————————————————————————————————————— (5) /FOR L=*LIST(A) —————————————————————————————————————————————————————— (6) / CANCEL-JOB JOB-ID=TSN(&L) —————————————————————————————————————————— (7) /END-FOR
Explanations:
(1) | Declaration of list variable A (DECLARE-VARIABLE: see "SDF-P commands "). |
(2) | Declaration of control variable L. |
(3) | The value 1436 is assigned to variable A (SET-VARIABLE: see "SDF-P commands "). WRITE-MODE=*EXTEND means that the list is extended by one element. The value is assigned to this new, last element. In this case, the last element is also the first element of the list. |
(4) | The value 1455 is assigned to variable A as a second value (the command name can be omitted for SET-VARIABLE). |
(5) | The value 1577 is assigned to variable A as a third value. |
(6) | The FOR loop begins with control variable L and the contents of variable A as a value list. |
(7) | For each value in variable A, a CANCEL-JOB command is issued to the TSN that is contained in variable L. |
The following commands are issued by the FOR loop:
/CANCEL-JOB JOB-ID=*TSN(1436) /CANCEL-JOB JOB-ID=*TSN(1455) /CANCEL-JOB JOB-ID=*TSN(1577)
Example 2
/FOR I = *COUNTER(1,8) / ENTER-PROCEDURE ENTERFILE.&I /END-FOR
The FOR loop creates eight ENTER-PROCEDURE commands for the files ENTERFILE.1 through ENTERFILE.8.
WHILE block
In the WHILE block, which is also called a WHILE loop, a command sequence continues to be executed until a condition defined by the user is no longer met. The WHILE block begins with the WHILE command and ends with the END-WHILE command.
The WHILE command contains the condition for traversing the loop; this condition is checked before each loop pass. If the condition is met, the command sequence within the block is executed. If the condition is not met, the loop is terminated and procedure execution is resumed at the command following the END-WHILE command.
The condition in the WHILE command is specified as a logical expression (see chapter“Expressions”).
/[tag:]WHILE condition [command sequence] /END-WHILE [tag] |
Example
/COND = 1 /WHILE (COND < 9) / ENTER-PROCEDURE ENTERFILE.&COND / COND = COND + 1 /END-WHILE
The WHILE loop generates eight ENTER-PROCEDURE commands for the files from ENTERFILE.1 to ENTERFILE.8.
REPEAT block
In the REPEAT block, which is also called a REPEAT loop, a command sequence continues to be executed until a condition defined by the user is met. The block begins with the REPEAT command and ends with the UNTIL command.
Unlike the FOR and WHILE loops, the condition for terminating the loop is not contained in the introductory command but in the block termination command, UNTIL. For this reason, the loop is executed at least once.
At the end of each loop pass, the condition in the UNTIL command is checked. As long as the termination condition is not met, the command sequence within the block is again executed; as soon as the termination condition is met, the commands that follow the UNTIL command are processed.
The condition in the UNTIL command is specified as a logical expression (see chapter“Expressions”).
|
Example
/DECLARE-VARIABLE A /DECLARE-VARIABLE SWITCH-1(TYPE=*BOOLEAN) /SET-VARIABLE A = 5 /SET-VARIABLE SWITCH-1 = ON /REPEAT / A = A + 10 / IF (A > 50) / SET-VARIABLE SWITCH-1 = OFF / END-IF / UNTIL (SWITCH-1 = OFF) /SHOW-VARIABLE A A = 55