[fpc-pascal] Using for-loop index variable after loop

Juha Manninen juha.manninen62 at gmail.com
Mon Oct 10 12:16:13 CEST 2016

I have always avoided using a for-loop index variable after the loop.
I thought its value can be unpredictable in some situations when the
compiler reuses its register or memory etc.

I noticed the ActionListEditor in Lazarus sources has code that uses
it after the loop.
The compiler, FPC 3.0, does not warn about it.
I made a small test:

procedure MyProcedure;
  i: Integer;
  for i := 0 to 5 do
    WriteLn('iii ', i);
  Write('After loop i=', i);
  if i = 5 then
    WriteLn(' as expected')
    WriteLn(' which is unpredictable');

Indeed, no warning!
Is it OK to do that? Have I misunderstood it?


More information about the fpc-pascal mailing list