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.
Rationale references: 3.10 Short-Circuit Control Forms, 3.12 Loop Statements
Style Guide references: 5.1.3 Exit Statements, 5.6.4 Loops, 5.6.5 Exit Statements
Address any questions or comments to adainfo@sw-eng.falls-church.va.us.