Ada conditionals
last modified November 23, 2026
A conditional selects which statements run, based on a
Boolean expression. Ada has two conditional statement forms,
if and case, and since Ada 2012 it also has two
expression forms that produce a value instead of choosing statements. This article covers all four,
together with the rules that make a case statement exhaustive.
if, elsif, and else
An if statement tests its condition and runs the statements after
then when the condition is true. The elsif keyword
adds another test, and else catches everything that remains. The
conditions are tried in order, and the first true one wins, so a later
condition is never evaluated once an earlier branch has been chosen.
with Ada.Text_IO;
use Ada.Text_IO;
procedure Grade is
Score : Integer := 0;
begin
for Round in 1 .. 4 loop
Score := 102 - Round * 10;
if Score >= 90 then
Put_Line (Integer'Image (Score) & ": grade A");
elsif Score >= 80 then
Put_Line (Integer'Image (Score) & ": grade B");
elsif Score >= 70 then
Put_Line (Integer'Image (Score) & ": grade C");
else
Put_Line (Integer'Image (Score) & ": below C");
end if;
end loop;
end Grade;
The four scores are 92, 82, 72, and 62, and each one selects exactly one
branch. The second test does not have to repeat the upper bound, because the
first test has already excluded every score of 90 and above. The reserved word
then is required after each condition, and the whole statement
ends with end if;, which makes the extent of the branches visible
without counting indentation levels.
$ gnatmake -gnat2022 -gnatwa grade.adb $ ./grade 92: grade A 82: grade B 72: grade C 62: below C
Nested conditionals
The statements inside a branch may themselves be conditional. A nested
if is useful when a decision depends on two facts and the second
question only makes sense after the first has been answered. Ada does not
require parentheses around the inner statement, but the compiler insists that
every if has its own end if.
with Ada.Text_IO;
use Ada.Text_IO;
procedure Admission is
Age : Integer := 0;
Member : Boolean := False;
begin
for Round in 1 .. 2 loop
Age := 15 + Round * 10;
Member := Round = 2;
if Age >= 18 then
if Member then
Put_Line ("adult member");
else
Put_Line ("adult guest");
end if;
else
Put_Line ("minor");
end if;
end loop;
end Admission;
The first round produces an adult guest and the second an adult member; the
minor branch is never reached because both ages are at least 25. The inner
if could be replaced by if Age >= 18 and Member
then, and the two forms would behave the same here. The nested form is
preferable when the outer branch needs to do more work before the second
question is asked.
$ gnatmake -gnat2022 -gnatwa admission.adb $ ./admission adult guest adult member
case with ranges and alternatives
A case statement compares one discrete value against a list of
choices and runs the statements of the matching choice. A choice may be a
single value, a range such as 3 .. 5, or several values joined
with a vertical bar. The compiler checks that no value is covered twice and
that every possible value is covered, either by a choice or by
when others.
with Ada.Text_IO;
use Ada.Text_IO;
procedure Season is
Month : Integer := 0;
begin
for M in 1 .. 13 loop
Month := M;
Put (Integer'Image (Month) & ": ");
case Month is
when 3 .. 5 =>
Put_Line ("spring");
when 6 .. 8 =>
Put_Line ("summer");
when 9 .. 11 =>
Put_Line ("autumn");
when 12 | 1 | 2 =>
Put_Line ("winter");
when others =>
Put_Line ("not a month");
end case;
end loop;
end Season;
Months 3 to 5 are spring, 6 to 8 summer, and 9 to 11 autumn; the last choice
lists December, January, and February one by one. Because Month
has type Integer and only the values 1 to 12 have a season, the
when others branch is required and catches 13. Removing it makes
the compiler reject the statement with a message about missing case values,
which is Ada's way of asking what should happen to the values left over.
$ gnatmake -gnat2022 -gnatwa season.adb $ ./season 1: winter 2: winter 3: spring 4: spring 5: spring 6: summer 7: summer 8: summer 9: autumn 10: autumn 11: autumn 12: winter 13: not a month
case over an enumeration
A case statement is a natural fit for an enumeration, because the
type lists every possible value in advance. When all the literals have their
own branch, no when others is needed, and the compiler verifies
that the branches are exhaustive. Adding a literal to the type later makes the
old statement incomplete, and the compiler points at the missing value.
with Ada.Text_IO;
use Ada.Text_IO;
procedure Traffic is
type Light is (Red, Amber, Green);
Signal : Light := Red;
begin
for Round in 1 .. 3 loop
Signal := Light'Val (Round - 1);
Put ("round" & Integer'Image (Round) & ": ");
case Signal is
when Red =>
Put_Line ("stop");
when Amber =>
Put_Line ("wait");
when Green =>
Put_Line ("go");
end case;
end loop;
end Traffic;
Light'Val turns the round number into a literal, so the three
iterations visit red, amber, and green in turn. The output follows the colour
order rather than the alphabetical order of the names, because the literals
keep the order in which they were declared. Unlike languages with a fall-through
switch, exactly one branch runs and no break is
needed.
$ gnatmake -gnat2022 -gnatwa traffic.adb $ ./traffic round 1: stop round 2: wait round 3: go
if expressions
An if expression chooses a value rather than a statement: it is
written in parentheses, begins with if, and uses
then, elsif, and else as in a statement.
All branches must produce the same type, and the whole expression can stand
wherever a value of that type is allowed, including an argument to
Put_Line.
with Ada.Text_IO;
use Ada.Text_IO;
procedure Label is
Score : Integer := 0;
begin
for Round in 1 .. 3 loop
Score := 45 + Round * 20;
Put_Line ("score" & Integer'Image (Score) & ": " &
(if Score >= 90 then "high"
elsif Score >= 70 then "middle"
else "low"));
end loop;
end Label;
The expression returns a string in every branch, so it can be joined to the
rest of the line with &. The scores 65, 85, and 105 select
low, middle, and high respectively. A statement
version would need a variable and three assignments, while the expression
states the choice where its result is used. The parentheses around the
expression are part of its syntax and cannot be omitted.
$ gnatmake -gnat2022 -gnatwa label.adb $ ./label score 65: low score 85: middle score 105: high
case expressions
A case expression applies the same idea to a discrete selector.
It is written in parentheses, and each alternative is written
when choice => value. The expression ends with a closing
parenthesis rather than with end case. The choices may use ranges
and alternatives, and the expression must be exhaustive just like the
statement.
with Ada.Text_IO;
use Ada.Text_IO;
procedure Classify is
C : Character := 'x';
begin
for Round in 1 .. 3 loop
C := Character'Val (47 + Round * 8);
Put_Line (C & " is " &
(case C is
when '0' .. '9' => "a digit",
when 'a' .. 'z' | 'A' .. 'Z' => "a letter",
when others => "something else"));
end loop;
end Classify;
Character'Val produces the characters with codes 55, 63, and 71,
which are 7, ?, and G; they take the
digit, other, and letter branches. The selector is a single character, and each
branch yields a string, so the result fits directly into the concatenation.
The same exhaustiveness rule applies as in the statement form, which is why
the when others choice is present: a Character has far
more values than the three ranges mention.
$ gnatmake -gnat2022 -gnatwa classify.adb $ ./classify 7 is a digit ? is something else G is a letter
Next steps
The operators and expressions article explains
the comparisons and Boolean operators that form the conditions, and the
attributes article shows how 'Image
turns the results into text. The next article, loops, repeats the
statements that these conditionals choose.
Source
In this article we have chosen branches with if,
elsif, and else, nested one conditional inside
another, selected ranges and alternatives with case, covered an
enumeration exhaustively, and produced values with the if and
case expressions of Ada 2012. The
previous article supplies the conditions, and the
next article repeats statements with loops.
Author
List all Ada tutorials.