-- Demonstrate named loops.
--
-- Find the value of start such that the sum of 50 or fewer numbers
-- starting at start is more than 1_000.
with ada.text_io; use ada.text_io;
procedure loops_named is
i, count, start, sum: Natural;
or_fewer: constant := 50;
max_value: constant := 1_000;
begin
start := 0;
outer: -- Give loop a name
loop
start := start + 1;
sum := 0;
inner: -- Give loop a name
for i in start .. start + or_fewer - 1 loop
sum := sum + i;
exit outer when sum >= max_value; -- Specify which to exit
end loop inner; -- Indicate which loop is ending
end loop outer;
put_line(start'img & sum'img);
-- Check results by printing partial sums and the count
count := 0;
sum := 0;
i := start;
check_loop: while sum < max_value loop
count := count + 1;
sum := sum + i;
put(sum'img);
i := i + 1;
end loop check_loop;
new_line;
put_line(count'img);
end loops_named;