Ada Programming/Types/range

A range is a signed integer value which ranges from a First to a last Last. It is defined as

Ada. Time-tested, safe and secure.
Ada. Time-tested, safe and secure.
 range First .. Last

When a value is assigned to an object with such a range constraint, the value is checked for validity and Constraint_Error exception is raised when the value is not within First to Last.

When declaring a range type, the corresponding mathematical operators are implicitly declared by the language at the same place.

The compiler is free to choose a suitable underlaying hardware type for this user defined type.

Working example edit

The following example defines a new range from -5 to 10 and then prints the whole range out.

File: range_1.adb (view, plain text, download page, browse all)
with Ada.Text_IO;

procedure Range_1 is
   type Range_Type is range -5 .. 10;

   package T_IO renames Ada.Text_IO;
   package I_IO is new Ada.Text_IO.Integer_IO (Range_Type);

   for A in Range_Type loop
      I_IO.Put (
         Item  => A,
         Width => 3,
         Base  => 10);

      if A < Range_Type'Last then
         T_IO.Put (",");
      end if;
   end loop;
end Range_1;

See also edit

Wikibook edit

Ada Reference Manual edit