Skip to content
Tutorials Center
My learning

Topics

Lesson 10 of 36 ยท Types and Control Flow

If, Case and Loops: Ada Control Flow

Control the flow of an Ada program with if and elsif, exhaustive case statements, for and while loops, bare loops with exit, loop labels, quantified expressions and goto.

Ada's control structures read like English and always close with their own keyword: end if, end case, end loop. There are no dangling-else bugs, no fall-through in case, and loop variables are managed for you. This lesson covers every structure you need for everyday programs.

Ada listings are read-only here; runnable C stand-ins follow.

if, elsif, else

ADA
if Temperature > 30 then
   Put_Line ("hot");
elsif Temperature > 15 then
   Put_Line ("mild");
else
   Put_Line ("cold");
end if;

The condition must be of type Boolean. Combine conditions with and, or, xor, not. The plain and and or evaluate both sides; use and then and or else when the right side must only run if the left allows it:

ADA
if Index <= Max and then Table (Index) = Wanted then
   ...
end if;

Without and then, Table (Index) could be evaluated with a bad index. Ada also has if expressions: Label : constant String := (if X > 0 then "pos" else "non-pos");. Mixing and with or without parentheses is a compile error, so ambiguity cannot hide.

case

ADA
case Grade is
   when 'A'        => Put_Line ("excellent");
   when 'B' | 'C'  => Put_Line ("good");
   when 'D' .. 'E' => Put_Line ("pass");
   when others     => Put_Line ("fail");
end case;

Every possible value must be covered exactly once; others catches the rest. There is no break and no fall-through. The selector can be any discrete type, including integers and enumerations (see enumerations).

for loops

ADA
for I in 1 .. 10 loop
   Put (I);
end loop;

for I in reverse 1 .. 10 loop      --  counting down
   Put (I);
end loop;

for C in Color loop                --  every value of an enumeration
   Put_Line (Color'Image (C));
end loop;

The loop variable is declared by the loop itself, has the type of the range, and is a constant inside the body: you cannot modify it, and it does not exist after the loop. An empty range (5 .. 1) runs zero times. If you need a step other than one, compute it: for K in 0 .. 4 loop Put (K * 2); end loop;.

while and bare loops

ADA
while Remaining > 0 loop
   Remaining := Remaining - 1;
end loop;

loop
   Get (Value);
   exit when Value = 0;       --  leave the loop
   Total := Total + Value;
end loop;

The bare loop ... end loop is Ada's do-while and infinite loop at once; exit when Condition; leaves it. Use exit; alone inside an if. To leave an outer loop, label it:

ADA
Outer : for I in 1 .. 10 loop
   for J in 1 .. 10 loop
      exit Outer when I * J > 50;
   end loop;
end loop Outer;

Quantified and iterator forms (Ada 2012+)

ADA
if (for all X of Items => X > 0) then ...
if (for some X of Items => X = Target) then ...
for Item of Items loop ... end loop;

for all and for some are Boolean expressions over a collection and appear a lot in contracts. They work on arrays and containers.

goto and null

Ada has goto, but its use is rarely appropriate. The empty statement is written null; and is required whenever a branch should do nothing, which makes intent explicit:

ADA
when Idle => null;
flowchart TD
    A["Start loop"] --> B{"exit condition?"}
    B -->|"no"| C["body"]
    C --> A
    B -->|"yes"| D["after loop"]

Bare loops test where you put exit when: at the top, bottom or middle.

Stand-in: loops and short-circuit evaluation in C

C's && and || are short-circuit like and then and or else. This stand-in finds a value in an array with a bounds check first, then runs a labelled-exit search over a multiplication table.

C Stand-in: and then, exit when and a labelled exit

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Here is a second stand-in: a case over ranges with a final others, and a downward for ... in reverse loop.

C Stand-in: case ranges and reverse loops

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Common mistakes

  • Writing elseif or else if instead of elsif.
  • Mixing and and or without parentheses (a compile error in Ada).
  • Trying to modify the loop variable of a for loop.
  • Omitting when others in a case over integers or characters, which fails to compile.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why use and then instead of and?

    and then short-circuits, which makes guards such as an index check safe.
  2. 2. What happens in a case statement that misses a possible value?

    Ada requires every value of the selector type to be covered, directly or with others.
  3. 3. Can you change the loop variable I inside for I in 1 .. 10 loop?

    The loop parameter is read-only and exists only in the loop body.
  4. 4. How do you leave an outer loop labelled Outer from an inner loop?

    exit accepts a loop name.

Summary

  • if ... elsif ... else ... end if needs Boolean conditions; use and then and or else for short-circuiting.
  • case is exhaustive and has no fall-through.
  • for loops declare a read-only variable; while and bare loop with exit when cover the rest.
  • Loop labels allow exits from nested loops; null; is the explicit empty statement.

Next: Procedures and Functions