Rational Developer for System z
Enterprise PL/I for z/OS, Version 3.8, Language Reference Manual

Types 2 and 3

Types 2 and 3 provide for the repetitive execution of the statements within the do-group.

Read syntax diagramSkip visual syntax diagramType 2
 
>>-DO--+-WHILE--(--exp4--)--+-------------------+-+--;---------><
       |                    '-UNTIL--(--exp5--)-' |
       '-UNTIL--(--exp5--)--+-------------------+-'
                            '-WHILE--(--exp4--)-'
 
Read syntax diagramSkip visual syntax diagramType 3
 
                     .-,-----------------.
                     V                   |
>>-DO--reference--=----| specification |-+---------------------><
 
specification:
 
|--exp1--+------------------------+--+------------------------------------------+--|
         +-TO--exp2--+----------+-+  +-WHILE--(--exp4--)--+-------------------+-+
         |           '-BY--exp3-' |  |                    '-UNTIL--(--exp5--)-' |
         +-BY--exp3--+----------+-+  '-UNTIL--(--exp5--)--+-------------------+-'
         |           '-TO--exp2-' |                       '-WHILE--(--exp4--)-'
         '-REPEAT--exp6-----------'
 
expn
An abbreviation for expression n.
WHILE (exp4)
Specifies that, before each repetition of do-group, exp4 is evaluated and, if necessary, converted to a bit string. If any bit in the resulting string is 1, the do-group is executed. If all bits are 0, or the string is null, execution of the Type 2 do-group is terminated. For Type 3, only the execution associated with the specification containing the WHILE option is terminated. Execution for the next specification, if one exists, then begins.
UNTIL (exp5)
Specifies that, after each repetition of do-group, exp5 is evaluated, and, if necessary, converted to a bit string. If all the bits in the resulting string are 0, or the string is null, the next iteration of the do-group is executed. If any bit is 1, execution of the Type 2 do-group is terminated. For Type 3, only the execution associated with the specification containing the UNTIL option is terminated. Execution for the next specification, if one exists, then begins.
reference
The only pseudovariables that can be used as references are SUBSTR, REAL, IMAG and UNSPEC. All data types are allowed.

The generation, g, of a reference is established once at the beginning of the do-group, immediately before the initial value expression (exp1) is evaluated. If the reference generation is changed to h in the do-group, the do-group continues to execute with the reference derived from the generation g. However, any reference to the reference inside the do-group is a reference to generation h. It is an error to free generation g in the do-group.

If a reference is made to a reference after the last iteration is completed, the value of the variable is the value that was out of range of the limit set in the specification. The preceding is true if the following conditions apply to the limit set in the application:

If reference is a program-control data variable, but is not a locator, the BY and TO options cannot be used in specification.

If reference is a program-control variable, but is not a locator or an ordinal, the UPTHRU and DOWNTHRU options cannot be used in specification.

exp1
Specifies the initial value of the reference.

If TO, BY, and REPEAT are all omitted from a specification, there is a single execution of the do-group, with the reference having the value of exp1. If WHILE(exp4) is included, the single execution does not take place unless exp4 is true.

TO exp2
exp2 is evaluated at entry to the specification and saved. This saved value specifies the terminating value of the reference. Execution of the statements in a do-group terminates for a specification as soon as the value of the reference, when tested at the beginning of the do-group, is out of range. Execution of the next specification, if one exists, then begins.

If TO exp2 is omitted from a specification, and if BY exp3 is specified, repetitive execution continues until it is terminated by the WHILE or UNTIL option, or until another statement transfers control out of the do-group.

BY exp3
exp3 is evaluated at entry to the specification and saved. This saved value specifies the increment to be added to the reference after each execution of the do-group.

If BY exp3 is omitted from a specification, and if TO exp2 is specified, exp3 defaults to 1.

If BY 0 is specified, the execution of the do-group continues indefinitely unless it is halted by a WHILE or UNTIL option, or control is transferred to a point outside the do-group.

UPTHRU exp2
exp2 is evaluated at entry to the specification and saved. This saved value specifies the terminating value of the reference. Execution of the statements in a do-group terminates for a specification as soon as the value of the reference, when tested at the end of the do-group, is out of range. Execution of the next specification, if one exists, then begins.

If UPTHRU is specified, the reference is compared to exp2 after the statements in the loop are executed, but before the reference is updated with the next value it can assume. The loop is executed at least once.

