[DUG] for..loop

Ross Levis ross at stationplaylist.com
Tue Aug 7 19:56:28 NZST 2007


I had the same concern when I heard that.  I do have several For loops that
rely on lowest to highest order.  I even have some that rely on the reverse
order and I use downto.  I presume the compiler is smart and only does this
when it determines nothing can be affected by the order.

Ross.

-----Original Message-----
From: delphi-bounces at delphi.org.nz [mailto:delphi-bounces at delphi.org.nz] On
Behalf Of John Bird
Sent: Tuesday, 7 August 2007 19:16
To: 'NZ Borland Developers Group - Delphi List'
Subject: RE: [DUG] for..loop

That sort of surprises me - surely counting down instead of up would spoil
the expected logic of a loop in some cases????

Eg a trivial example, getting the first word from a string...

Function getfirstword(aline:string):integer;
Var
i:integer;
For i:=1 to length(aline) do
Begin
   if(aline[i]=' ') then
   begin
     result:=copy(aline,1,i-1);
     exit;
   end;
End;

I would indeed be rather upset if I got either the last word back, or the
whole of the line except the last word.
This is a made up example, but I am sure a lot of "for" loops might be
looking for the first instance of something...are you saying we cannot rely
on the order the loop runs???

John

> p.s. has anyone else noticed that sometimes when you step through a 
> for loop the counter actually counts down (in the debugger) (even if 
> it set to count up).  This was the case sometimes in D6 if I recall 
> correctly.
>

It's an optimisation thing.  Counting down to 0 is more efficient than
counting up.

Sean



More information about the Delphi mailing list