How do I use a C-style for loop in Python?

There is no simple, precise equivalent of C's for statement in Python. Other answers cover using a Python for statement with a range, and that is absolutely what you should do when possible.

If you want to be able to modify the loop variable in the loop (and have it affect subsequent iterations), you have to use a while loop:

i = 0
while i < 7:
    if someCondition(i):
        i = 5
    i += 1

But in that loop, a continue statement will not have the same effect that a continue statement would have in a C for loop. If you want continue to work the way it does in C, you have to throw in a try/finally statement:

i = 0
while i < 7:
    try:
        if someCondition(i):
            i = 5
        elif otherCondition(i):
            continue
        print 'i = %d' % i
    finally:
        i += 1

As you can see, this is pretty ugly. You should look for a more Pythonic way to write your loop.

UPDATE

This just occurred to me... there is a complicated answer that lets you use a normal Python for loop like a C-style loop, and allows updating the loop variable, by writing a custom iterator. I wouldn't recommend this solution for any real programs, but it's a fun exercise.

Example “C-style” for loop:

for i in forrange(10):
    print(i)
    if i == 3:
        i.update(7)

Output:

0
1
2
3
8
9

The trick is forrange uses a subclass of int that adds an update method. Implementation of forrange:

class forrange:

    def __init__(self, startOrStop, stop=None, step=1):
        if step == 0:
            raise ValueError('forrange step argument must not be zero')
        if not isinstance(startOrStop, int):
            raise TypeError('forrange startOrStop argument must be an int')
        if stop is not None and not isinstance(stop, int):
            raise TypeError('forrange stop argument must be an int')

        if stop is None:
            self.start = 0
            self.stop = startOrStop
            self.step = step
        else:
            self.start = startOrStop
            self.stop = stop
            self.step = step

    def __iter__(self):
        return self.foriterator(self.start, self.stop, self.step)

    class foriterator:

        def __init__(self, start, stop, step):
            self.currentValue = None
            self.nextValue = start
            self.stop = stop
            self.step = step

        def __iter__(self): return self

        def next(self):
            if self.step > 0 and self.nextValue >= self.stop:
                raise StopIteration
            if self.step < 0 and self.nextValue <= self.stop:
                raise StopIteration
            self.currentValue = forrange.forvalue(self.nextValue, self)
            self.nextValue += self.step
            return self.currentValue

    class forvalue(int):
        def __new__(cls, value, iterator):
            value = super(forrange.forvalue, cls).__new__(cls, value)
            value.iterator = iterator
            return value

        def update(self, value):
            if not isinstance(self, int):
                raise TypeError('forvalue.update value must be an int')
            if self == self.iterator.currentValue:
                self.iterator.nextValue = value + self.iterator.step

In C:

for(int i=0; i<9; i+=2)
{
    dosomething(i);
}

In python3:

for i in range(0, 9, 2):
    dosomething(i)

You just express the same idea in different languages.

Tags:

Python