[fpc-pascal] for loop variable value reliable after loop?

Michael Van Canneyt michael at freepascal.org
Mon Oct 5 15:07:49 CEST 2015

On Mon, 5 Oct 2015, Sven Barth wrote:

> Am 05.10.2015 11:48 schrieb "Dennis" <dec12 at avidsoft.com.hk>:
>> var i , n : integer;
>> for i := 1 to 100 do begin
>>    if IntArray[i] > 100 then  begin
>>     n := i;
>>     break;
>>    end;
>> end;
>> writeln('The '+IntToStr(i)+' item in the array > 100');
>> //can we be always sure the value i is always the same as n?
>> Even when the compiler optimizes it and uses a cpu register for i instead
> of a memory location?
> The for-loop-variable contains a valid value after the loop if the loop was
> left using a break or goto (this does not seem to be documented in FPC's
> language reference guide, but AFAIK it is in Delphi; also don't know what
> the ISO standard says about this...).

I am not sure that this is intentional. 
In fact I seem to remember mails from Jonas to the contrary, but he would need to confirm/deny that. 
As far as I know, the for loop variable is supposed to be undefined after the loop.


More information about the fpc-pascal mailing list