[fpc-pascal]"for" loop questions

support at falconsoft.com support at falconsoft.com
Sun Dec 8 08:34:19 CET 2002


Hi.

I have a question regarding for loops.  If you execute the following code:

var
  L : TList;
  P : Pointer;
  I : Integer;
begin
  L := TList.Create;

  for I := 0 to L.Count - 1 do begin
    P := L[I];
  end;
end;

You get an exception telling you that the list index is out of bounds. 
L.Count returns "0", but the loop still executes (or at least, it tries
to) once, which is incorrect.  According to every language I've ever used,
if L.Count is 0 the first time, the loop should never execute at all.

Is this a bug, or am I missing something?  Of course the fix is to just
put an "if L.Count > 0" before the "for" loop, but that's not the point -
the point is I shouldn't have to.  :)

Thanks.

Tim







More information about the fpc-pascal mailing list