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

Examples of basic repetitions

In the following example, the do-group is executed ten times, while the value of the reference I progresses from 1 through 10.

  do I = 1 to 10;

·
·
·
end;

The effect of this DO and END statement is equivalent to the following:

    I = 1;
  A: if I > 10 then go to B;

·
·
·
I = I +1; go to A; B: next statement

The following DO statement executes the do-group three times--once for each assignment of 'Tom', 'Dick', and 'Harry' to Name.

  do Name = 'Tom', 'Dick', 'Harry';

The following statement specifies that the do-group executes thirteen times--ten times with the value of I equal to 1 through 10, and three times with the value of I equal to 13 through 15:

  do I = 1 to 10, 13 to 15;

Repetition using the reference as a subscript

The reference of a DO statement can be used as a subscript in statements within the do-group, so that each execution deals with successive elements of a table or array.

In the following example, the first ten elements of A are set to 1 through 10 in sequence:

  do I = 1 to 10;
     A(I) = I;
  end;

Repetition with TO and BY

The following example specifies that the do-group is executed five times, with the value of I equal to 2, 4, 6, 8, and 10:

  do I = 2 to 10 by 2;

If negative increments of the reference are required, the BY option must be used. For example, the following is executed with I equal to 10, 8, 6, 4, 2, 0, and -2:

  do I = 10 to -2 by -2;

In the following example, the do-group is executed with I equal to 1, 3, 5:

  I=2;
  do I=1 to I+3 by I;

·
·
·
end;

The preceding example is equivalent to:

  do I=1 to 5 by 2;

·
·
·
end;

Terms of use | Feedback

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