Annotated Ada Reference ManualLegal Information
Contents   Index   References   Search   Previous   Next 

5.5 Loop Statements

1
[A loop_statement includes a sequence_of_statements that is to be executed repeatedly, zero or more times.] 

Syntax

2
loop_statement ::= 
   [loop_statement_identifier:]
      [iteration_schemeloop
         sequence_of_statements
       end loop [loop_identifier];
3/3
{AI05-0139-2} iteration_scheme ::= while condition
   | for loop_parameter_specification
   | for iterator_specification
4
loop_parameter_specification ::= 
   defining_identifier in [reversediscrete_subtype_definition
5
If a loop_statement has a loop_statement_identifier, then the identifier shall be repeated after the end loop; otherwise, there shall not be an identifier after the end loop

Static Semantics

6
A loop_parameter_specification declares a loop parameter, which is an object whose subtype is that defined by the discrete_subtype_definition.

Dynamic Semantics

7
For the execution of a loop_statement, the sequence_of_statements is executed repeatedly, zero or more times, until the loop_statement is complete. The loop_statement is complete when a transfer of control occurs that transfers control out of the loop, or, in the case of an iteration_scheme, as specified below.
8
For the execution of 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.
9/3
{AI05-0139-2} {AI05-0262-1} For the execution of a loop_statement with the a for iteration_scheme being for loop_parameter_specification, 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 that satisfies the predicate of the subtype (or until the loop is left as a consequence of a transfer of control). 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. 
9.a
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).
9.b/3
{AI05-0262-1} The predicate (if any) necessarily has to be a static predicate as a dynamic predicate is explicitly disallowed — see 3.2.4.
9.1/3
  {AI05-0262-1} [For details about the execution of a loop_statement with the iteration_scheme being for iterator_specification, see 5.5.2.]
NOTES
10
6  A loop parameter is a constant; it cannot be updated within the sequence_of_statements of the loop (see 3.3).
11
7  An object_declaration should not be given for a loop parameter, since the loop parameter is automatically declared by the loop_parameter_specification. 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. 
11.a
Implementation Note: An implementation could give a warning if a variable is hidden by a loop_parameter_specification.
12
8  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. 
13
for J in reverse 1 .. 0
for J in 0 .. 1
13.a
Ramification: If a loop_parameter_specification has a static discrete range, the subtype of the loop parameter is static.

Examples

14
Example of a loop statement without an iteration scheme: 
15
loop
   Get(Current_Character);
   exit when Current_Character = '*';
end loop;
16
Example of a loop statement with a while iteration scheme: 
17
while Bid(N).Price < Cut_Off.Price loop
   Record_Bid(Bid(N).Price);
   N := N + 1;
end loop;
18
Example of a loop statement with a for iteration scheme: 
19
for J in Buffer'Range loop     --  works even with a null range
   if Buffer(J) /= Space then
      Put(Buffer(J));
   end if;
end loop;
20
Example of a loop statement with a name: 
21
Summation:
   while Next /= Head loop       -- see 3.10.1
      Sum  := Sum + Next.Value;
      Next := Next.Succ;
   end loop Summation;

Wording Changes from Ada 83

21.a
The constant-ness of loop parameters is specified in 3.3, “Objects and Named Numbers”. 

Wording Changes from Ada 2005

21.b/3
{AI05-0139-2} {AI05-0262-1} Generalized iterator_specifications are allowed in for loops; these are documented as an extension in the appropriate clause. 

Contents   Index   References   Search   Previous   Next 
Ada-Europe Ada 2005 and 2012 Editions sponsored in part by Ada-Europe