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

Bob Gailer bgailer at alum.rpi.edu
Fri Feb 23 03:57:04 CET 2007


etrade.griffiths at dsl.pipex.com wrote:
> 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();
I'd put this code in a function, replace the goto one with return, and 
put a call to the function in place of the code.

def tests():
  if test_1:
    etc etc
        if test_4: return

Some may take issue with that. You can, as Alan says, restructure the 
code, or use intermediate variables.

-- 
Bob Gailer
510-978-4454



More information about the Tutor mailing list