0
Why is there a need to add continue when a while loop repeats itself?
Lesson 4(Regarding continue) of While Loops of Control Structures of Python 3. Why is there a need to add continue when a while loop repeats itself? I took out the line that contained continue and I got a similar result to when it had the line with continue.
3 Antworten
+ 1
1. Removing "continue" prints an extra 2.
2. "continue" may be used to simplify codes. Here's the same code without it. Some may prefer the first version, others the second.
i = 0
while True:
i = i +1
if i != 2:
if i == 5:
print("Breaking")
break
print(i)
else:
print("Skipping 2")
print("Finished")
0
A continue statement is actually essential in some cases, it skips what comes after it and it goes back to redo the loop completely ignoring the sentences after it.
0
Can you give an example when its needed?