# [Tutor] How many loops does "break" jump out of?

Thu Feb 22 18:39:20 CET 2007

```Am trying to convert a C program with a GOTO in it to Python and was wondering
how many loops a Python "break" jumps out of.  Here is the C pseudo code

if (test_1) {
for (;;) {
if (test_2) {
do_stuff();
} else if (test_2) {
for (ip=m1+m2+1;ip<=m;ip++) {
if (test_3) {
do_more_stuff();
if (test_4)
goto one;
}
}
for (i=m1+1;i<=m1+m2;i++)
do_even_more_stuff();

do_yet_more_stuff();
}

do_final_stuff();

one:    continue_program();

Python version 1 - assumes break jumps out of outer enclosing loop

if (test_1):
while 1:
if (test_2):
do_stuff()
elif (test_2):
for ip in range(m1+m2+1, m):
if (test_3):
do_more_stuff()
if (test_4):
break
for i in range(m1+1, m1+m2):
do_even_more_stuff()

do_yet_more_stuff()
}

do_final_stuff()

continue_program()    # one (break jumps out of while loop to here)

Python version 2 - assumes break jumps out of inner loop only.

if (test_1):
while 1:
if (test_2):
do_stuff()
elif (test_2):
for ip in range(m1+m2+1, m):
if (test_3):
do_more_stuff()
if (test_4):
quit_loop = true
break    # jump out of for loop

if quit_loop:
break:           # jump out of if test

for i in range(m1+1, m1+m2):
do_even_more_stuff()

do_yet_more_stuff()
}

# break jumps out of if loop to here

if quit_loop:
break    # jump out of while loop

do_final_stuff()

continue_program()    # one

Python version 3 - assumes break jumps out of for loop only.

if (test_1):
while 1:
if (test_2):
do_stuff()
elif (test_2):
for ip in range(m1+m2+1, m):
if (test_3):
do_more_stuff()
if (test_4):
quit_loop = true
break    # jump out of for loop

for i in range(m1+1, m1+m2):
do_even_more_stuff()

do_yet_more_stuff()
}

# break jumps out of for loop to here

if quit_loop:
break    # jump out of while loop

do_final_stuff()

continue_program()    # one

Unfortunately, can't test the Python script against the C program 'cos I don't
have a C compiler.  All commments gratefully received
```