Thursday, November 22, 2018

Ada Loop Tutorial


The Ada programming language provides four different looping constructs; the “for” loop, the “while” loop, the simple loop and the container iterator loop.

The For loop

The Ada “for” loop iterates over a range of discrete values. Ada discrete values include signed integer values, modular (unsigned) integer values, and enumeration values. Ada ranges are always specified as lowest_value..highest value. Every “for” loop has a loop variable which is NOT declared before it is used. The loop variable has the type of the loop range. The loop variable cannot be altered within the body of the loop.

Ranges are always specified as lowest_value..highest_value, even if one wants to iterate through the range in reverse order. Any range where the first value is greater than the last value is considered an empty range. To iterate through a range in reverse order simply add the “reverse” reserved word

For Num in reverse 1..10 loop

Example For loop

with Ada.Text_Io; use Ada.Text_IO;
with Ada.Calendar; use Ada.Calendar;

procedure For_Loop is
   C     : Positive;
   start : time;
   Stop  : Time;
begin
   Start := Clock;
   for I in 1..10**9 loop
      C := I;
   end loop;
   Stop := Clock;
   Put_Line("counted " & C'Image & " numbers in " & Duration'Image(Stop - Start)
            & " seconds");
end For_Loop;

The loop variable in this example is named “I”. The first iteration of the loop variable I contains the value 1. The second iteration I contains 2. The final iteration of the loop I contains 10**9 (1000000000).

The output of the program is

counted  1000000000 numbers in  0.577727576 seconds

The While loop

The Ada while loop iterates while the condition specified at the top of the loop is true. The condition is always evaluated before the start of an iteration. The condition must evaluate to a Boolean value (False or True). Ada Boolean values are an enumeration type, not a numeric type. Ada does NOT provide any implicit conversion between zero and False or between not-zero and True as is done in the C language.

Example While loop

with Ada.Text_IO; use Ada.Text_IO;
with Ada.Calendar; use Ada.Calendar;

procedure While_Loop is
   Start : Time;
   Stop  : Time;
   C     : Positive := 1;
begin
   Start := Clock;
   while C < 10**9 loop
      C := C + 1;
   end loop;
   Stop := Clock;
   Put_Line("counted " & C'Image & " numbers in " & Duration'Image(Stop - Start)
              & " seconds");
end While_Loop;

Unlike C, C++ or Java, the Boolean expression in Ada does not need to be enclosed in parentheses “()”.

The output of the program is

counted  1000000000 numbers in  0.559296318 seconds

The Simple loop

The Ada simple loop continues to iterate until an exit is executed within the body of the loop. If the body of the loop does not contain an exit command the loop behaves as an infinite loop. The exit command is typically found within a conditional expression such as

if C = 10**9 then
   exit;
end if;

The expression above can be abbreviated to

exit when C = 10**9;

Example Simple loop

with Ada.Text_IO; use Ada.Text_IO;
with Ada.Calendar; use Ada.Calendar;

procedure Simple_Loop is
   Start : Time;
   Stop  : Time;
   C     : Positive := 1;
begin
   Start := Clock;
   loop
      C := C + 1;
      exit when C = 10**9;
   end loop;
   Stop := Clock;
   Put_Line("counted " & C'Image & " numbers in " & Duration'Image(Stop - Start)
              & " seconds");
end Simple_Loop;

The output of the program is

counted  1000000000 numbers in  0.576855340 seconds

The Iterator loop

The iterator loop is used to iterate through containers, and may be used with arrays. The iterator loop traverses the values of the container object or array object allowing reading or manipulation of each value in sequence.

The iterator loop strongly resembles the “for” loop with some subtle differences. The “loop variable” is actually a member of the container or array. No range is specified. The iteration proceeds through each data element in the container or array.

Example Iterator loop

The following example dynamically allocates an array of 10**9 elements. Dynamic memory allocation is used because such an array is too large to fit on the program stack.

with Ada.Text_Io; use Ada.Text_IO;
with Ada.Calendar; use Ada.Calendar;

procedure Iterator_Loop is
   type Nums_Array is Array(1..10**9) of Integer;
   type Nums_Access is access Nums_Array;
   Nums : Nums_Access := new Nums_Array;
   Start, Stop : Time;
   Counter : Integer := 1;
begin
   Start := Clock;
   for Value of Nums.all loop
      Value := Counter;
      Counter := Counter + 1;
   end loop;
   Stop := Clock;
   Put_Line("counted " & Nums(Nums'Last)'Image & " numbers in " &
            Duration'Image(Stop - Start) & " seconds");
end Iterator_Loop;

The variable Nums is defined to be an access type which references an instance of Nums_Array. The syntax Nums.all evaluates to the array referenced by Nums.

The output of the program is

counted  1000000000 numbers in  3.208108244 seconds

As you can see, iterator loops are much slower than the other loop constructs.

No comments:

Post a Comment

Comparison of three algorithms for summing an array of integers

This article shares a comparison of the time taken to sum an array of integers. This article tests three algorithms for summing the array. •...