Leave (LEAVE)

Where allowed to run:
  • Batch program (*BPGM)
  • Interactive program (*IPGM)
Threadsafe: Yes
Parameters
Examples
Error messages

The Leave (LEAVE) command ends the processing of commands in the associated DOWHILE, DOUNTIL, or DOFOR loop and passes control to the first command following the associated ENDDO command.

The following command sequence shows this flow.

L1:     DOWHILE &LGL1
        ...
L2:     DOWHILE &LGL2
        ...
        IF &LGL3 (LEAVE CMDLBL(L1))
        IF &LGL4 LEAVE
        ...
        ENDDO
        /* Here if &LGL4 evaluates to true */
        ...
        ENDDO
        /* Here if &LGL3 evaluates to true */
        ...

Restrictions:

Top

Parameters

Keyword Description Choices Notes
CMDLBL Command label Simple name, *CURRENT Optional, Positional 1
Top

Command label (CMDLBL)

The label must be within the same program as the LEAVE command and be a label on an active DOWHILE, DOUNTIL, or DOFOR group. A CL variable name cannot be used to specify the label name.

*CURRENT
Leaves the innermost loop surrounding this LEAVE command.
simple-name
Specify the label name of the surrounding DOWHILE, DOUNTIL, or DOFOR command which is being ended.
Top

Examples

Example 1: Leave Simple DOFOR Loop

DCL   VAR(&INT)  TYPE(*INT)  LEN(2)
DCL   VAR(&NAME)  TYPE(*CHAR)  LEN(10)
 :
DOFOR   VAR(&INT)  FROM(0)  TO(10)
 :   (group of CL commands)
 IF   COND(&NAME *EQ *NONE)   THEN(LEAVE)
 :   (group of CL commands)
ENDDO

The LEAVE command interrupts processing of the active DOFOR group and processing continues with command following the ENDDO.

Example 2: Leave with Nested Loops

DCL   VAR(&INT)  TYPE(*INT)  LEN(2)
DCL   VAR(&NAME)  TYPE(*CHAR)  LEN(10)
DCL   VAR(&LGL)  TYPE(*LGL)  VALUE('1')      /* True */
 :
LOOP1: DOFOR   VAR(&INT)  FROM(0)  TO(10)
 :   (group of CL commands)
 LOOP2: DOUNTIL   COND(&LGL)
   :   (group of CL commands)
   IF   COND(&NAME *EQ *NONE)  THEN(LEAVE  CMDLBL(LOOP1))
   :   (group of CL commands)
 ENDDO /* DOUNTIL */
 :   (group of CL commands)
ENDDO /* DOFOR */

The LEAVE command interrupts processing of both the active DOUNTIL and DOFOR groups and processing continues with command following the ENDDO matching the DOFOR command.

Top

Error messages

None

Top