The VARYING phrase increases or decreases the value of one or more identifiers or index-names, according to certain rules. (See Varying Phrase Rules.)
The Format 4 VARYING phrase PERFORM statement can serially search an entire 7-dimensional table.
PERFORM Statement - Format 4
>>-PERFORM------------------------------------------------------>
>--+-procedure-name-1--+-------------------------------+--| phrase 3 |-+-><
| '-+-THROUGH-+--procedure-name-2-' |
| '-THRU----' |
'-| phrase 3 |--imperative-statement-1--END-PERFORM-----------------'
phrase 3:
|--+----------------------------+--VARYING--+-identifier-2-+---->
'-+------+--TEST--+-BEFORE-+-' '-index-name-1-'
'-WITH-' '-AFTER--'
>--FROM--+-identifier-3-+--BY--+-identifier-4-+----------------->
+-index-name-2-+ '-literal-2----'
'-literal-1----'
>--UNTIL--condition-1--+------------------+---------------------|
| .--------------. |
| V | |
'---| phrase 4 |-+-'
phrase 4:
|--AFTER--+-identifier-5-+--FROM--+-identifier-6-+--BY---------->
'-index-name-3-' +-index-name-4-+
'-literal-3----'
>--+-identifier-7-+--UNTIL--condition-2-------------------------|
'-literal-4----'
Related Information:
After the condition(s) specified in the UNTIL phrase are satisfied, control is passed to the next executable statement following the PERFORM statement.
If any of the operands specified in condition-1 or condition-2 is subscripted, reference modified, or is a function-identifier, the subscript, reference-modifier, or function is evaluated each time the condition is tested.
+-------------------------------IBM Extension--------------------------------+
Floating-point data items and literals can be used anywhere a numeric data item or literal can be specified.
+----------------------------End of IBM Extension----------------------------+
When TEST BEFORE is indicated, all specified conditions are tested before the first execution, and the statements to be performed are executed, if at all, only when all specified tests fail. When TEST AFTER is indicated, the statements to be performed are executed at least once, before any condition is tested. Any subscripting associated with the operands specified in condition-1 is evaluated each time the condition is tested.
If neither the TEST BEFORE nor the TEST AFTER phrase is specified, the TEST BEFORE phrase is assumed.
The way in which operands are increased or decreased depends on the number of variables specified. In the following discussion, every reference to identifier-n refers equally to index-name-n (except when identifier-n is the object of the BY phrase).
If identifier-2 or identifier-5 is subscripted, the subscripts are evaluated each time the content of the data item referenced by the identifier is set or augmented. If identifier-3, identifier-4, identifier-6, or identifier-7 is subscripted, the subscripts are evaluated each time the content of the data item referenced by the identifier is used in a setting or an augmenting operation.
This section provides an example that describes how to use the PERFORM statement to vary one identifier.
PERFORM procedure-name-1 THROUGH procedure-name-2
VARYING identifier-2 FROM identifier-3
BY identifier-4 UNTIL condition-1
At the end of PERFORM statement execution identifier-2 has a value that exceeds the last-used setting by the increment/decrement value (unless condition-1 was true at the beginning of PERFORM statement execution, in which case, identifier-2 contains the current value of identifier-3).
Figure 16 illustrates the logic of the PERFORM statement when an identifier is varied with TEST BEFORE. Figure 17 illustrates the logic of the PERFORM statement when an identifier is varied with TEST AFTER.
Figure 16. Varying One Identifier--with TEST BEFORE

Figure 17. Varying One Identifier--with TEST AFTER

This section provides an example that describes how to use the PERFORM statement to vary two identifiers.
PERFORM procedure-name-1 THROUGH procedure-name-2
VARYING identifier-2 FROM identifier-3
BY identifier-4 UNTIL condition-1
AFTER identifier-5 FROM identifier-6
BY identifier-7 UNTIL condition-2
At the end of PERFORM statement execution:
Figure 18 illustrates the logic of the PERFORM statement when two identifiers are varied with TEST BEFORE. Figure 19 illustrates the logic of the PERFORM statement when two identifiers are varied with TEST AFTER.
Figure 18. Varying Two Identifiers--with TEST BEFORE

The previous figure assumes that identifier-5 and identifier-2 are not related. If one is dependent on the other (through subscripting, for example), the results may be predictable but generally undesirable.
Figure 19. Varying Two Identifiers--with TEST AFTER

This section provides an example that describes how to use the PERFORM statement to vary three identifiers.
PERFORM procedure-name-1 THROUGH procedure-name-2
VARYING identifier-2 FROM identifier-3
BY identifier-4 UNTIL condition-1
AFTER identifier-5 FROM identifier-6
BY identifier-7 UNTIL condition-2
AFTER identifier-8 FROM identifier-9
BY identifier-10 UNTIL condition-3
The actions are the same as those for two identifiers, except that identifier-8 goes through the complete cycle each time identifier-5 is augmented by identifier-7, which, in turn, goes through a complete cycle each time identifier-2 is varied.
At the end of PERFORM statement execution:
Figure 20 illustrates the logic of the PERFORM statement when three identifiers are varied.
Figure 20. Format 4 PERFORM Statement Logic--Varying Three Identifiers

The previous figure assumes that identifier-5 and identifier-2 are not related. If one is dependent on the other (through subscripting, for example), the results may be predictable but generally undesirable.
The previous figure also assumes that identifier-8 and identifier-5 are not related. If one is dependent on the other (through subscripting, for example), the results may be predictable but generally undesirable.
In the VARYING phrase, you may extend the previous examples by adding up to four more AFTER phrases, for a total of six AFTER phrases.
This is an example that describes how to use the PERFORM statement to vary more than three identifiers.
No matter how many variables are specified, the following rules apply:
(C) Copyright IBM Corporation 1992, 2006. All Rights Reserved.