Ada loops
last modified November 23, 2026
A loop repeats a sequence of statements until a condition ends it. Ada offers
the unbounded loop with an explicit exit, the
while loop that tests before each iteration, and the
for loop that visits every value of a discrete range. The
for loop also has a reverse form and an element form that walks a
string without an index.
loop and exit when
A plain loop repeats its body without any condition of its own, so
the body must contain an exit statement or the program never
leaves the loop. The form exit when tests a condition at the point
where it stands, which means the statements before it have already run.
with Ada.Text_IO;
use Ada.Text_IO;
procedure Countdown is
N : Integer := 5;
begin
loop
Put_Line (Integer'Image (N));
N := N - 1;
exit when N = 0;
end loop;
Put_Line ("lift off");
end Countdown;
The value is printed first and decremented afterwards, so the sequence begins
at 5 and ends at 1. When N reaches zero the exit
leaves the loop, and the statement after end loop runs. Replacing
exit when N = 0 with a bare exit would leave the loop
on its first iteration, and removing the exit entirely would make the loop
endless.
$ gnatmake -gnat2022 -gnatwa countdown.adb $ ./countdown 5 4 3 2 1 lift off
while loops
A while loop tests its condition before each iteration, so the
body runs only while the condition is true. If the condition is false at the
start, the body runs zero times. The condition must be a
Boolean expression, and it is checked again after every iteration,
which makes the loop suitable when the number of repetitions is not known in
advance.
with Ada.Text_IO;
use Ada.Text_IO;
procedure Halving is
N : Integer := 100;
begin
while N > 1 loop
Put_Line (Integer'Image (N));
N := N / 2;
end loop;
Put_Line ("last =" & Integer'Image (N));
end Halving;
One hundred becomes fifty, then twenty-five; the next division truncates 12.5
to 12, and the sequence continues through 6 and 3 to 1, where the condition
fails. The final value is printed after the loop to show that the loop stopped
without changing it further. A step that never lands exactly on the bound
would be a problem for a condition such as N /= 1, which could run
past the value forever; testing N > 1 instead ends the loop as
soon as the value falls to one or below.
$ gnatmake -gnat2022 -gnatwa halving.adb $ ./halving 100 50 25 12 6 3 last = 1
for loops over a range
A for loop names a parameter and a range, and repeats its body
once for each value from the lower bound to the upper bound. The bounds are
evaluated once, before the first iteration, and the parameter takes each value
in turn. Adding reverse before the range visits the same values
from the upper bound down to the lower one.
with Ada.Text_IO;
use Ada.Text_IO;
procedure Ranges is
Base : constant Integer := 7;
begin
for I in 1 .. 5 loop
Put_Line (Integer'Image (Base) & " x " & Integer'Image (I) &
" = " & Integer'Image (Base * I));
end loop;
Put_Line ("countdown");
for I in reverse 1 .. 3 loop
Put_Line (Integer'Image (I));
end loop;
end Ranges;
The first loop prints the multiples of seven from one to five, and the second
prints three, two, one. The same name I is used twice because each
parameter is local to its own loop. The upper bound of the first loop could be
a variable or an attribute such as a string's 'Last, and the loop
would still evaluate it once; changing the variable inside the body would not extend
or shorten the loop.
$ gnatmake -gnat2022 -gnatwa ranges.adb $ ./ranges 7 x 1 = 7 7 x 2 = 14 7 x 3 = 21 7 x 4 = 28 7 x 5 = 35 countdown 3 2 1
The loop parameter is a constant
The parameter of a for loop is not a variable: it is a constant
that the loop assigns on each iteration, and the body may read it but not
change it. This rule keeps the number of iterations predictable and lets the
compiler check the loop independently of what the body does. GNAT rejects an
assignment to the parameter before the program is built.
with Ada.Text_IO;
use Ada.Text_IO;
procedure Fixed_Parameter is
begin
for I in 1 .. 3 loop
I := I + 1;
Put_Line (Integer'Image (I));
end loop;
end Fixed_Parameter;
The error points at the assignment and names the reason directly. A program
that needs a variable counter inside a loop declares one before the loop and
updates it in the body, or uses a while loop with its own
variable. The loop parameter itself disappears after end loop, so
its name is free for another declaration in the same declarative part.
$ gnatmake -gnat2022 -gnatwa fixed_parameter.adb x86_64-linux-gnu-gcc-14 -c -gnat2022 -gnatwa fixed_parameter.adb fixed_parameter.adb:7:07: error: assignment to loop parameter not allowed gnatmake: "fixed_parameter.adb" compilation error
Named loops
A loop may carry a name written before loop, and the same name
follows end loop. The name becomes useful when loops are nested:
exit Outer; leaves the named loop and every loop inside it, while a
bare exit leaves only the innermost one. Names also make the end
of a long loop easier to find.
with Ada.Text_IO;
use Ada.Text_IO;
procedure Grid is
begin
Outer:
for Row in 1 .. 3 loop
for Col in 1 .. 3 loop
if Row * Col = 4 then
Put_Line ("found at" & Integer'Image (Row) &
"," & Integer'Image (Col));
exit Outer;
end if;
end loop;
end loop Outer;
Put_Line ("search finished");
end Grid;
The product of the two indices first equals four at row 2, column 2, and the
named exit leaves both loops at once. Without the name, the exit
would leave only the inner loop, and the outer loop would continue with row 3.
The label after end loop must match the one before the loop; GNAT
reports a mismatch as an error rather than treating it as a comment.
$ gnatmake -gnat2022 -gnatwa grid.adb $ ./grid found at 2, 2 search finished
for ... of over a string
The form for C of Word loop visits the elements of an array
instead of the indices of a range. For a string the elements are characters,
so the loop reads each character without an index and without knowing the
bounds. The element is a constant, just like a loop parameter, and the loop
ends after the last character.
with Ada.Text_IO;
use Ada.Text_IO;
procedure Vowels is
Word : constant String := "Ada 2022";
Count : Integer := 0;
begin
for C of Word loop
if C in 'a' .. 'z' | 'A' .. 'Z' then
Count := Count + 1;
end if;
end loop;
Put_Line ("letters =" & Integer'Image (Count));
end Vowels;
The string contains eight characters, of which the three letters
'A', 'd', and 'a' satisfy the
membership test; the space and the digits do not. The condition joins two ranges with
|, so one test covers both cases of the alphabet. The loop
variable Count is a real variable declared before the loop,
because a loop parameter or element cannot be modified.
$ gnatmake -gnat2022 -gnatwa vowels.adb $ ./vowels letters = 3
Next steps
The conditionals article explains the tests that stop these loops, and scalar types and subtypes defines the ranges they walk through. The next article, procedures and functions, moves repeated statements out of the main program and gives them names and parameters.
Source
In this article we have repeated statements with a plain loop and
exit when, tested a condition before each iteration with
while, walked a range forwards and with reverse,
seen that a loop parameter cannot be assigned, left two nested loops through a
name, and visited the characters of a string with for ... of. The
previous article supplies the conditions, and
the next article puts repeated work into subprograms.
Author
List all Ada tutorials.