Roel Schroeven <
roel@roelschroeven.net> writes:
:I wouldn't say "i *should* also disappear". There is no big book of :programming language design with rules like that that all languages have
:to follow. Different languages have different behavior. In some
:languages, for/if/while statements introduce a new scope, in other
:languages they don't. In Python, they don't. I won't say one is better
:than the other; they're just different.
From the point of view of purity, the scope of names should
be a small as possible.
... but not smaller! From the standpoint of practicality,
I remember that I need the last value of the loop variable often
enough after the end of the loop that it should remain available.
And I think, in BASIC, it's available too.
LIST
10 FOR I = 1 TO 2
20 PRINT I
30 NEXT I
40 PRINT I
50 END
RUN
1
2
3
Yes!
However, it is not clear whether it's 2 or 3.
In C,
main.c
#include <stdio.h>
int main( void )
{ int j;
for( j = 1; j < 3; ++j )printf( "%d\n", j );
printf( "%d\n", j );
for( int i = 1; i < 3; ++i )printf( "%d\n", i ); /*
printf( "%d\n", i ); // error: 'i' undeclared */ }
transcript
1
2
3
1
2
Similar to Basic.
In Pascal,
TMP.PAS
PROGRAM TMP( OUTPUT );
VAR I: INTEGER;
BEGIN
FOR I:=1 TO 2 DO
WRITELN( I );
WRITELN( I )
END.
OUTPUT
1
2
2
tmp.py
for i in range( 1, 3 ): print( i )
print( i )
sys.stdout
1
2
2
--- SoupGate-Win32 v1.05
* Origin: fsxNet Usenet Gateway (21:1/5)