[fpc-pascal] for loop variable value reliable after loop?
Sven Barth
pascaldragon at googlemail.com
Tue Oct 6 11:16:57 CEST 2015
Am 06.10.2015 08:48 schrieb "Santiago A." <svaa at ciberpiula.net>:
>
> El 05/10/2015 a las 11:48, Dennis escribió:
> >
> > 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?
>
> I remember having read somewhere that in Pascal i value is not
> guaranteed after loop, although I have never found a Pascal compiler
> where i<>n.
> Nevertheless, you should never rely on i value after loop. It is a bad
> practice.
As written in this thread already it's guaranteed if and only if the loop
had been left by break/goto.
Regards,
Sven
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20151006/925c82e8/attachment.html>
More information about the fpc-pascal
mailing list