Infinite Loops
Learn about infinite loops, its examples and their use cases.
As already seen, a 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. This could be because the the loop variable is modified in a manner 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 a intentional and a design requirement. This lesson focuses on those loops before listing some of the popular use cases.
Infinite while
loop
Down below is an example of an infinite loop. This program prints "Infinite loop" using a while
loop infinite times. This means this program will continue running unless an external process stops it.
Note: You 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.
By signing up, you agree to Educative's Terms of Service and Privacy Policy