Infinite Loops

This lesson explains how infinite loops might arise in a while loop using an example

Arisal of Infinite Loops

One common programming mistake is to create an infinite loop. An infinite loop refers to a loop, which under certain valid (or at least plausible) input, will never exit.

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.

Note: In the fully general case, it is not possible to automatically detect an infinite loop. This is known as the halting problem.

While the halting problem is not solvable in the fully general case, it is possible to determine whether a loop will halt for some specific cases.

Example of Infinite loop

Down below is an example of an infinite loop.

Note: You will get an error when you try to run the code below due to limitations of our platform. ​

Create a free account to access the full course.

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