5.5 Loop Statements
Syntax
Static Semantics
Dynamic Semantics
{execution (loop_statement
with a for iteration_scheme) [partial]} {elaboration
(loop_parameter_specification) [partial]} 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
elaborates the
discrete_subtype_definition.
If the
discrete_subtype_definition
defines a subtype with 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 subtype defined by the
discrete_subtype_definition
(or until the loop is left as a consequence of a transfer of control).
{assignment operation (during execution
of a for loop)} Prior to each such iteration,
the corresponding value of the discrete subtype 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.
Ramification: The order of creating the
loop parameter and evaluating the
discrete_subtype_definition
doesn't matter, since the creation of the loop parameter has no side
effects (other than possibly raising Storage_Error, but anything can
do that).
7 The
discrete_subtype_definition
of a for loop is elaborated just once. Use of the reserved word
reverse
does not alter the discrete subtype defined, 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
Examples
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 -- works even with a null range
if Buffer(J) /= Space then
Put(Buffer(J));
end if;
end loop;
Example of a loop
statement with a name:
Summation:
while Next /= Head
loop --
see 3.10.1
Sum := Sum + Next.Value;
Next := Next.Succ;
end loop Summation;
Wording Changes from Ada 83