The control structures of Ada are similar in style to most conventional languages. However some differences remain.
As usual Ada control structures are designed for maximum readability, all control structures are clearly ended with an 'end something'.
All if statements end with an end if statement.
if boolean expression then statements end if; if boolean expression then statements else other statements end if;
To prevent the common sight of if's marching across the page there is the elsif structure. As many elsifs as required can used. Note the spelling of elsif carefully.
if boolean expression then statements elsif boolean expression then other statements elsif boolean expression then more other statements else even more other statements end if;
The final else is optional in this form of the if.
The case statement must have an action for every possible value of the case item. The compiler checks that this is the case. In situations where it is impractical to list every possible value the others case label should be used.
Each choice's value can be either a single value (e.g. 5), a range (1..20) or a combination of any of these, separated by the character '|' .
Each of the case values must be a static value i.e. it must be able to be computed at compile time.
case expression is
when choices => statements
when choices => statements
. . .
when others => statements
end case;
Important The others option is mandatory in a case statement unless all possible values of the case selector have been enumerated in the when statements.
case letter is
when 'a'..'z'| 'A'..'Z' => put ("letter");
when '0'..'9' => put ("digit! value is"); put (letter);
when ''' | '"' | '`' => put ("quote mark");
when '&' => put ("ampersand");
when others => put ("something else");
end case;
Each of the case values must be a static value i.e. it must be able to be computed at compile time.
All Ada looping constructs use the loop/ end loop form. Several variations exist. The exit statement can be used to break out of loops.
The simple loop is an infinite loop. It is usually used in conjuction with the exit statement.
loop
statements
end loop;
The while loop is identical to the Pascal while loop. The test is performed before the loop is entered.
while boolean expression loop statements end loop;
The for looping contructs are similar to those in Pascal.
There are several rules that apply to the use of for statements.
l The index in the for loop must be a discrete type - floats are not allowable.
l The index is not explicity declared.
l The index cannot be modified by any statements (read only)
Note that the statements will not be executed if the lower value of the range is higher than the upper value.
Important The index used in the for loop does not need to be declared. It is implicitly declared to be of the same type as the range.
for index in range loop
statements
end loop;
for i in 1..20 loop
put (i);
end loop;
To count backwards...
for index in reverse range loop statements end loop;
for i in reverse 1..20 loop put(i); end loop;
A type can be used as a range.
declare
subtype list is integer range 1..10;
begin
for i in list loop
put(i);
end loop;
end;
Here the type list is being used as a range. In a similar manner an enumerated type can be used.
The exit and exit when statements can be used to exit loops prematurely. Execution continues with the first statement following the loop. The two forms have identical effects. The following code segments are identical.
loop
statements
if boolean expression then
exit;
end if;
end loop;
loop
statements
exit when boolean expression;
end loop;
An exit statement will normally only exit the inner most loop in which it is enclosed. We can label loops and modify the exit statement accordingly to allow for an escape from a series of nested loops. In all cases the instruction next executed is that following the loop exited.
outer_loop:
loop
statements
loop
statements
exit outer_loop when boolean_expression;
end if;
end loop;
end loop outer_loop;
Note that the end loop statement is also labelled.
The goto statement is provided in Ada for use in exceptional situations.
goto label;
<<label>>
The use of goto's is very restrictive and quite sensible. You cannot jump into if statements, loop statements or, unlike Pascal, out of procedures.