USING COUNTERS FOR LOOP CONTROL A primary consideration in designing a portion of an EXEC procedure that is to be executed many times is how to control the number of executions. One way to control the execution of a sequence of instructions is to
create a loop that tests and changes the value of a counter.
Before entering the loop, the counter is initialized to a value. Each time through the loop, the counter is adjusted (increased or
decreased) toward a limit value. When the limit value is reached (the
counter value is the same as the limit value), control passes out of the
loop and it is not executed again. For example, the following EXEC initializes a counter based on the argument &1:
&IF &INDEX EQ 0 &EXIT 12 &TYPE COUNT IS &1
&1 = &1 -1
&IF &1 GT 0 &SKIP -2 When this EXEC procedure is invoked, it checks that at least one
argument was passed to it. If an argument is passed, it is assumed to
be a number that indicates how many times the leop is to execute. Each
time it passes through the loop, the value of &1 is decreased by 1. When the value of &1 reaches zero, control passes from the loop to the
next sequential statement.
There are several ways of setting, adjusting, and testing counters. Some ways to set counters are by: Reading arguments from a terminal, such as:
&READ VARS &COUNT1 &COUNT2 Assigning an arbitrary value, such as: &COUNTER = 43 Assigning a variable value or expression, such as: &COUNTS = &INDEX - 1
Counter values can be increased or decreased by any increment or
decrement that meets your purposes. For example: &COUNTEM = &COUNTEM - &RETCODE &COUNT1 = &COUNT + 100 LOOP CONTROL WITH THE &LOOP STATEMENT A convenient way to control execution of a sequence of EXEC statements
is with the &LOOP control statement. An &LOOP statement can be set up
in four ways: To execute a particular number of statements a specified number of
times. For example, the statement: &LOOP 3 2
indicates that the three statements following the &LOOP statement are
to be executed twice. 280 IBM VM/370 eMS User's Guide
To execute a particular number of statements until a specified
condition is satisfied. For example: &LOOP 4 &X = 0 The four statements following this statement are executed until the
value of &X is o. To execute the statements down to (and including) the statement
identified by a label for a specified number of times. For example: &LOOP -ENDLOOP 6
The statements between this &LOOP statement and the label -ENDLOCP are executed six times. To execute the statements down to (and including) the statement
identified by a label until a specified condition is satisfied. In
the following example: &LOOP -ENDLOOP &X = 0 the statements are executed repeatedly until the value of &X is O. The numbers specified for the number of lines to execute and the number of times through the loop must be positive integers. You can use
a variable symbol to represent the integer. If a label is used to
define the limit of the loop, it must follow the &LOOP statement (it
cannot precede the &LOOP statement) In a conditional &LOOP statement, any
conditional phrase are substituted each time example, the statements: &X = 0 &LOOP -END &X EQ 2 &X = &X + 1
-END &TYPE &X are interpreted and executed as follows:
1. The variable &X is assigned a value of O. variable symbols in the
the loop is executed. For 2. The &LOOP statement is interpreted as a conditional form; that is,
to loop to -END until the value of &X equals 2. Since the value cf &X is not 2, the loop is entered.
3. The variable &X is increased by 1 and is then displayed.
4. Control returns to the beginning of the loop, where &X is tested to
see if it equals 2. Since it does not, the loop is executed again
and 2 is displayed. The next time through the loop, when &X equals
2, control is passed to the EXEC statement immediately following
the label -END. When this EXEC procedure is executed, the following lines are
displayed:
1
2
at which time the value of &X equals 2; the loop is not executed again. Section 14. Building EXEC Procedures 281
Previous Page Next Page