All pastes #928840 Raw Edit

Untitled

public text v1 · immutable
·published 2008-03-05 10:14 UTC
rendered paste body
In PHP, iterating an array with a for loop could be done like this:

$SomeArray = array("Var1","Var2","Var3");
$Counter = count(SomeArray);

for($i = 0; $i < $Counter; $i++){

    // Do something

}

Not using the $Counter variable is frowned upon, as

for($i = 0; $i < count($SomeArray); $i++){

    // Do something

}

would've executed the count() function as many times as there are elements in the array, generating quite a lot of overhead.

In Ada I've found myself doing this:

for i in 1..SomeArray'Length loop

    -- Do something

end loop;

Does Ada suffer from the same thing as PHP? Should I use a counter of sorts, like this

x : Natural := SomeArray'Length;
for i in 1..x loop

    -- Do something

end loop;

Or is this even better:

for i in SomeArray'Range loop

    -- Do something

end loop;