5.7. Exit Statements

[PREVIOUS][UP][NEXT]

An exit statement is used to complete the execution of an enclosing loop statement (called the loop in what follows); the completion is conditional if the exit statement includes a condition.

    exit_statement ::=
       exit [loop_name] [when condition]; 

An exit statement with a loop name is only allowed within the named loop, and applies to that loop; an exit statement without a loop name is only allowed within a loop, and applies to the innermost enclosing loop (whether named or not). Furthermore, an exit statement that applies to a given loop must not appear within a subprogram body, package body, task body, generic body, or accept statement, if this construct is itself enclosed by the given loop.

For the execution of an exit statement, the condition, if present, is first evaluated. Exit from the loop then takes place if the value is TRUE or if there is no condition.

Examples:

    for N in 1 .. MAX_NUM_ITEMS loop
       GET_NEW_ITEM(NEW_ITEM);
       MERGE_ITEM(NEW_ITEM, STORAGE_FILE);
       exit when NEW_ITEM = TERMINAL_ITEM;
    end loop; 

    MAIN_CYCLE:
       loop
          --  initial statements
          exit MAIN_CYCLE when FOUND;
          --  final statements
       end loop MAIN_CYCLE;

Note:

Several nested loops can be exited by an exit statement that names the outer loop.

References: accept statement, condition, evaluation, generic body, loop name, loop statement, package body, subprogram body, true boolean value.


[INDEX][CONTENTS]