for loop's

Top  Previous  Next

What is translated > for loop's

 

Delphi for-loop parameters are evaluated only once, before the loop runs. This complicates a correct translation to C++ a little bit. The number of loops in the following example is determined by the variable n:

 

procedure test;

var

  I, n : Integer

begin

  n := 10;

  for I:=1 to n do 

  begin

    DoSomething;  

    n := 11;

  end;

end;

 

A straightforward translation of this code - as done with Delphi2Cpp up to version 1.5.0 - would be;

 

int I = 0, n = 0;

n = 10;

for ( I = 1; I <= n; I++)

{

  DoSomething();

  n = 11;

}

 

However, in C++ an additional loop would be executed, because n is changed in the loop and the number of loops is recalculated with this new value. Therefore a correct translation has to remember the original loop count like in the following code, which is produced by Delphi2Cpp from version 1.5.1 on:

 

int I = 0, n = 0;

n = 10;

for ( int stop = n, I = 1; I <= stop; I++)

{

  DoSomething();

  n = 11;

}

 

 



This page belongs to the Delphi2Cpp Documentation

Delphi2Cpp home  Content