Outside a for loop the loop variable is undefined, and any value it has is implementation-dependent (as you found).
You can always make an explicit assignment:
program Project1; {$mode delphi} uses SysUtils; var i, KylixEmulation: Integer; begin KylixEmulation := 1; for i := KylixEmulation to 6 do begin Inc(KylixEmulation); end; WriteLn('i: ',i,' KylixEmulation: ',KylixEmulation); ReadLn; end.
However a regular "for" loop counter behaves differently in Kylix than in Lazarus.
After the for statement terminates (provided this was not forced by a Break or an Exit procedure), the value of counter is undefined.
Kylix shows a value of 7 whereas FPC shows a value of 6 - at the end of the loop!Tell that the Kylix-people. It’s a “severe bug” in Kylix. A glance at ISO 7185 (http://www.pascal-central.com/iso7185.html#6.8.3.9%20For-statements) tells us
Noteshall be equivalent to
for v := e1 to e2 do body
begin temp1 := e1; temp2 := e2; if temp1 <= temp2 then begin v := temp1; body; while v <> temp2 do begin v := succ(v); body end end end
After a for-statement is executed, other than being left by a goto-statement, the control-variable shall be undefined.You (as a programmer) aren’t supposed to presume your control-variable has any particular value. In other words, you shouldn’t be “outraged” that FPC terminates with 6 and Kylix with 7.
QuoteAfter a for-statement is executed, other than being left by a goto-statement, the control-variable shall be undefined.