Ada Programming/Control

condition statement; other statement; ;

condition statement; condition other statement; condition other statement; ...    another statement; ;

;  ; ...  Degrees   Float  -273.15 .. Float'Last; ... Temperature : Degrees; ... Temperature >= 40.0 Put_Line ("Wow!"); Put_Line ("It's extremely hot"); Temperature >= 30.0 Put_Line ("It's hot"); Temperature >= 20.0 Put_Line ("It's warm"); Temperature >= 10.0 Put_Line ("It's cool"); Temperature >= 0.0 Put_Line ("It's cold"); Put_Line ("It's freezing"); ;

In Ada, conditional statements with more than one conditional do not use short-circuit evaluation by default. In order to mimic C/C++'s short-circuit evaluation, use  or   between the conditions.

X     1 Walk_The_Dog; 5       Launch_Nuke; 8 | 10       Sell_All_Stock; Self_Destruct; ;

The subtype of X must be a discrete type, i.e. an enumeration or integer type.

In Ada, one advantage of the case statement is that the compiler will check the coverage of the choices, that is, all the values of the subtype of variable X must be present or a default branch  must specify what to do in the remaining cases.

For procedures:

;

For functions:

Value;

Label; Dont_Do_Something; Label ...

Use_Return Do_Something; Test ;    ;    Do_Something_Else; ; Use_Return;

Use_Goto Do_Something Test Exit_Use_Goto Do_Something_Else Exit_Use_Goto Use_Goto

Use_If Do_Something; Test Do_Something_Else; ;   ;  Use_If;

Endless_Loop : Do_Something; Endless_Loop;

The loop name (in this case, "Endless_Loop") is an optional feature of Ada. Naming loops is nice for readability but not strictly needed. Loop names are useful though if the program should jump out of an inner loop, see below.

While_Loop : X <= 5 X := Calculate_Something; While_Loop;

Until_Loop : X := Calculate_Something; Until_Loop X > 5; Until_Loop;

Exit_Loop : X := Calculate_Something; Exit_Loop X > 5; Do_Something (X); Exit_Loop;

In Ada the exit condition can be combined with any other loop statement as well. You can also have more than one exit statement. You can also exit a named outer loop if you have several loops inside each other.

For_Loop : I Integer  1 .. 10       Do_Something (I) For_Loop;

You don't have to declare both subtype and range as seen in the example. If you leave out the subtype then the compiler will determine it by context; if you leave out the range then the loop will iterate over every value of the subtype given.

As always with Ada: when "determine by context" gives two or more possible options then an error will be displayed and then you have to name the type to be used. Ada will only do "guess-works" when it is safe to do so.

The loop counter I is a constant implicitly declared and ceases to exist after the body of the loop.

Array_Loop : I X'        X (I) := Get_Next_Element; Array_Loop;

With X being an array. Note: This syntax is mostly used on arrays &mdash; hence the name &mdash; but will also work with other types when a full iteration is needed.

Working example
The following example shows how to iterate over every element of an integer type.

; Range_1 Range_Type  -5 .. 10;    T_IO  ; I_IO    (Range_Type); A Range_Type I_IO.Put (Item => A,                 Width => 3,                 Base  => 10); A < Range_Type'Last T_IO.Put (","); T_IO.New_Line; ;    ;  Range_1;

Wikibook

 * Ada Programming

Ada Reference Manual


|Control Programación en Ada/Sentencias y estructuras de control