Range Over Built in Ada

Based on the provided input, here is the explained and translated code example in Ada from the specified code snippet.

Here we use `for .. of` loop to sum the numbers in an array. Arrays work like this too.

```ada
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;

procedure Range_Over_Built_In_Types is
begin
    declare
        type Int_Array is array (Positive range <>) of Integer;
        Nums : constant Int_Array := (2, 3, 4);
        Sum  : Integer := 0;
    begin
        for Num of Nums loop
            Sum := Sum + Num;
        end loop;
        Put("sum: ");
        Put(Sum);
        New_Line;
    end;
end Range_Over_Built_In_Types;

for .. of loop on arrays provides both the index and value for each entry. Above we didn’t need the index, so we ignored it with the plain loop. Sometimes we actually want the indexes though.

with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;

procedure Range_Over_Built_In_Types is
begin
    declare
        type Int_Array is array (Positive range <>) of Integer;
        Nums : constant Int_Array := (2, 3, 4);
        Sum  : Integer := 0;
    begin
        for I in Nums'Range loop
            if Nums(I) = 3 then
                Put("index: ");
                Put(I);
                New_Line;
            end if;
        end loop;
    end;
end Range_Over_Built_In_Types;

for .. of loop on maps iterates over key/value pairs.

with Ada.Text_IO; use Ada.Text_IO;

procedure Range_Over_Built_In_Types is
    type String_Map is array (Character) of String(1..10); -- Example map type
    Kvs : constant String_Map := (Character'Value('a') => "apple", Character'Value('b') => "banana");
begin
    for K in Kvs'Range loop
        Put(K);
        Put(" -> ");
        Put(Kvs(K));
        New_Line;
    end loop;
end Range_Over_Built_In_Types;

for .. of loop can also iterate over just the keys of a map.

with Ada.Text_IO; use Ada.Text_IO;

procedure Range_Over_Built_In_Types is
    type String_Map is array (Character) of String(1..10); -- Example map type
    Kvs : constant String_Map := (Character'Value('a') => "apple", Character'Value('b') => "banana");
begin
    for K in Kvs'Range loop
        Put("key: ");
        Put(K);
        New_Line;
    end loop;
end Range_Over_Built_In_Types;

for .. of loop on strings iterates over Unicode code points. The first value is the starting byte index of the character and the second is the character itself.

with Ada.Text_IO; use Ada.Text_IO;
with Ada.Characters.Latin_1; use Ada.Characters.Latin_1;

procedure Range_Over_Built_In_Types is
begin
    declare
        S : constant String := "go";
    begin
        for I in S'Range loop
            Put(Integer'Image(I));
            Put(' ');
            Put(S(I));
            New_Line;
        end loop;
    end;
end Range_Over_Built_In_Types;

To run the program, compile the code into an executable and then run it.

$ gnatmake range_over_built_in_types.adb
$ ./range_over_built_in_types
sum: 9
index: 2
a -> apple
b -> banana
key: a
key: b
 1 g
 2 o

Now that we can run and build basic Ada programs, let’s learn more about the language.