Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2015-10

for loop variable value reliable after loop?

11 messages · 7 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

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

Reply by email

Santiago A.
to Dennis
Dennis wrote 14 lines
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 in.
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.

Reply by email

Sven Barth
to Dennis: var i , n : integer; for i := 1 to 100 do begin if IntArray[i] > 100 then begin n := i;…
"Dennis" wrote 14 lines
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

Reply by email

Michael Van Canneyt
to Sven Barth
Sven Barth wrote 22 lines
Am 05.10.2015 11:48 schrieb "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? > 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.

Reply by email

Jonas Maebe
to Michael Van Canneyt

Michael Van Canneyt wrote on Mon, 05 Oct 2015:

Show quoted text 11 lines
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

Reply by email

Michael Van Canneyt
to Jonas Maebe
Jonas Maebe wrote 18 lines
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.

Reply by email

Adriaan van Os
to Sven Barth: of a memory location? The for-loop-variable contains a valid value after the loop if the…
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

Reply by email

Dennis
to Adriaan van Os
Adriaan van Os wrote 14 lines
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

Reply by email

Sven Barth
to Adriaan van Os: ISO/IEC 7185 :1990(E) says in section 6.8 .3.9 For-statements After a for-statement is…
"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...).

Show quoted text 5 lines
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

Reply by email

Dmitry Boyarintsev
to Sven Barth

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

Sven Barth wrote 24 lines
Am 05.10.2015 15:43 schrieb "Adriaan van Os" : > > 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 - http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Reply by email

Sven Barth
to Santiago A.: I remember having read somewhere that in Pascal i value is not guaranteed after loop,…
"Santiago A." wrote 22 lines
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 in. 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

Reply by email