2

Suppose I have a a loop as such:

for i in range(0,100):
    if (condition):
    X

and I want the loop, in the event of the conditional being true, not to skip to the next iteration of the loop, but to skip several iterations forward. i.e. It is as if I want not just one 'continue' statement, but several.

For example, suppose my loop is on i=57. The conditional is true. I want it to then skip 58, 59, 60, 61, and go to process i=62. The conditional there is false, and it then goes to process 63.

Is there any way to implement this in a standard for loop?

3 Answers 3

2

Perhaps use a while loop:

x = 0
while x < 100:
    if condition(x):
        x += 5
    else:
        x += 1
Sign up to request clarification or add additional context in comments.

1 Comment

I just thought of that! Thanks, nonetheless!
1

Never mind, the solution is simple! I just need to use a while loop and increment it manually.

1 Comment

20 second right after my post :p. Well, congrats in getting it working! :)
1

If the range you are processing over is indeed static, you would be better served by a while block. That way you have arbitrary control over the index being used.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.