Loops/For with a specified step: Difference between revisions

m
Line 2:
Demonstrate a for loop where the step value is greater than one.
=={{header|Ada}}==
The FOR loop construct in Ada does not give the programmer the ability to directly modify the loop control variable during the execution of the loop. Instead, wea havevalid torange providemust aalways rangebe ofprovided evenbefore valuesentering to thea loop,. or multiply each value by two to get the same result.
Because exact adherence to the task is impossible, we have three versions to approximate a solution. Looper_1 goes through a range of values which are even. Looper_2 multiples each value by two. Looper_3 most closely adheres to the requirements of this task, and achieves this by using a second range for the indices.
 
<lang ada>with Loopers;
Line 12 ⟶ 13:
Looper_1;
Looper_2;
Looper_3;
end For_Main;
 
Line 18 ⟶ 20:
procedure Looper_1;
procedure Looper_2;
procedure Looper_3;
end Loopers;
 
Line 49 ⟶ 52:
end loop;
end Looper_2;
 
procedure Looper_3 is
Values : array(1..10) of Integer := (1,2,3,4,5,6,7,8,9,10);
Indices : array(1..5) of Integer := (2,4,6,8,10);
begin
for I in Indices'Range loop
Put(Values(Indices(I)),0);
if I = Indices'Last then
Put_Line(".");
else
Put(",");
end if;
end loop;
end Looper_3;
 
end Loopers;
 
Anonymous user