ILE COBOL Language Reference

PERFORM with VARYING Phrase

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:

condition-1, condition-2
May be any condition described under Conditional Expressions. If the condition is true at the time the PERFORM statement is initiated, the specified procedure(s) are not executed.

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.

Varying Identifiers

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.

Varying One Identifier

This section provides an example that describes how to use the PERFORM statement to vary one identifier.

Example

PERFORM procedure-name-1 THROUGH procedure-name-2
       VARYING identifier-2 FROM identifier-3
             BY identifier-4 UNTIL condition-1
  1. Identifier-2 is set equal to its starting value, identifier-3 (or literal-1).
  2. Condition-1 is evaluated as follows:
    1. If it is false, steps 3 through 5 are executed.
    2. If it is true, control passes directly to the statement following the PERFORM statement.
  3. Procedure-1 and everything up to and including procedure-2 (if specified) is executed once.
  4. Identifier-2 is augmented by identifier-4 (or literal-2), and condition-1 is evaluated again.
  5. Steps 2 through 4 are repeated until condition-1 is true.

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

REQTEXT

Figure 17. Varying One Identifier--with TEST AFTER

REQTEXT

Varying Two Identifiers

This section provides an example that describes how to use the PERFORM statement to vary two identifiers.

Example

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
  1. identifier-2 and identifier-5 are set to their initial values, identifier-3 and identifier-6, respectively.
  2. condition-1 is evaluated as follows:
    1. If it is false, steps 3 through 7 are executed.
    2. If it is true, control passes directly to the statement following the PERFORM statement.
  3. condition-2 is evaluated as follows:
    1. If it is false, steps 4 through 6 are executed.
    2. If it is true, identifier-2 is augmented by identifier-4, identifier-5 is set to the current value of identifier-6, and step 2 is repeated.
  4. procedure-name-1 and everything up to and including procedure-name-2 (if specified) are executed once.
  5. identifier-5 is augmented by identifier-7.
  6. Steps 3 through 5 are repeated until condition-2 is true.
  7. Steps 2 through 6 are repeated until condition-1 is true.

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

REQTEXT

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

REQTEXT

Varying Three Identifiers

This section provides an example that describes how to use the PERFORM statement to vary three identifiers.

Example

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

REQTEXT

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.

Varying More Than Three Identifiers

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.

Varying Phrase Rules

No matter how many variables are specified, the following rules apply:

  1. In the VARYING/AFTER phrases, when an index-name is specified:

    1. The index-name is initialized and incremented or decremented according to the rules under INDEXED BY Phrase. (See also SET Statement.)
    2. In the associated FROM phrase, an identifier must be described as an integer and have a positive value; a literal must be a positive integer.
    3. In the associated BY phrase, an identifier must be described as an integer; a literal must be a nonzero integer.
  2. In the FROM phrase, when an index-name is specified:
    1. In the associated VARYING/AFTER phrase, an identifier must be described as an integer. It is initialized, as described in the SET statement.
    2. In the associated BY phrase, an identifier must be described as an integer and have a nonzero value; a literal must be a nonzero integer.
  3. In the BY phrase, identifiers and literals must have nonzero values.
  4. Changing the values of identifiers and/or index-names in the VARYING, FROM, and BY phrases during execution changes the number of times the procedures are executed.
  5. The way in which operands are incremented or decremented depends on the number of variables specified.


[ Top of Page | Previous Page | Next Page | Table of Contents | Index ]