Loops/For with a specified step: Difference between revisions

Content added Content deleted
Line 2: Line 2:
Demonstrate a for loop where the step value is greater than one.
Demonstrate a for loop where the step value is greater than one.
=={{header|Ada}}==
=={{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, we have to provide a range of even values to the loop, or multiply each value by two to get the same result.
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, a valid range must always be provided before entering a loop.
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;
<lang ada>with Loopers;
Line 12: Line 13:
Looper_1;
Looper_1;
Looper_2;
Looper_2;
Looper_3;
end For_Main;
end For_Main;


Line 18: Line 20:
procedure Looper_1;
procedure Looper_1;
procedure Looper_2;
procedure Looper_2;
procedure Looper_3;
end Loopers;
end Loopers;


Line 49: Line 52:
end loop;
end loop;
end Looper_2;
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;
end Loopers;