If your look is large, then it may not be very efficient calling a function inside a for loop. Better to use David's method:


for i := 0 to 100 do begin
 if FirstToLast then begin
   x := i;
 end
 else begin
   x := 100 - i;
 end;
 // DO YOUR STUFF WITH X
end;

Now C++ has a solution for this problem - use #define or templates.

From: "Jeremy Coulter" <[EMAIL PROTECTED]>

thats a DAMN good idea ! I shall try that !
My other problem, that I didnt mention is that I ALSO want to go from
the Middle to the end or the Middle to the begining, but this idea will
cover that fine I think.

Jeremy

-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]
On Behalf Of Trevor Jones
Sent: Saturday, 20 March 2004 06:14
To: NZ Borland Developers Group - Delphi List
Subject: Re: [DUG] Multiple "FOR" loops


Jeremy, unless I am very badly mistaken, this looks like a job for a language feature which AFAIK only exists in Pascal: nested procedures/functions.

e.g.

procedure DoStuff(Direction : TUpDown; const Lower, Upper : integer)

  procedure ReallyDoStuff(x : integer);
    begin
      Do Things with element X to suit Jeremy
    end;
var
  x : integer;
begin
  if Direction  = Up then
    for x := Lower to Upper do
      ReallyDoStuf(x)
  else
    for x := Upper downto Lower do
      ReallyDoStuff(x);
end


----- Original Message ----- From: Jeremy Coulter <mailto:[EMAIL PROTECTED]> To: [EMAIL PROTECTED] Sent: Saturday, March 20, 2004 5:42 PM Subject: [DUG] Multiple "FOR" loops

Hi All.
I want to be able to specify the direction of a FOR loop.

i.e. if the type is FirstToLast then it will be for x:=0 to 100
i.e. if the type is LastToFirst then it will be for x:=100 downto 0

Basically I want to be able to go :-

CASE Direction of
    0: for x:=0 to 100
    1: for x:=100 to 0
END;

Begin
  //for loop stuff here
End;

But of course this is not valid.

I then thought I should use a WHILE LOOP  but the problem here is that
even if I define the start and stop positions, it doesnt really herlp,
i.e.

While iStartPos <= iStopPos do

IF iStop=100 and iStart=1 its fine, BUT if iStop is 1 and iStart=100
then it will not work.

Anyone got any bright ideas? I REALLY dont want to make 2 "FOR" loops.

Cheers,

Jeremy Coulter


------------------------------------------------------------------------ All email scanned with Nortons Antivirus 2003 ------------------------------------------------------------------------




_____





_______________________________________________ Delphi mailing list [EMAIL PROTECTED] http://ns3.123.co.nz/mailman/listinfo/delphi


_______________________________________________ Delphi mailing list [EMAIL PROTECTED] http://ns3.123.co.nz/mailman/listinfo/delphi

_________________________________________________________________
Listen to music online with the Xtra Broadband Channel http://xtra.co.nz/broadband


_______________________________________________
Delphi mailing list
[EMAIL PROTECTED]
http://ns3.123.co.nz/mailman/listinfo/delphi

Reply via email to