Infinite Loop is a loop that does not end. Infinite loops consist of repetitive steps which condition require repeating always. Python provides different ways for infinite loops. The while statement can be used for the infinite loop in Python. But before using Python infinite loops keep in mind that the infinite loops use a lot of system resources like CPU, Memory which may cause system crashes or performance problems.
Infinite Loop with while
One of the most popular and easy way to implement infinite loop in Python is using the while statement. The while statement has the following syntax where the condition is used to control steps and iterations. By providing allways true condition to the while loop this will be an infinite loop.
while CONDITION BLOCK
- CONDITON is used to control while loop. We will set the CONDITION true for allways in order to create infinite loop with while.
- BLOCK is the body of the while infinite loop which will be executed in every step or iteration.
The most easy way is setting the condition as True for the while loop.
while True: print("This is infinite loop")
We can see the following output that the same output printed again and again.
Stop Infinite Loop
The infinite loop continues unless it is killed, stopped. There are different ways to stop the infinite loop. The CTRL+C key shortcut can be used to kill the infinite loop. While the infinite loop is executing press CTRL+C at the same time. We can see that the KeyboardInterrupt is printed as the infinite loop is interrupted.
This is infinite loop This is infinite loop This is infinite loop This is infinite loop This is infinite loop This is infinite loop This is infinite loop This is infinite loop ^CTraceback (most recent call last): File "", line 2, in KeyboardInterrupt This is infinite loop
Infinite Loop with if
Infinite loop is created to iterate to infinite but in some cases, we may want to check some conditions in every iteration. The if statement or conditional can be used to check every step of the infinite loop. In the following example, we check if the current number is odd or not by using the if statement.
number=0 while True: if (number%2) == 1: print(number "is odd")
Break Infinite Loop
In some cases, we may need to break the infinite loop. Even it may seem that this is not an infinite loop the breaking infinite loop is an exceptional case. The break statement can be used to break the infinite loop. The break statement also used with the if statement in order to break the infinite loop for specific cases. In the following example if the square of the number is greater than 10000 the infinite loop ended.
number=0 while True: if (number*number) > 10000: break
Skip Steps In Infnite Loop
Another useful case for the infinite loop is skipping some steps. The continue statement can be used to skip some steps with the if statement. In the following example we will skip odd numbers by using continue in the infinite loop.
number=0 while True: if (number%2) == 1: continue print(number "is even")