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

Sven Barth pascaldragon at googlemail.com
Mon Oct 5 14:19:54 CEST 2015


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...).

Regards,
Sven
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20151005/4bb2e2f6/attachment.html>


More information about the fpc-pascal mailing list