Infinite Loops

Learn about infinite loops, their examples, and their use cases.

As already seen, a common programming mistake is creating an infinite loop. An infinite loop refers to a loop that will never exit under certain valid (or at least plausible) input. This could be because the loop variable is modified so that the exit condition is never met.

Note: Beginning programmers should be careful to examine all the possible inputs into a loop to ensure that for each such set of inputs, there is an exit condition that will eventually be reached.

Compilers, debuggers, and other programming tools can only help the programmer so far in detecting infinite loops.

Sometimes, an infinite loop is an intentional design requirement. This lesson focuses on those loops before listing some of the popular use cases.

Infinite while loop

Below is an example of an infinite loop. This program uses a while loop to print “Infinite loop” repeatedly without stopping. This means this program will continue running unless an external process stops it.

Note: We will get an error when you try to run the code below due to security checks in our platform. ​

Create a free account to access the full course.

Continue your learning journey with a 14-day free trial.

By signing up, you agree to Educative's Terms of Service and Privacy Policy