Skip to content

Latest commit

 

History

History
20 lines (17 loc) · 429 Bytes

20221121092758.md

File metadata and controls

20 lines (17 loc) · 429 Bytes

while else

You can use an else on a while loop which will hit if the loop runs normally. If the loop is terminated prematurely though (using break or return), the code in the else won't execute. This construct can also be used with a for loop.

>>> while True:
...     break
... else:
...     print("hits")
...
>>> a = True
>>> while a:
...     a = False
... else:
...     print("hits")
...
hits

#looping