Good python equivalent to C goto

Grant Edwards grante at visi.com
Sun Aug 17 10:08:35 EDT 2008


On 2008-08-16, Dennis Lee Bieber <wlfraed at ix.netcom.com> wrote:
> On Sat, 16 Aug 2008 23:20:52 +0200, Kurien Mathew <kmathew at envivio.fr>
> declaimed the following in comp.lang.python:
>
>> Hello,
>> 
>> Any suggestions on a good python equivalent for the following C code:
>> 
>> while (loopCondition)
>> {
>>   if (condition1)
>>     goto next;
>>   if (condition2)
>>     goto next;
>>   if (condition3)
>>     goto next;
>>   stmt1;
>>   stmt2;
>> next:
>>   stmt3;
>>   stmt4;
>>   }
>> 
>
> 	Nasty code even for C... I've never used goto in C...

I do sometimes, but it's for exception handling, and in Python
one uses try/raise/except.  The example above isn't the best
way to show this, but perhaps something like the code below

while loopCondition:
    try:
        if condition 1:
           raise MyException    
        if condition 2:
           raise MyException
        if condition 3:
           raise MyException
        stmt1;
        stmt2;
        stmt3;
    except: MyException
        pass
    stmt4;
    stmt5;
    


-- 
Grant Edwards                   grante             Yow!  People humiliating
                                  at               a salami!
                               visi.com            



More information about the Python-list mailing list