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

Chuck Coker python-tutor at ccoker.net
Fri Feb 23 18:12:02 CET 2007


Bob,

Watch your code blocks to make sure you don't change your logic
inadvertently. The code starting with "if (test_2)" and continuing to
the end of the snippet is inside the perpetual for loop which is inside
the "if (test_1)" condition.

Chuck



Bob Gailer wrote:

> 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.

-- 
======================================================================
Chuck Coker, Software Developer                python-tutor at ccoker.net
Tyrell Software Corporation                      http://www.tyrell.com
Office: +1 949 458 1911 x 203                    Cell: +1 714 326 5939
======================================================================



More information about the Tutor mailing list