-3

I want to skip the while loop only once. How can I do this?

import time
i = 0
while True:
    
    if i == 3:
        pass
    
    i += 1
    print(i)
    time.sleep(1)

Thank you.

  • 1
    refer here:https://stackoverflow.com/questions/58872300/like-while-loops-how-can-i-skip-a-step-in-a-for-loop – gretal Nov 30 '21 at 01:58
  • Does this answer your question? [Like while loops, how can I skip a step in a for loop?](https://stackoverflow.com/questions/58872300/like-while-loops-how-can-i-skip-a-step-in-a-for-loop) – Skully Dec 01 '21 at 00:03

2 Answers2

1

You can use the continue statement to move to the beginning of the loop and that way "skip" it, but you will need to increment i first.

NiKiZe
  • 1,256
  • 10
  • 26
  • Is there any other way I can jump to the next iteration once a condition is satisfied? – deepLearner Nov 30 '21 at 02:13
  • continue is the right thing to use for skipping over a loop, a different alternative would be to wrap the internals in an if block, but that gets ugly fast, another is to use a separate function that does the work that you call for each iteration, either "return early" or use if around it. – NiKiZe Nov 30 '21 at 02:27
0

You can do it like this :-

import time 
i = 0 
while True: 
    i += 1
    if i == 3: 
        continue   
    print(i) 
    time.sleep(1)
Python learner
  • 1,159
  • 1
  • 8
  • 20