UPTHRU is used primarily when processing ordinals using loops; however, it can also be used for a reference which is an arithmetic or locator control variable. If the reference is not an ordinal, the increment to be added to the reference after each execution of the do-group is assumed to be +1.

DOWNTHRU exp2
exp2 is evaluated at entry to the specification and saved. This saved value specifies the terminating value of the reference. Execution of the statements in a do-group terminates for a specification as soon as the value of the reference, when tested at the end of the do-group, is out of range. Execution of the next specification, if one exists, then begins.

If DOWNTHRU is specified, the reference is compared to exp2 after the statements in the loop are executed, but before the reference is updated with the next value it could assume. The loop is executed at least once.

DOWNTHRU is used primarily when processing ordinals using loops; however, it can also be used for a reference which is an arithmetic or locator control variable. If the reference is not an ordinal, the increment to be added to the reference after each execution of the do-group is assumed to be -1.

REPEAT exp6
exp6 is evaluated and assigned to the reference after each execution of the do-group. Repetitive execution continues until it is terminated by the WHILE or UNTIL option, or another statement transfers control out of the do-group.

In Type 3 do-groups, you should not rely on the order in which exp1, exp2 and exp3 are evaluated. Consequently, it is best if none of these expressions invoke functions that set values used in the other expressions.

Control can transfer into a do-group from outside the do-group only if the do-group is delimited by the DO statement in Type 1. Consequently, Type 2 and 3 do-groups cannot contain ENTRY statements. Control can also return to a do-group from a procedure or ON-unit invoked from within that do-group.

The following sections give more information about using Type 2 and Type 3 DO groups. Examples of DO groups begin in Examples of basic repetitions.

Using type 2 WHILE and UNTIL

If a Type 2 DO specification includes both the WHILE and UNTIL option, the DO statement provides for repetitive execution as defined by the following:

  Label:  do while (Exp4)
until (Exp5)
statement-1

·
·
·
statement-n end; Next: statement /* Statement following the do-group */

The above is equivalent to the following expansion:

  Label:  if (Exp4) then;
else
go to Next;
statement-1

·
·
·
statement-n Label2: if (Exp5) then; else go to Label; Next: statement /* Statement following the do-group */

If the WHILE option is omitted, the IF statement at label Label is replaced by a null statement. Note that if the WHILE option is omitted, statements 1 through n are executed at least once.

If the UNTIL option is omitted, the IF statement at label Label2 in the expansion is replaced by the statement GO TO Label.

Using type 3 with one specification

The following sequence of events summarizes the effect of executing a do-group with one specification:

  1. If reference is specified and BY, TO, UPTHRU, or DOWNTHRU options are also specified, exp1, exp2, and exp3 will be evaluated prior to the assignment of exp1 to the reference. Then the initial value is assigned to reference, for example:
      do Reference = Exp1 to Exp2 by Exp3;

    For a variable that is not a pseudovariable, the action of the do-group definition in the preceding example is equivalent to the following expansion:

      E1=Exp1;
      E2=Exp2;
      E3=Exp3;
      V=E1;

    The variable V is a compiler-created based variable with the same attributes as the reference. E1, E2, and E3 are compiler-created variables.

  2. If the TO option is present, test the value of the control variable against the previously-evaluated expression (E2) in the TO option.
  3. If the WHILE option is specified, evaluate the expression in the WHILE option. If it is false, leave the do-group.
  4. Execute the statements in the do-group.
  5. If the UNTIL option is specified, evaluate the expression in the UNTIL option. If it is true, leave the do-group.
  6. If the UPTHRU option is specified, test the value of the control variable against the previously evaluated expression in the UPTHRU expression.
  7. If the DOWNTHRU option is specified, test the value of the control variable against the previously evaluated expression in the DOWNTHRU expression.
  8. If there is a reference:
    1. If the TO or BY option is specified, add the previously-evaluated exp3 (E3) to the reference.
    2. If the REPEAT option is specified, evaluate the exp6 and assign it to the reference.
    3. If the TO, BY, and REPEAT options are all absent, leave the do-group.
    4. If the UPTHRU option is specified and the reference is an ordinal, assign the reference the successor of its current value. Otherwise, add 1 to the reference.
    5. If the DOWNTHRU option is specified and the reference is an ordinal, assign it the predecessor of its current value. Otherwise, subtract 1 from the reference.
    6. If the TO, BY, UPTHRU, DOWNTHRU, and REPEAT options are all absent, leave the do-group.
  9. Go to 2.

