how to write a C-style for loop?

Duncan Booth duncan.booth at invalid.invalid
Wed Feb 15 07:48:11 EST 2006


Steven D'Aprano wrote:

>> about range()/xrange(): what if you want to traslate this c-loop? for
>> (int i=1; i<50; i*=2)
> 
> That's a completely different question, so of course it has a completely
> different answer. Here is one way:

... various options snipped ...

and another way for use when you have more than one loop following this 
pattern:

def powerrange(base, start=0, limit=0):
    value = base**start
    while value < limit:
        yield value
        value *= base

        
for i in powerrange(2,0,50):
    dosomething(i)


Putting the question the other way round: how would you move the control 
logic out of the loop in C?



More information about the Python-list mailing list