12.2. While Patterns — Python
Until
Infinite Loop
Exit Flag
12.2.1. Until
Has stop conditions
>>> i = 0 >>> until = 3 >>> >>> while i < until: ... print('Processing...') ... i += 1 ... Processing... Processing... Processing...
12.2.2. Infinite Loop
Never ending loop. Used in servers to wait forever for incoming connections. Used in games for game logic.
>>> while True: ... print('Processing...') ... Processing... Processing... Processing... ...
12.2.3. Exit Flag
>>> abort = False >>> >>> while not abort: ... print('Processing...') ... if ...: # problem detected ... print('Abort, Abort, Abort!') ... abort = True ... Processing... Abort, Abort, Abort!
In real life the exit flag pattern is useful if you have for example multi-threaded application. You can kill all the threads if any thread changes the flag. Multi-threaded apps will share this value and kill the loop as soon as the condition will be evaluated.