Infinite Loops in Python: Definition & Examples

An error occurred trying to load this video.

Try refreshing the page, or contact customer support.

Coming up next: Nested Loops in Python: Definition & Examples

You're on a roll. Keep up the good work!

Take Quiz Watch Next Lesson
Your next lesson will play in 10 seconds
  • 0:04 Finite vs Infinite Loops
  • 1:17 Using an Infinite Loop
  • 2:20 Kinds of Infinite Loops
  • 5:18 Lesson Summary
Save Save Save

Want to watch this again later?

Log in or sign up to add this lesson to a Custom Course.

Log in or Sign up

Lesson Transcript
Instructor: Paulo Lemelle Fernandes

Paulo has been a Computer Science Professor and researcher for more than 25 years. He has a Ph.D. degree from Institut National Polytechnique de Grenoble, France (1998).

Loops are basic to all programming languages, and for Python it is no different. In this lesson we will see how to handle a type of loop: the infinite loop (i.e., loops that do not have a previously explicit end, but eventually have to end anyway).

Finite vs. Infinite Loops

There are two different types of loop, the finite ones and the infinite ones.

The most common kind of loop is the finite loop (i.e., a loop that we explicitly know, in advance, which values the control variables will have during the loop execution). The simplest case of a finite loop is the for loop within a range, as in the example appearing here. This loop goes through all the values from 0 to 9 (one before 10) for the control variable i:

for i in range(0, 10):

This program will result in the following output:











Another example of a finite loop can be done with the while command. The code here produces the same result as the previous finite loop example using for:

i = 0
while i < 10:
    i += 1

An infinite loop, on the other hand, is characterized by not having an explicit end, unlike the finite ones exemplified previously, where the control variable i clearly went from 0 to 9 (note that at the end i = 10, but that value of i wasn't printed). In an infinite loop the control is not explicitly clear, as in the example appearing here:

i = 0
while True:
    if i == 9:
       i += 1

Note that this program will also print the values from 0 till 9, just like the finite loop examples shown before. However, in this example, the ending value of the variable i will be equal to 9, and it will be printed.

Using an Infinite Loop

Some occasions, like the previously shown examples, may be coded with finite or infinite loops. However, in some other cases it's impossible to code without using infinite loops. For example, let's consider a program that generates positive random numbers and keeps on accumulating these numbers until reaching a threshold; for example, 63. The program appearing now codes this example using an infinite loop:

from random import randint
num_acc = 0
num_rand = 0
acc = 0
while (true):
    x = randint(0, 10)
    num_rand += 1
    if acc + x > 63:
       print(' Discarding: ', x)
       print('Considering: ', x)
       num_acc += 1
       acc += x
       if acc == 63:
print('%s numbers were generated, but only %s numbers were considered to reach the %s threshold' % (num_rand, num_acc, acc))

This program actually generates several random numbers, and it prevents the accumulated sum from exceeding the threshold value of 63. It also computes how many numbers were randomized, and how many numbers were considered before the accumulated value reached the threshold. One possible execution for this program would result in the following output:

Considering: 4

Considering: 10

Considering: 1

Considering: 2

Considering: 1

Considering: 4

Considering: 2

Considering: 0

Considering: 4

Considering: 9

Considering: 7

Considering: 7

Considering: 5

Discarding: 10

Considering: 0

Discarding: 8

Considering: 7

17 numbers were generated, but only 15 numbers were considered to reach the 63 threshold.

Note that this example receives as input a random number; therefore, it's impossible to know in advance how many loop iterations will be performed. Thus, an infinite loop is not a choice, but a necessity.

Kinds of Infinite Loops

Generally speaking, there are three kinds of infinite loops:

  1. The fake infinite loops
  2. The intended infinite loops
  3. The unintended infinite loops

Let's look at these three types one at a time.

1. Fake Infinite Loops

Despite the name, these kinds of infinite loops are the really interesting ones, since these are infinite loops that aren't really infinite. They just seem infinite (a while True condition exists), and actually they exit upon a condition (a break inside an if).

In fact, we may consider that while fake infinite loops may have an exiting test:

  • At the beginning - these are regular while loops with a condition after the while, like the second example of finite loops
  • In the middle - these are infinite loops with the exiting condition (if - break) inside the loop commands
  • At the end - these are the most common ones, and they have the exit condition as the last command of the loop. The previous example is one of those fake infinite loops with an exit condition at the end (the if at line 15).

2. Intended Infinite Loops

It's rare, but sometimes you may want to have a loop that intentionally doesn't end, as, for example, an overall loop controlling the user interface of your program. In those cases you want to code an infinite loop that is supposed to run forever. Note that in this context forever does not really mean until the end of time, but it means until the program is externally interrupted (for example, with a control-C).

To unlock this lesson you must be a Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use

Become a member and start learning now.
Become a Member  Back
What teachers are saying about
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create an account