In C, for loops can have multiple 'increment' statements. For example:
int j;
unsigned int r;
r = 4; // in reality, a non-trivial initialization
for (j = 0; j < 1000; j++, r++) {
if (r == 8)
r = 0;
printf("j = %d, r = %u\n", j, r);
// ... do some work, including continue statements
}
In the above loop j and r are increased simultaneously in every iteration of the loop.
Now I wonder how I can replicate the same in Python. So far I've found the following solution, which is not as elegant.
r = 4 # actually a complicated initialization
r = r - 1
for j in range(1000):
r = r + 1
if r == 8:
r = 0
# the rest of the loop
The Python version is clearly uglier (particularly because of decrementing r by 1 before the start of the loop).
Is there a nicer way to do so?