for loop variable value reliable after loop?

classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|

for loop variable value reliable after loop?

Dennis

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?

Dennis
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: for loop variable value reliable after loop?

Santiago A.
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.

A little off topic, maybe you should think of using a while.

n:=1;
While (n<=100) and (IntArray[n] <= 100)
     do inc(n);

When the final round count is not the result of a complex computation,
(like this case, 100),  it is as fast as for loop and, IMHO, clearer.


--
Regards

Santiago A.

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: for loop variable value reliable after loop?

Sven Barth-2
In reply to this post by Dennis

Am 05.10.2015 11:48 schrieb "Dennis" <[hidden email]>:
>
>
> 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


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: for loop variable value reliable after loop?

Michael Van Canneyt


On Mon, 5 Oct 2015, Sven Barth wrote:

> Am 05.10.2015 11:48 schrieb "Dennis" <[hidden email]>:
>>
>>
>> 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.

Michael.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: for loop variable value reliable after loop?

Jonas Maebe-2

Michael Van Canneyt wrote on Mon, 05 Oct 2015:

> On Mon, 5 Oct 2015, 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...).
>
> 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.

Yes, except if you use break/goto. Sven is correct and it's indeed the  
same in TP and Delphi.


Jonas
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: for loop variable value reliable after loop?

Michael Van Canneyt


On Mon, 5 Oct 2015, Jonas Maebe wrote:

>
> Michael Van Canneyt wrote on Mon, 05 Oct 2015:
>
>> On Mon, 5 Oct 2015, 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...).
>>
>> 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.
>
> Yes, except if you use break/goto. Sven is correct and it's indeed the same
> in TP and Delphi.

In that case I will add this to the docs.

Michael.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: for loop variable value reliable after loop?

Adriaan van Os-2
In reply to this post by Sven Barth-2
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 .

Regards,

Adriaan van Os
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: for loop variable value reliable after loop?

Dennis


Adriaan van Os wrote:

> 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 .
>
> Regards,
>
Thanks so much for clarification.

Dennis
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: for loop variable value reliable after loop?

Sven Barth-2
In reply to this post by Adriaan van Os-2

Am 05.10.2015 15:43 schrieb "Adriaan van Os" <[hidden email]>:
>
> 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  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: for loop variable value reliable after loop?

dmitry boyarintsev
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 <[hidden email]> wrote:

Am 05.10.2015 15:43 schrieb "Adriaan van Os" <[hidden email]>:
>
> 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  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: for loop variable value reliable after loop?

Sven Barth-2
In reply to this post by Santiago A.

Am 06.10.2015 08:48 schrieb "Santiago A." <[hidden email]>:
>
> 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


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal