Infinite Loop in Python | Stop, Example

Sometimes, we might need to want a loop to repeat as long as the program is running. We can perform it with the help of an infinite loop.

An infinite loop (also called endless loop) in Python is a sequence of statements in a program which executes endlessly.

It occurs when the loop condition always evaluates to true. In other words, a loop becomes an infinite loop if a loop condition never becomes false.

Usually, it is an error in the program when an infinite loop occurs. This is because it takes place only when there is some logical disconnection between the loop body (program code) and the test condition.

Infinite Loop Example using While Loop

Let’s understand it with the help of an example code.

# An example of an infinite loop.
count = 0
while count < 100:
    print('Count: ',count)
    count = count * 1 # OOPS, does not change the count value.

In the above example code, the variable count does not increment its value and remains 0. Due to which, the while loop executes the statement inside the loop body endlessly and produces the result as given:

      Count:  0
      Count:  0
      . . . . 
    --never stops--

Suppose you forget to place the count code that increment its value. For example:

# An example of infinite loop.
count = 0
while count < 100:
    print('Count: ',count)
    # OOPS, forget to put in a line that increments count value at all. 

However, in some cases, we might to run the code without stopping. For example, lots of video game programs use an infinite loop known as main loop.

An infinite loop is also useful in the client/server programming where the server needs to run continuously so that the client can communicate with it as and when requisite.

How to create an Infinite Loop in Python?

A simple way to make an infinite loop by setting the loop condition to True in the while loop, so that it keeps repeating a block of code forever! Let’s take an example based on it.

Example 1:

# A simple way to create an infinite loop.
while True: # It create an infinite loop.
    print('This is an infinite loop!')

The above code repeats forever, printing the string “This is an infinite loop!” over and over again because there is no false option to escape from the loop. The loop will repeat nonstop until you quit the program.

Example 2:

count = 1
while count == 1: # Creates an infinite loop.
    num = int(input('Enter a number: '))
    print('Your entered number = ',num)
print('Good bye!')

In this example, we have assigned the value 1 to the variable count. The count variable does not increment its value and continuously executing statements because the condition always produces the true result.

Look at the flowchart diagram of this program.

Flowchart diagram of an infinite loop in Python

When you will run the above program code, it produces the following result:

       Enter a number: 29
       Your entered number =  29
       Enter a number: 30
       Your entered number =  30
       Enter a number: t
       Traceback (most recent call last):
       File "C:\Python Project\", line 3, in 
       num = int(input('Enter a number: '))
       ValueError: invalid literal for int() with base 10: 't'

Note: Any statement or code written after an infinite loop will never execute. In the example 2, the last line of statement is unreachable because of the infinite while loop that comes before it.

How to Stop Infinite Loop in Python?

If you accidentally code an infinite loop, you can escape out from it by pressing the C key while holding down the Ctrl (control) key in the Python shell. This is a common way to stop an infinite loop.

If you are writing the code in IDLE, such as PyCharm editor, then you click on the run in the menu and click on the stop option. In IDLE, you can also press Ctrl+F6 to stop loop as well.

Stopping Loop using Break Statement

By mistake, if you construct an infinite while loop in the Python program, you can stop it using the break statement. It will stop the loop, come out of loop and execute the next statement that is following the infinite loop.

Let’s take an example program based on infinite while loop where we will print the sum of squares of numbers from 1 to 20.

sum = 0
i = 0
while True:
    sum = sum + i * i
    i = i + 1
    if i >= 20:
        break; # If the i value exceeds 20, then the control will come out of this loop.
print("Sum: ",sum)
      Sum: 2470

In this tutorial, you learned about infinite while loop statement in Python programming language with example programs. Hope that you will have understood the basic points of infinite while loop.

In the next tutorial, we will learn about the break statement in Python with a variety of example programs.
Thanks for reading!!!