Value of runner variable after a for loop

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

Value of runner variable after a for loop

Luiz Americo Pereira Camara
Take the following example:

  for i:= 0 to 2 do;
  WriteLn(i);

Can i safely assume that after the for loop the value of "i" is 2?

Thanks in advance

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

Re: Value of runner variable after a for loop

michael.vancanneyt


On Mon, 12 Mar 2007, Luiz Americo Pereira Camara wrote:

> Take the following example:
>
>  for i:= 0 to 2 do;
>  WriteLn(i);
>
> Can i safely assume that after the for loop the value of "i" is 2?

No. I think it is even specified in the pascal standard.

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

Re: Value of runner variable after a for loop

etrusco
On 3/12/07, Michael Van Canneyt <[hidden email]> wrote:

>
> On Mon, 12 Mar 2007, Luiz Americo Pereira Camara wrote:
>
> > Take the following example:
> >
> >  for i:= 0 to 2 do;
> >  WriteLn(i);
> >
> > Can i safely assume that after the for loop the value of "i" is 2?
>
> No. I think it is even specified in the pascal standard.
>

And unless the compiler (programmer) use an extra variable just to
attain this behaviour, the value after the loop will 3 (i.e. end value
+1).

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

RE: Value of runner variable after a for loop

Andrew Denton
I would NEVER rely on this in production code. The value of the control
variable of a For loop after execution should never be relied upon. A simple
change in the compiler at some future point could break a lot of your code.

Cheers,

Andy

-----Original Message-----
From: [hidden email]
[mailto:[hidden email]]On Behalf Of Flávio
Etrusco
Sent: 12 March 2007 15:19
To: FPC-Pascal users discussions
Subject: Re: [fpc-pascal] Value of runner variable after a for loop


On 3/12/07, Michael Van Canneyt <[hidden email]> wrote:

>
> On Mon, 12 Mar 2007, Luiz Americo Pereira Camara wrote:
>
> > Take the following example:
> >
> >  for i:= 0 to 2 do;
> >  WriteLn(i);
> >
> > Can i safely assume that after the for loop the value of "i" is 2?
>
> No. I think it is even specified in the pascal standard.
>

And unless the compiler (programmer) use an extra variable just to
attain this behaviour, the value after the loop will 3 (i.e. end value
+1).

-Flávio
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal

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

Re: Value of runner variable after a for loop

Jonas Maebe-2
In reply to this post by etrusco

On 12 mrt 2007, at 16:19, Flávio Etrusco wrote:

> And unless the compiler (programmer) use an extra variable just to
> attain this behaviour, the value after the loop will 3 (i.e. end value
> +1).

No, it can be anything, and the actual value even depends on the  
compiler version.


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

Re: Value of runner variable after a for loop

etrusco
In reply to this post by Andrew Denton
> I would NEVER rely on this in production code. The value of the control
> variable of a For loop after execution should never be relied upon. A simple
> change in the compiler at some future point could break a lot of your code.

To be honest I would call it a bug if a comipler didn't warn about it
(I don't only advocate it being reported as an 'error' so people don't
call me a fanatic ;-)

IMHO the for-loop variable should be visible only inside the loop and
should even be not reusable ;-) Actualy I've always dreamed the
somebody came up with a Pascal-coherent syntax for scoped variables,
but now FPC has inline functions support, I guess I was dreaming of
the wrong feature ;-)

But I still dream the day people agreed that all variables had to be
declared with a some modifier to be alowed to be assigned (with ':=')
more than once ;-)

Cheers,
Flávio
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Value of runner variable after a for loop

Luiz Americo Pereira Camara-2
In reply to this post by Jonas Maebe-2
Jonas Maebe wrote:

>
> On 12 mrt 2007, at 16:19, Flávio Etrusco wrote:
>
>> And unless the compiler (programmer) use an extra variable just to
>> attain this behaviour, the value after the loop will 3 (i.e. end value
>> +1).
>
> No, it can be anything, and the actual value even depends on the
> compiler version.
>
Thanks for all

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