Loops: loop, while, for, exit & Named Loops

Use the basic loop with exit/exit when, while loops, for loops over ranges and types, reverse iteration, for ... of loops over array and container elements, named loops for exiting nested loops, and understand the loop parameter's constant, scoped nature.

Ada has one loop construct with three headers: none (an infinite loop you exit explicitly), while Condition, and for Parameter in Range. Combined with exit statements and loop names, these cover every iteration pattern while keeping the loop's intent obvious. This lesson walks through each form and the rules that make Ada loops safe.

Basic, while and exit

'loop ... end loop;' repeats forever until an exit statement leaves it. 'exit when Condition;' is the idiomatic way to leave a loop, and it can appear anywhere in the body — so a 'loop-and-a-half' (read, test, process) needs no duplicated code. 'while Condition loop ... end loop;' tests before each iteration. There is no 'continue' statement; use an if or (Ada 2022) restructure the body; GNAT also supports goto <<Continue>> labels if absolutely needed.

• loop ... end loop; — exit with exit / exit when. • while C loop ... end loop; — tested before each iteration. • exit when can appear anywhere in the body. • No continue statement in Ada.

for Loops

'for I in 1 .. 10 loop' declares the loop parameter I implicitly; it is a constant inside the body (you cannot assign to it) and does not exist after the loop. The range is evaluated once, before the loop starts. 'reverse' iterates downward: for I in reverse 1 .. 10. You can iterate over a whole discrete type (for D in Day loop) or an array's index range (for I in A'Range loop). An empty range such as 5 .. 1 simply executes zero times — GNAT even warns 'loop range is null'.

'for Element of Container loop' (Ada 2012) iterates over the components themselves rather than indices; for a variable array or container, Element can be modified to update the component in place. There is no 'step' clause: to iterate by 2, loop over a range and compute 2 * I, or use a while loop.

• for I in L .. H loop — I is constant, local to the loop, range evaluated once. • reverse iterates backwards; empty ranges run zero times. • for X of Array_Or_Container loop — iterate over elements (can update in place). • No step clause — compute from the index instead.

Named Loops

A loop can be named: 'Search : for Row in ... loop ... end loop Search;'. 'exit Search;' then leaves the named loop even from inside nested loops — the clean replacement for flags or goto when searching a two-dimensional structure. Names also document intent in long loops.

• Name : loop ... end loop Name; • exit Name; or exit Name when C; leaves the named (outer) loop directly.

Code Examples

All loop forms in one program: exit when, while, for, reverse, for... of over an array written with an Ada 2022 [ ] aggregate, a named loop exited from the inner loop, and an empty range. Output: First N with N*N > 20: 5 Total after while: 12 for 1.. 5: 1 2 3 4 5 reverse 1.. 5: 5 4 3 2 1 for P of Primes: 2 3 5 7 11 Found 42 at 6 x 7 Done for I in 1.. 10 loop I:= I + 1; -- skip every other end loop;

for I in 1 .. 5 loop Process (2 * I); end loop;

The loop parameter is a constant; compute the value you need from it instead.

for I in 10.. 1 loop -- expecting a countdown for I in reverse 1.. 10 loop

10 .. 1 is an empty range; use reverse with an ascending range.

Found := False; for R ... loop for C ... loop if Match then Found := True; exit; end if; end loop; exit when Found; end loop;

Search : for R ... loop for C ... loop exit Search when Match; end loop; end loop Search;

Named loops remove the need for flag variables.

Can you assign to a for loop parameter inside the loop? A. Yes B. No, it is a constant C. Only with reverse D. Only in while loops

B. No — the loop parameter is a constant.

How many times does 'for I in 5 .. 1 loop' execute? A. 5 B. 4 C. 0 D. Infinite

C. 0 — it is a null range (use reverse 1 .. 5 to count down).

How do you leave an outer loop from an inner loop? A. break 2; B. exit Outer_Name; C. return; D. goto end;

B. Name the outer loop and use exit Name;

What does 'for X of A loop' iterate over? A. Indices of A B. The elements of A C. Addresses D. Only strings B. The elements themselves.

Trace the output of the loops example.

Real output: First N with N*N > 20: 5 Total after while: 12 for 1 .. 5 : 1 2 3 4 5 reverse 1 .. 5 : 5 4 3 2 1 for P of Primes : 2 3 5 7 11 Found 42 at 6 x 7 Done

Write a loop that prints the multiples of 3 from 3 to 30 using a for loop (no step clause).

for I in 1 .. 10 loop Put (Integer'Image (3 * I)); end loop; New_Line;

Ada has no step clause, so iterate over 1 .. 10 and compute 3 * I.

with Ada.Text_IO; use Ada.Text_IO;

procedure L12_Loops is
   N     : Integer := 0;
   Total : Integer := 0;
   Primes : constant array (1 .. 5) of Integer := [2, 3, 5, 7, 11];
begin
   --  1. Basic loop with exit when
   loop
      N := N + 1;
      exit when N * N > 20;
   end loop;
   Put_Line ("First N with N*N > 20:" & Integer'Image (N));

   --  2. while loop
   while Total < 10 loop
      Total := Total + 3;
   end loop;
   Put_Line ("Total after while:" & Integer'Image (Total));

   --  3. for loop and reverse
   Put ("for 1 .. 5      :");
   for I in 1 .. 5 loop
      Put (Integer'Image (I));
   end loop;
   New_Line;
   Put ("reverse 1 .. 5  :");
   for I in reverse 1 .. 5 loop
      Put (Integer'Image (I));
   end loop;
   New_Line;

   --  4. for ... of iterates over the elements themselves
   Put ("for P of Primes :");
   for P of Primes loop
      Put (Integer'Image (P));
   end loop;
   New_Line;

   --  5. named loops: exit the outer loop from the inner one
   Search :
   for Row in 1 .. 9 loop
      for Col in 1 .. 9 loop
         if Row * Col = 42 then
            Put_Line ("Found 42 at" & Integer'Image (Row) & " x" & Integer'Image (Col));
            exit Search;
…