• Aucun résultat trouvé

The expression following the IF keyword can be only an element expression; i t

Dans le document OS PL/I (Page 67-70)

cannot be an array or structure expression.

I t can, however, be a logical expression with more than one operator. For example:

IF A

=

B , C

=

D THEN GO TO Ri

The same kind of test could be made with nested IF statements. The following three examples are equivalent:

Example 1:

IF A = B , C = D THEN GO TO Ri B = B + 1;

Example 2:

IF A = B

THEN IF C = D

THEN GO TO R:

B = B + 1:

Example 3:

IF A .,= B THEN GO TO Si IF C .,= D THEN GO TO S:

GO TO R:

S: B = B + 1:

ISELECT STATEMENT

I

IThe SELECT statement heads a select-group.

I

A select-group provides a multi-way conditional branch and has the following form:

SELECT CE);

WHEN CE1,E2,E3) action-l:

WHEN CE4,E5) action-2i

OTHERWISE action-n;

END:

NL: next statement;

In this example, E, El, etc.; are expressions. When control reaches the ISELECT statement, the expression E is levaluated and its value is saved. The lexpressions in the WHEN clauses are then levaluated in turn (in the order in which Ithey appear), and each value is compared Iwith the value of E. If a value is found Ithat is equal to the value of E, the action Ifollowing the corresponding WHEN clause is I performed; no further WHEN clause

lexpressions are evaluated. If none of the lexpressions in the WHEN clauses is equal to Ithe expression in the SELECT statement, the laction specified after the OTHERWISE clause lis executed unconditionally.

The 'action' after a WHEN or OTHERWISE clause may be a single or compound

statement, a do-group, a select-group, or a begin block. After the 'action' has been performed, control passes to the first executable statement tollowing the select-group, unless the normal flow is changed by the specified action.

If the expression in the SELECT statement is omitted, each WHEN clause expression is evaluated and converted, if necessary, to a bit string. The action after the WHEN clause is performed if any bit in the resulting bit string is a 'l'B.

For example:

SELECT:

WHEN CA>B) CALL BIGGER;

WHEN (A=B) CALL SAMEi OTHERWISE CALL SMALLER;

END;

If a select-group contains no WHEN

clauses, the action in the OTHERWISE clause is executed unconditionally. If the

OTHERWISE clause is omitted, and execution of the select-group does not result in the selection of a WHEN clause, the ERROR condition is raised.

The following example shows nested

select-groups used to set a variable to the number of days in a specified month.

DECLARE MONTH CHAR(3), YEAR PIC'99',

NO_DAYS FIXED BINARY;

SELECTCMONTH) ;

WHENC'FEB') SELECT (MODCYEAR,4»;

WHENCO) NO DAYS = 29:

OTHERWISE NO_DAYS = 28;

END:

WHENC'APR','JUN','SEP','NOV')

NO_DAYS = 30;

OTHERWISE NO_DAYS = 31;

END;

In this example, the MOD built-in

function returns the remainder when YEAR is divided by 4. (The algorithm is incorrect for century years.)

100 STATEMENT

I I

IThe DO statement, and its corresponding END I statement, delimit a group of statements Icollectively called a do-group.

I

I A common use of the DO statement is to Ispecify that a group of statements is to be lexecuted a stated number of times while a

Chapter 5: Statement Classification 55

Icontrol variable is incremented each time Ithrough the loop. Such a group might take Ithe form:

I

I DO I = 1 TO 10;

I I

I I

END;

I

In this example, the group of statements will be executed ten times, while the value of the control variable I ranges from 1 through 10. The effect of the DO and END statements would be the same as the following:

I

=

1;

A: IF I

>

10 THEN GO TO B;

I

=

I +1:

GO TO A:

B: next statement

Note that the increment is made before the control variable is tested and that, in general, control goes to the statement following the group only when the value of the control variable exceeds the limit set in the DO statement. If a reference is made to a control variable after the last iteration is completed, the value of the variable will be one increment beyond the specified limit.

The increment applied to the control variable is assumed to be one unless some other value is stated, as follows:

DO I

=

2 TO 10 BY 2;

This specifies that the loop is to be executed five times, with the value of I equal to 2, 4, 6, 8, and 10.

If negative increments of the control variable are required, the BY option must be used. For example:

DO I

=

10 TO 1 BY -1;

The TO and BY options enable the control

Ivari~ble to be varied in fixed positive or Inegative increments. In contrast, the IREPEAT option, which is an alternative to Ithe TO and BY options, enables the control Ivariable to be varied non-linearly. It is lused in the following way:

I I

DO I

=

1 REPEAT 2*1;

I I

I

END:

I

I In this example, the control variable I Ihas the value 1 for the first execution of

56 OS PL/I CKT AND OPT LRM PART I

the group. For succeeding executions, the

express~on in the REPEAT option (in this example, 2*1) is evaluated and assigned to the control variable. The group is thus executed with I equal to 1, 2, 4, 8" 16, and so on.

The effect of the preceding example is the same as the following:

1=1:

A:

1=2*1 : GOTO A:

Note that the REPEAT option does not specify a terminal condition, and execution of the group will continue indefinitely unless i t is halted by a WHILE or UNTIL option (see following paragraphs) or control is transferred to a point outside Ithe group.

I

I The WHILE and UNTIL options provide a Imethod of making successive executions of Ithe do-group dependent upon a specified I condition. Their basic format is:

I I

DO WHILE (A=B):

I

I DO UNTIL (A=B);

I

I In the DO WHILE statement, the

lexpression in the WHILE option is evaluated Ibefore each execution of the do-group. If Ithe expression is 'true', the do-group is I executed; i f i t is not, control passes to the first executable statement follOWing the do-group. I t is thus equivalent to the following:

S: IF A=B THEN;

ELSE GOTO R;

GOTO S;

R: next statement

In the DO UNTIL statement, the

expression in the UNTIL option is evaluated after each execution of the group. I f the expression is 'true', control passes to the first executable statement follOWing the do-group; otherwise, the group is executed again. I t is thus equivalent to the

following:

S:

.

'--IF (A=B) THEN GOTO R:

GOTO S:

R: next statement

Note that (in the absence of other loptions) a do-group headed by a DO UNTIL Istatement is executed at least once, but a

Ido-group headed by a DO WHILE statement may

Inot be executed at all. That is, the

Istatements DO WHILE (A=B) and DO UNTIL

Dans le document OS PL/I (Page 67-70)