Concepts of Programming Languages

(Sean Pound) #1

364 Chapter 8 Statement-Level Control Structures


8.3.1.2 The Ada for Statement
The Ada for statement has the following form:

for variable in [reverse] discrete_range loop

...
end loop;


A discrete range is a subrange of an integer or enumeration type, such as 1..10
or Monday..Friday. The reverse reserved word, when present, indicates that
the values of the discrete range are assigned to the loop variable in reverse order.
The most interesting new feature of the Ada for statement is the scope
of the loop variable, which is the range of the loop. The variable is implicitly
declared at the for statement and implicitly undeclared after loop termination.
For example, in

Count : Float := 1.35;
for Count in 1..10 loop
Sum := Sum + Count;
end loop;

the Float variable Count is unaffected by the for loop. Upon loop termina-
tion, the variable Count is still Float type with the value of 1.35. Also, the
Float-type variable Count is hidden from the code in the body of the loop,
being masked by the loop counter Count, which is implicitly declared to be
the type of the discrete range, Integer.
The Ada loop variable cannot be assigned a value in the loop body. Vari-
ables used to specify the discrete range can be changed in the loop, but because
the range is evaluated only once, these changes do not affect loop control. It
is not legal to branch into the Ada for loop body. Following is an operational
semantics description of the Ada for loop:

[define for_var (its type is that of the discrete range)]
[evaluate discrete range]
loop:
if [there are no elements left in the discrete range] goto out
for_var = [next element of discrete range]
[loop body]
goto loop
out:
[undefine for_var]

Because the scope of the loop variable is the loop body, loop variables are
not defined after loop termination, so their values there are not relevant.

8.3.1.3 The for Statement of the C-Based Languages
The general form of Cā€™s for statement is
Free download pdf