The elements of control are those that allow us to control the order of statements on a program module.
Unlike C, Ada is build to avoid side effects (I swear sometimes code in C feels is accidentally succeeding thanks to side effects) therefor the control structures use only Boolean expressions.
Expressions and statements are not interchangeable. Therefor, it's impossible to use an assignation (:=) instead of a comparison (=) . Expressions always resolve to Boolean types (by definition).
Statements that repeat.
As many embedded systems are designed to work indefinitely, you can just have an infinite loop.
[<Loop_name> : ] loop
<code>
[]
end loop [<loop_name>];
If you name your loop you also must specify the loop when it ends.
Loop that executes while some condition is true.
[<Loop_name> : ] while <condition> loop
<code>
end loop [<loop_name>];
The Boolean Condition can be either a variable, a value (but, just use a plain loop or don't), or a Boolean expression, that resolves to a Boolean value.
Loop that executes for an indexed set of values.
[<Loop_name> : ] for <index> in <range> loop
<code>
end loop [<Loop_name>];
Note that the index variable will only exist inside the scope of the loop. But it cannot be manipulated inside the loop (is kind of a constant).
The values will be determined by the range, that has to be of certain type.
for i in Integer range 1..10 loop
x := x+i;
end loop;
for i in range 1..10 loop
put(i);
end loop;
--X is an array
for i in X'Range loop
--Loops over all elements
put( X(i) );
end loop;
To be honest, do-while is not a direct feature of Ada. But it can be easily implemented.
[<Loop_name> : ] loop
<code>
exit [<loop_name>] when <condition>;
[<code>]
end loop [<loop_name>];
As you can see the completion condition can be placed anywhere in the loop.
The name is not mandatory in the exit statement. In case of ambiguity it will leave the last loop.
The values will be determined by the range, that has to be of certain type.
Search: loop
Found := Searching(Area);
exit Search when Found;
report(Area,Clear);
end loop Search;
report(Area,Found);
loop
Put("Nyan Cat Lalala ");
exit when infinite=0;
end loop
boil: loop
heat(100); --100ΒΊC
exit when boil_flag;
end loop boil;
main: loop
<task>;
sleep(1);
end loop main;
Classic Ifs are easily achieved
if <condition> then
<code>
[elsif <condition2>
<code>]
[else
<code>]
end if;
It is resolved in sequential order from top to bottom. If no condition is matched, it executes the else code. If a condition is met, it only executes that code.
It works for conditional expressions too (the triplet a?b:c).
Search: loop
Found := Searching(Area);
exit Search when Found;
report(Area,Clear);
end loop Search;
report(Area,Found);
Put(if (a>0) then "positive" else "negative" );
main: loop
<task>;
sleep(1);
end loop main;
Cases are multi-valued conditions. You may know them as switches. The are not equivalent because cases are inclusive (if two conditions are met, both execute).
case <control_variable> then
[ when <value> => <code> ]
[ when <range> => <code> ]
[ when others => <code> ]
end case;
All possible values of the control variable must be stated, either by range, value, or the code-word others, that exist for (you guessed) all other values non specified.
Conditions can be concatenated with the operator and, or, and then, or else.
And and Or both check for both sides of the operators.
And-Then and Or-Else both check for the left side, and on a different cycle check the right side .
<condition> and <condition>
<condition> or <condition>
<condition> and then <condition>
<condition> or else <condition>
Note that the symbolic representation is also valid
& for and
| for or
You can start a self completed block of code without the need of building a process (but probably would be a better idea).
declare
a: integer := 42;
begin
a := a +1;
end;