-- Demonstrates declaring variables with ranges
-- Why: Better demonstrates programmer intent
-- Why: errors are caught as early as possible (on assignment, not later use)
-- Why: Allows program properties to be proved (discuss later)
with ada.integer_text_io; use ada.integer_text_io;
with ada.float_text_io; use ada.float_text_io;
procedure ranges is
i : Integer range -50 .. 100; -- Other values not allowed
f : Float range 0.0 .. 100.0;
n : Natural range 1 .. Natural'last - 1;
p : Positive;
begin
get(i); -- Error if outside range -50 .. 100
get(f); -- Error if outside range
put(i);
put(f);
i := -100; -- Compiles but gives warning: constraint error at runtime
get(n); -- Runtime error possible?
p := n + 1; -- Runtime error possible?
end ranges;