Previous | Next | Table of Contents | Index | Program List | Copyright

6.4 Control Structures: The General LOOP and EXIT Statements

There is another kind of loop statement in Ada that is used less frequently than the FOR and the WHILE loops but comes in very handy in certain situations. This is the general LOOP structure. Instead of a loop control construct at the head of the loop (as in the case of the FOR and the WHILE), loop exit occurs when the EXIT statement is reached. There are two common forms of the EXIT statement. The first is EXIT WHEN:

    EXIT WHEN Distance < 0.5;
Here are a WHILE statement and a general LOOP statement that both accomplish the same purpose, which is to compute and display all powers of 2 less than 10,000:
    Power := 1;
    WHILE Power < 10000 LOOP
        Ada.Integer_Text_IO.Put (Item => Power, Width => 5);
        Power := Power * 2;
    END LOOP;
    
    Power := 1;
    LOOP
        EXIT WHEN Power >= 10000;
        Ada.Integer_Text_IO.Put (Item => Power, Width => 5);
        Power := Power * 2;
    END LOOP;
The test in the EXIT WHEN loop (Power >= 10000) is the complement, or opposite, of the test used in the WHILE loop. The loop body is repeated until the value of Power is greater than or equal to 10,000. Loop repetition stops when the condition is true, whereas in the WHILE loop, repetition stops when the condition is false.

The EXIT WHEN statement is allowed to appear anywhere in the loop body. It is most often used if a loop termination condition is more conveniently placed at the end or middle of a loop body instead of at the top, as is required by FOR and WHILE. We will have occasional opportunities to use this structure in later chapters of the book.

The other form of the EXIT statement is an unconditional statement,

    EXIT;
which appears without an explicit condition. A common example of the use of this statement in Ada appears in the next section; it is associated with robust exception handling.

SYNTAX DISPLAY
General LOOP Statement

Form:
LOOP 
    statement sequence1
    EXIT WHEN condition;
    statement sequence2
END LOOP;

Example:
PowerOf2 := 1; 
LOOP
    MyInt_IO.Put (Item => PowerOf2); 
    PowerOf2 := PowerOf2 * 2;
    EXIT WHEN PowerOf2 > 10000;
END LOOP;

Interpretation:
Statement sequence1 is executed and condition (a Boolean expression) is tested. If condition is found to be true, the loop is exited and the next program statement after END LOOP is executed. If condition is found to be false, statement sequence2 is executed and the loop is repeated.

Note:
EXIT transfers out of the innermost loop in which it appears; that is, if EXIT appears inside a nested loop, only the inner loop is exited.

SYNTAX DISPLAY
EXIT Statement

Form:
EXIT;

Example:
An example will appear in the next section.

Interpretation:
EXIT is a meaningful statement only within a loop structure. EXIT transfers control to the next statement after the nearest END LOOP.


Previous | Next | Table of Contents | Index | Program List | Copyright

Copyright © 1996 by Addison-Wesley Publishing Company, Inc.