If you haven’t read my previous articles about the for loop and the while loop, feel free to do so. Today we’ll be talking about infinite loops.
It may happen that the condition in the while statement is always true. Then the loop never stops. We call such loops infinite loops. Sometimes such loops are useful, for example in client/server programming, but oftentimes they’re a result of our mistake. If we happen to be trapped in such a loop, we can use the Keyboard Interrupt combination (Ctrl + C) to get out of it. Here’s an example of such an infinite loop:
number = 1
while number == 1:
x = input("Enter a number: ")
print(x)
As number is equal to 1 all the time (its value isn’t changed inside the loop), the condition is always true and the loop is infinite.