5 Control Structures


Overview

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'.


If statements

All if statements end with an end if statement.

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.

The final else is optional in this form of the if.


Case statements

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.

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.

Each of the case values must be a static value i.e. it must be able to be computed at compile time.


Loops

All Ada looping constructs use the loop/ end loop form. Several variations exist. The exit statement can be used to break out of loops.


Simple Loops

The simple loop is an infinite loop. It is usually used in conjuction with the exit statement.


While Loops

The while loop is identical to the Pascal while loop. The test is performed before the loop is entered.


For Loops

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.

To count backwards...

A type can be used as a range.

Here the type list is being used as a range. In a similar manner an enumerated type can be used.


Exit and exit when

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.


Labeled loops

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.

Note that the end loop statement is also labelled.


Goto statement

The goto statement is provided in Ada for use in exceptional situations.

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.


to the index...