Using the structure while/else in Python

Using the structure while/else in Python

Certainly, you must be used to “while” and “else”. However, Python has one structure called while/else. For example:

while condition:
    doStuff()
else:
    doAnotherStuff()

It seems rather strange, but this makes sense. The condition in while will be evaluated, if true, while will be executed. If false, the inner while will not be executed. Meanwhile, if the “while” block is never executed, then the “else” block will. Only in this situation!

and that’s all folks!

If you have any doubts, problems or suggestions, just leave a message.

No Comments Yet