Skip to content
Tutorials Center
My learning

Topics

Lesson 7 of 36 ยท Types and Control Flow

Enumerations, Characters and Booleans in Ada

Learn Ada enumeration types and the attributes Pos, Val, Succ, Pred, Image and Value, how Boolean and Character are enumerations, and how to use them as loop and array indexes.

An enumeration type is a list of named values. Ada's enumerations are real types, not integers in disguise: you cannot add a Color to an Integer, but you can iterate over all colors, index arrays by them, and ask for the next or previous value. Boolean and Character are themselves enumeration types, so everything in this lesson applies to them too.

The Ada code is read-only on this site; C stand-ins model the behavior.

Declaring enumerations

ADA
type Color is (Red, Green, Blue);
type Day   is (Mon, Tue, Wed, Thu, Fri, Sat, Sun);
type Light is (Off, Dim, Bright);

C : Color := Green;

The names are called literals. Because literals are overloaded by type, Off could belong to several enumerations, and the compiler disambiguates by context.

Subtypes work as for numbers:

ADA
subtype Weekday is Day range Mon .. Fri;
subtype Weekend is Day range Sat .. Sun;

Ordering and attributes

Enumeration values are ordered by their position in the declaration, starting at 0. You can compare them with <, <=, = and so on, and use attributes to navigate:

ADA
Color'First         --  Red
Color'Last          --  Blue
Color'Succ (Red)    --  Green
Color'Pred (Blue)   --  Green
Color'Pos (Blue)    --  2
Color'Val (1)       --  Green
Color'Image (Blue)  --  "BLUE"
Color'Value ("RED") --  Red

'Succ of the last value raises Constraint_Error, so the end of the range is checked. The pair 'Pos and 'Val converts between a literal and its number when you really need to.

Loops, arrays and case

Enumerations are discrete types, so they work as loop ranges, array indexes and case selectors:

ADA
for D in Weekday loop
   Put_Line (Day'Image (D));
end loop;

Hours : array (Day) of Natural := (others => 0);
Hours (Mon) := 8;

A case statement over an enumeration must cover every literal (or use others). Add a new value to the type and the compiler points at every case that needs updating. That single feature prevents a large class of maintenance bugs.

ADA
case D is
   when Mon .. Fri => Put_Line ("Work");
   when Sat | Sun  => Put_Line ("Rest");
end case;

Boolean and Character are enumerations

ADA
type Boolean is (False, True);   --  conceptually

Because Boolean is an enumeration, Boolean'Pos (True) is 1, and Boolean'Image (X) gives "TRUE". It has the logical operators and, or, xor, not, and the short-circuit forms and then and or else. Conditions must be genuinely Boolean: if Count then is an error when Count is an integer; write if Count /= 0 then.

Character is an enumeration of 256 values (Latin-1), with the Wide_Character and Wide_Wide_Character types for Unicode. You can loop over it:

ADA
for Ch in Character range 'a' .. 'e' loop
   Put (Ch);              --  abcde
end loop;

Characters have 'Pos and 'Val too, so Character'Pos ('A') is 65.

Enumeration representation

Normally you never care how the compiler encodes the values. For hardware, you can force it:

ADA
type Mode is (Idle, Run, Fault);
for Mode use (Idle => 0, Run => 4, Fault => 15);

This representation clause maps each literal to a specific integer, which is useful when the enumeration mirrors a register. Mode'Pos still counts 0, 1, 2; the representation is separate.

Stand-in: enumerations in C

C enums are integers, so they lack the safety, but the stand-in below recreates 'Succ, 'Pred, 'Image and a checked end of range.

C Stand-in: Color'Succ, Color'Pred and Image

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.

The second stand-in models an array indexed by an enumeration and a case that handles every literal; using -Wswitch makes GCC warn when you forget one, which is how Ada always behaves.

C Stand-in: array indexed by enum and exhaustive switch

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

  • Treating an enumeration as an integer. Use 'Pos and 'Val when a number is truly needed.
  • Using others in every case and losing the compiler's help; list the literals when you can.
  • Calling 'Succ on the last literal.
  • Writing if Flag = True then; just write if Flag then.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is Day'Pos (Mon) if Mon is the first literal of Day?

    Position numbers start at zero.
  2. 2. What happens with Color'Succ (Blue) when Blue is the last literal?

    There is no successor of the last value, so the check fails.
  3. 3. Why is a case over an enumeration safer than a chain of if statements?

    Ada insists that a case statement cover all values, so adding a literal reveals every place that must change.
  4. 4. Which condition is valid if N : Integer?

    Conditions must be Boolean; integers are not implicitly converted.

Summary

  • Enumerations are ordered lists of named values and distinct types.
  • Attributes 'First, 'Last, 'Succ, 'Pred, 'Pos, 'Val, 'Image and 'Value navigate and convert.
  • They serve as loop ranges, array indexes and exhaustive case selectors.
  • Boolean and Character are enumeration types; conditions must be Boolean.

Next: Arrays and Strings