+ 2
What is the significance of infinite loop ?
In most of the cases we use normal finite loop through while and for loop. What is the use of the infinite loop ? Where we can use an infinite loop ?
3 Antworten
+ 8
Infinite loops are useful if you want your script (or part of the code) to run endlessly, until a specific action is taken. Especially, when there can be more than one action that stops your script or block of code from working.
In event-based programming paradigm endless loops are very common.
Some automation jobs are running endlessly and constantly listening to certain events to happen, to react adequately.
+ 6
Imagine something like this:
while True:
won = game()
# Now the code of function
# game happens, returns
# True or False
if won:
print('You won! :)')
else:
print('You lost. :(')
print('Another game (y/n)')
if input() == 'n':
break
0
Another simple example is when you open a Linux terminal, it's in a continuous loop waiting to execute the commands that you enter one after the other , then finally when you are done , you may type in the exit command, which will cause the loop to break and exit the terminal program.