LEAVE

Purpose

Transfers control out of the immediately containing DO-group or the containing DO-group whose label is specified with the statement.

Syntax

LEAVE [label-reference];

Parameters

label-reference
A reference to a label on a DO statement that heads a containing DO-group. The label reference must be a label constant or a subscripted label constant for which the subscript is specified with an integer constant.

Description

The LEAVE statement transfers control out of the immediately containing DO-group or the containing DO-group whose label is specified with the statement.

Execution of a LEAVE statement with no label reference transfers control to the first statement following the END statement that ends the immediately controlling DO-group. If the LEAVE statement has a label, control is passed to the first executable statement after the END statement for the corresponding DO label indicated in the LEAVE statement. In this manner, the LEAVE statement serves as an alternate means to end the execution of a DO-group.

A LEAVE statement with a label reference can cause several nested DO-groups to be ended as control transfers outside the referenced DO-group.

Example

In the following example, the LEAVE statement, shown without a label reference, transfers control directly to the PUT statement if the condition in the IF statement is satisfied.

DO K = 9 TO 91;
   .
   .
   .
   IF COMMAND = 'QUIT' THEN LEAVE;
   .
   .
   .
END;
PUT LIST ('Job finished');

In the next example, the LEAVE statement, shown with a label reference, transfers control to the first statement following the END statement that ends LOOP2:

LOOP2: DO WHILE (MORE);
   .
   .
   .
   LOOP3: DO J = 2 TO 5;
   . 
   .
   .
      IF QUAN(J) > 150 THEN LEAVE LOOP2; 
   END;     /* Loop 3 */
   .  
   .
   .
END;       /* Loop 2 */

Restrictions

The LEAVE statement must be contained within a DO-group and must be in the same block as the DO statement to which it refers.

If the LEAVE statement has a label reference, it must refer to a label on a DO statement that heads a DO-group that contains the LEAVE statement. The LEAVE statement must be in the same block as the labeled DO statement.

The label reference cannot be a label variable or a subscripted label constant.