re: Which types of loops are most popular in the programming languages you use? VIEW POST


Ada is based on old-style grammar, so basically we have the usual for loop (but only with +1 or -1 increments, a-la Pascal), while loop and just "loop" for never ending loops (you exit with an exit in the middle).

For loop allows for a very convenient construction. If V is an array you can write

for Idx in V'Range loop 
   V(Idx) := V(Idx)+1;
end loop; 

to run over the index range of V. No risk of buffer overflow or off-by-one errors.

However, recently (Ada 2005 or 2012) the syntax of for has been extended in something that resembles the each loop in Ruby (compatibly with old syntax, of course). If Container is any kind of container (standard or defined by you) you can write

for Item of Container loop
  Item :=  Item + 1;  -- Here Item is an element stored in Container
end loop;

Very convenient. Not much different from the Ruby-sque

container.each do |item|
  item = item+1;

The same syntax can be used even if Container is a simple array.

You also have an extension of "old school loop" for a container that is not an array

for Idx in Container.Iterate loop
  Container(Idx) := Container(Idx)+1;
end loop;
code of conduct - report abuse