Using type 3 with two or more specifications

If the DO statement contains more than one specification, the second expansion is analogous to the first expansion in every respect. However, the statements in the do-group are not actually duplicated in the program. A succeeding specification is executed only after the preceding specification has been terminated.

When execution of the last specification terminates, control passes to the statement following the do-group.

Using type 3 with TO, BY, REPEAT

The TO and BY options let you vary the reference in fixed positive or negative increments. In contrast, the REPEAT option, which is an alternative to the TO and BY options, lets you vary the control variable nonlinearly. The REPEAT option can also be used for nonarithmetic control variables (such as pointer).

If the Type 3 DO specification includes the TO and BY options, the action of the do-group is defined by the following:

  Label:  do Variable=
Exp1
to Exp2
by Exp3
while (Exp4)
until(Exp5);
statement-1

·
·
·
statement-m Label1: end; Next: statement

The action of the previous do-group definition is equivalent to the following expansion. In this expansion, V is a compiler-created variable with the same attributes as Variable; and E1, E2, and E3 are compiler-created variables:

  Label:  E1=Exp1;
  E2=Exp2;
  E3=Exp3;
  V=E1;
  Label2: if (E3>=0)&(V>E2)|(E3<0)&(V<E2) then
go to Next;
if (Exp4) then;
else
 go to Next;
statement-1

·
·
·
statement-m Label1: if (Exp5) then go to Next; Label3: V=V+E3; go to Label2; Next: statement

If the specification includes the REPEAT option, the action of the do-group is defined by the following:

  Label:  do Variable=
Exp1
repeat Exp6
while (Exp4)
until(Exp5);
statement-1

·
·
·
statement-m Label1: end; Next: statement

The action of the previous do-group definition is equivalent to the following expansion:

  Label:  E1=Exp1;
V=E1;
  Label2: ;
if (Exp4) then;
else
  go to Next;
statement-1

·
·
·
statement-m Label1: if (Exp5) then go to Next; Label3: V=Exp6; go to Label2; Next: statement

Additional rules for the sample expansions are as follows:

  1. The previous expansion shows only the result of one specification. If the DO statement contains more than one specification, the statement labeled NEXT is the first statement in the expansion for the next specification. The second expansion is analogous to the first expansion in every respect. Statements 1 through m, however, are not actually duplicated in the program.
  2. If the WHILE clause is omitted, the IF statement immediately preceding statement-1 in each of the expansions is also omitted.
  3. If the UNTIL clause is omitted, the IF statement immediately following statement-m in each of the expansions is also omitted.

Using type 3 with UPTHRU

If the Type 3 DO specification includes the UPTHRU option, the action of the do-group is defined by the following:

  Label:  do Variable = Exp1 upthru Exp2 while (Exp4) until (Exp5);
statement1

·
·
·
statementn Label1: end; Next: statement

The action of the previous do-group is equivalent to the following expansion. In this expansion, V is a compiler-generated variable with the same attributes as Variable; and E1 and E2 are compiler-generated variables:

  Label:  E1 = Exp1;
E2 = Exp2;
V = E1;
  Label2: if (Exp4) then;
else
go to next;
statement1

·
·
·
statementn   Label1: if (Exp5) then go to Next; if V >= E2 then go to Next; V = V + 1; go to Label2; Next: statement

If the reference is an ordinal, the statement V = V + 1 is replaced by V = ordinalsucc(V).

Using type 3 with DOWNTHRU

If the Type 3 DO specification includes the DOWNTHRU option, the action of the do-group is defined by the following:

  Label:  do Variable = Exp1 downthru Exp2 while (Exp4) until (Exp5);
statement1

·
·
·
statementn Label1: end; Next: statement

The action of the previous do-group is equivalent to the following expansion. In this expansion, V is a compiler-generated variable with the same attributes as Variable; and E1 and E2 are compiler-generated variables:

  Label:  E1 = Exp1;
E2 = Exp2;
V = E1;
  Label2: if (Exp4) then;
else
go to Next;
statement1

·
·
·
statementn   Label1: if (Exp5) then go to Next; if V <= E2 then go to Next; V = V - 1; go to Label2; Next: statement

If the reference is an ordinal, the statement V = V - 1 is replaced by V = ordinalpred(V).


Terms of use | Feedback

This information center is powered by Eclipse technology. (http://www.eclipse.org)