Learn Python Programming

Infinite Loops


Sometimes a loop becomes infinite loop when a condition never becomes FALSE. This results in a loop to never ends. An infinite loop is useful in client/server programming where the server requires to run continuously so that clients program can communicate with it as and on need.

Example for Infinite Loops

i=1
while i<=10:
		print(i)

In the above example, it will display the value 1 forever. As there is always a increment statement is used i.e., i+=1, under while loop. So that a time occurs when value of i becomes equal or greater than 10. And the execution could terminate. But here we miss that to write. And due to this this program becomes infinite loop.

Below example will become finite loop:

i=1
while i<=10:
		print(i)
		i+=1

Another example of Infinite Loops

while (True):
		print('Hi')

This program will always display ‘Hi’ without stopping. Since the condition is read as True always.