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

Dmitry Boyarintsev skalogryz.lists at gmail.com
Mon Oct 5 17:25:15 CEST 2015


Delphi 7 follows ISO rule.

The loop:
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');
Doesn't produce a warning.

The loop:
 n:=-1;
 for i := 1 to 100 do begin
    if (IntArray[i] > 100) and (n<0) then
      n := i;
  end;
  writeln('The '+IntToStr(i)+' item in the array > 100');

Does: [Warning] FOR-Loop variable 'i' may be undefined after loop

thanks,
Dmitry

On Mon, Oct 5, 2015 at 11:21 AM, Sven Barth <pascaldragon at googlemail.com>
wrote:

> Am 05.10.2015 15:43 schrieb "Adriaan van Os" <fpc at microbizz.nl>:
> >
> > Sven Barth wrote:
> >
> >> 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...).
> >
> >
> > ISO/IEC 7185 :1990(E) says in section 6.8 .3.9 For-statements
> >
> > After a for-statement is executed, other than being left by a
> goto-statement, the control-variable shall be undefined .
>
> Thanks, so this applies to ISO as well ^^
>
> Regards,
> Sven
>
> _______________________________________________
> fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org
> http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20151005/0e3bf728/attachment.html>


More information about the fpc-pascal mailing list