0

Wha's equivalent in Delphi of this c++ sintaxe?

See that i variable is incremented before.

for(int i = 0; i < 20; ++i)

Thanks in advance.

EDIT:

In my case, this is how ++i is used with sintaxe above:

void testStruct *testMethod()
{
   for(int i = 0; i < 20; ++i)
   {
      if(values[i].id == (DWORD) 10)
          return &values[i];
   }

   return NULL;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
FLASHCODER
  • 1
  • 7
  • 24

1 Answers1

5
for(int i = 0; i < 20; ++i)

The pre-increment vs post-increment is not relevant here. Semantically this is just the same as

for(int i = 0; i < 20; i++)

In Delphi this would be

for i := 0 to 19 do

Pre-increment matters in expressions where the value of the variable is used. For instance

x[++i] = 42;

differs from

x[i++] = 42;

because the value of ++i differs from the value of i++. However in your C++ for loop, the pre-increment operator is used solely to increment the loop variable, and the value of the expression is not used.

Just to make this clear, if i is 0 then ++i == 1 and i++ == 0. But in the C++ loop, the value of the increment expression is not used, and the only point of it is the side effect of the increment.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • Then based in your answer, **++i** is the same that **i++** on C++, right? Are only two options that will produce the same finalresult. – FLASHCODER Apr 19 '19 at 22:28
  • 1
    No. They are not the same in general. But they are the same in this context. – David Heffernan Apr 19 '19 at 22:30
  • @BrowJr: In the for loop construct, it doesn't matter whether you use `i++` or `++i`, because the *result* of the expression is not used, only the (side-)effect that `i` is incremented. In other contexts, where the *result of the expression* matters, like the one David added, it can make a difference. – Rudy Velthuis Apr 19 '19 at 23:05
  • 1
    And for those Delphi doesn't really have equivalents, if only to prevent confusion. There is the `Inc` procedure, but it doesn't return a value, so it cannot be used as an expression. It would be easy to write PreInc and PostInc functions that do, but, well, meh... – GolezTrol Apr 19 '19 at 23:38