The Infinite Loop in C

A loop that repeats indefinitely and never terminates is called an Infinite loop.

Most of the time we create infinite loops by mistake. However, this doesn’t mean that the infinite loops are not useful. Infinite loops are commonly used in programs that keep running for long periods of time until they are stopped like the web server.

In the following examples, we demonstrate what kind of mistakes can lead to an infinite loop:

Example 1:

This loop is an infinite loop. Here is why? According to the condition, the loop will execute until (i < 32768). Initially, the value of i is 32765 and after each iteration, its value is incremented by the update expression (i++).  But the value of short int type ranges from -32768 to 32767. If you try to increment the value of i beyond 32767, it goes on the negative side and this process keeps repeating indefinitely. Hence the condition (i < 32768) will always be true.

Example 2:

Here we are not updating the value of i. So after each iteration, the value of i remains the same. As a result, the condition (i<10) will always be true. For the loop to work correctly add i++;, just after the printf() statement.

Always remember, it is easy to forget update expression in while and do while loop, than in the for loop.

Example 3:

Another common mistake that leads to an infinite loop is to use the assignment operator (=) where the equality operator is needed (==).

What we actually wanted is that loop should execute until i is equal to 10. The value of the whole expression (i=10) is 10, since a non-zero value is considered true, the condition is always true and the loop will go on to execute indefinitely. To fix the problem replace the expression (i=10) with (i==10).

Example 4:

This loop is infinite because computers represent floating point numbers as approximate numbers, so 3.0 may be stored as 2.999999 or 3.00001. So the condition (f != 31.0) never becomes false. To fix this problem write the condition as f <= 31.0.

Example 5:

This loop will produce no output and will go on executing indefinitely. Take a closer look and notice the semicolon ( ; ) at the end of while condition. We know semicolon ( ; ) after the condition is a null statement. So the loop is equivalent to the following:

We can now clearly see that we are not updating the value if i inside the while loop. As a result, it will go on executing indefinitely.

9 thoughts on “The Infinite Loop in C

  1. I m confused in example 4. i copy and pasted and run the program(with error) and the result was same as written in example 4 i.e there is an infinite loop but i couldn’t understand why f could not be equal to 31.000000 i mean we are only incrementing +.1 so 2 will become 2.1 in first iteration and then 2.2 in 2nd and 2.3 in 3rd and 3 in 10th iteration and so on until 2 will become 31.000000. plz help me friends

    • because you are doing wrong , instead of i+2 you have use i+=2
      with i+2 you are not incrementing the value of i , but you just take the value of i a plus it with 2 and dont save this value antwhere

Leave a Comment

%d bloggers like this: