5.5. Loop Statements

[PREVIOUS][UP][NEXT]

A loop statement includes a sequence of statements that is to be executed repeatedly, zero or more times.

    loop_statement ::=
       [loop_simple_name:]   
          [iteration_scheme] loop
             sequence_of_statements
           end loop [loop_simple_name]; 

    iteration_scheme ::= while condition
       | for loop_parameter_specification 

    loop_parameter_specification ::=
       identifier in [reverse] discrete_range 

If a loop statement has a loop simple name, this simple name must be given both at the beginning and at the end.

A loop statement without an iteration scheme specifies repeated execution of the sequence of statements. Execution of the loop statement is complete when the loop is left as a consequence of the execution of an exit statement, or as a consequence of some other transfer of control (see 5.1).

For a loop statement with a while iteration scheme, the condition is evaluated before each execution of the sequence of statements; if the value of the condition is TRUE, the sequence of statements is executed, if FALSE the execution of the loop statement is complete.

For a loop statement with a for iteration scheme, the loop parameter specification is the declaration of the loop parameter with the given identifier. The loop parameter is an object whose type is the base type of the discrete range (see 3.6.1). Within the sequence of statements, the loop parameter is a constant. Hence a loop parameter is not allowed as the (left-hand side) variable of an assignment statement. Similarly the loop parameter must not be given as an out or in out parameter of a procedure or entry call statement, or as an in out parameter of a generic instantiation.

For the execution of a loop statement with a for iteration scheme, the loop parameter specification is first elaborated. This elaboration creates the loop parameter and evaluates the discrete range.

If the discrete range is a null range, the execution of the loop statement is complete. Otherwise, the sequence of statements is executed once for each value of the discrete range (subject to the loop not being left as a consequence of the execution of an exit statement or as a consequence of some other transfer of control). Prior to each such iteration, the corresponding value of the discrete range is assigned to the loop parameter. These values are assigned in increasing order unless the reserved word reverse is present, in which case the values are assigned in decreasing order.

Example of a loop statement without an iteration scheme:

    loop
       GET(CURRENT_CHARACTER);
       exit when CURRENT_CHARACTER = '*';
    end loop;                                                                                      

Example of a loop statement with a while iteration scheme:

    while BID(N).PRICE < CUT_OFF.PRICE loop
       RECORD_BID(BID(N).PRICE);
       N := N + 1;
    end loop;  

Example of a loop statement with a for iteration scheme:

    for J in BUFFER'RANGE loop     --  legal even with a null range
       if BUFFER(J) /= SPACE then
          PUT(BUFFER(J));
       end if;
    end loop;  

Example of a loop statement with a loop simple name:

    SUMMATION:
       while NEXT /= HEAD loop       -- see 3.8
          SUM  := SUM + NEXT.VALUE;
          NEXT := NEXT.SUCC;
       end loop SUMMATION;  

Notes:

The scope of a loop parameter extends from the loop parameter specification to the end of the loop statement, and the visibility rules are such that a loop parameter is only visible within the sequence of statements of the loop.

The discrete range of a for loop is evaluated just once. Use of the reserved word reverse does not alter the discrete range, so that the following iteration schemes are not equivalent; the first has a null range.

    for J in reverse 1 ..  0
    for J in 0 .. 1

Loop names are also used in exit statements, and in expanded names (in a prefix of the loop parameter).

References: actual parameter, assignment statement, base type, bound of a range, condition, constant, context of overload resolution, conversion, declaration, discrete range, elaboration, entry call statement, evaluation, exit statement, expanded name, false boolean value, generic actual parameter, generic instantiation, goto statement, identifier, integer type, null range, object, prefix, procedure call, raising of exceptions, reserved word, return statement, scope, sequence of statements, simple name, terminate alternative, true boolean value, and 3.5.4, visibility.


[INDEX][CONTENTS]