goto
Kay Schluehr
kay.schluehr at gmx.net
Mon Jul 18 11:40:16 EDT 2005
Hayri ERDENER schrieb:
> hi,
> what is the equivalent of C languages' goto statement in python?
> best regards
No, but some of goto's use cases can be covered by unconditional jumps
provided by exceptions.
Here is a C function using goto:
void main()
{
int i, j;
for ( i = 0; i < 10; i++ )
{
printf( "Outer loop executing. i = %d\n", i );
for ( j = 0; j < 2; j++ )
{
printf( " Inner loop executing. j = %d\n", j );
if ( i == 3 )
goto stop;
}
}
/* This message does not print: */
printf( "Loop exited. i = %d\n", i );
stop: printf( "Jumped to stop. i = %d\n", i );
}
And here is a Python equivalent using exception handling:
def main():
class stop(Exception):pass
try:
for i in range(10):
print "Outer loop executing. i = %d"%i
for j in range(2):
print " Inner loop executing. j = %d"%j
if i == 3:
raise stop
print "Loop exited. i = %d"%i # message does not print
except stop:
print "Jumped to stop. i = %d"%i
Regards,
Kay
More information about the Python-list
mailing list