Page 1 of 1

[For] A 'For to' which reacts like a 'for downto'

PostPosted: June 15th, 2004, 8:27 am
by Dave_lopez
Hi everybody,

I got a this extract of code which uses a for:

Code: Select all
For Cpt:=8 to 128 do
  D[CPT] := S.D[CPT];


What's happening ?
Answer:

D[121] := S.D[121];
D[120] := S.D[120];
D[119] := S.D[119];
...
D[2] := S.D[2];
D[1] := S.D[1];

It is driving me crazy ...

Thanx in advance ...

PostPosted: June 15th, 2004, 9:17 am
by Kambiz
The downward for loop is faster than the upward one. Because of that, when direction of the for loop variable doesn't affect the result, the compiler optimizes the code and generates the downward loop.