0
Continue loop doubt?
In this case When 3 comes print skipping 3 So why in another 3 is printed https://code.sololearn.com/cqunRttKwO42/?ref=app
3 Answers
+ 5
In your loop you are printing at first i.
i = 2 -> print 2
i gets 3 -> print skipping i
continue jumps just to the beginning of the loop but does not change i. A while loop is not a for loop.
i is still 3 -> print(3)
i gets 4
If you want to work with continue in a while loop:
if i == 3:
i += 1
continue
But I would say it makes no sense to use continue in a while loop.
+ 5
i think it should be like that:
âȘïžfor output there is one print() in the if... clause -> 'Skipping...'
âȘïžand the other one is in the else... clause -> print number
i = 1
while i<=5:
if i==3:
print("Skipping 3")
else:
print(i)
i+=1
+ 1
You aren't actually skipping print(i) on line 3, so that prints all numbers.
Also, continue starts the next iteration of the loop, so having it as last statement in the loop doesn't change anything