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

Mailing lists / fpc-pascal / 2007-03
7 messages · 5 people ·
Reply by email Subscribe to this list Your subscription
Read-only archive. Use j and k to move between messages.
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
No. I think it is even specified in the pascal standard.
Michael.
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
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
And unless the compiler (programmer) use an extra variable just to attain this behaviour,…
Flávio Etrusco wroteAnd 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
I would NEVER rely on this in production code. The value of the control variable of a For…
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
No, it can be anything, and the actual value even depends on the compiler version. Jonas
Thanks for all
Luiz