Infinite Loop in C Language

What is an Infinite Loop in C Language?

An infinite loop in C language is a looping construct that does not terminate and executes continuously. It is als

o referred to as an indefinite loop or an endless loop. It either produces a continuous output or no output at all.

When to Use an Infinite Loop in C Language

An infinite loop in C language is useful for applications that continuously accept user input and generate output until the user manually exits the application. It can be used in the following scenarios:

  1. Operating Systems: All operating systems run in an infinite loop and only exit when the user manually shuts down the system.
  2. Servers: Servers operate in an infinite loop, responding to client requests. They exit the loop only when an administrator manually shuts down the server.
  3. Games: Games also run in an infinite loop, accepting user requests until the user chooses to exit.

You can create an infinite loop using various loop structures. The following loop structures can be used to define an infinite loop:

  1. for loop
    Here’s an example of an infinite ‘for’ loop:
   for(; ;)  
   {  
       // Body of the for loop.  
   }  

In this example, there’s no condition specified in the ‘for’ loop, so it will execute infinitely.

   #include <stdio.h>  
   int main()  
   {  
      for(;;)  
      {  
        printf("Hello javatpoint");  
      }  
      return 0;  
   }  

The above code runs the ‘for’ loop infinitely, continuously displaying “Hello javatpoint.”

Output:

   Infinite Loop in C
  1. while loop
    You can create an infinite loop using a ‘while’ loop as follows:
   while(1)  
   {  
      // Body of the loop..  
   }  

In this ‘while’ loop, ‘1’ is used as the condition, where any non-zero integer represents a true condition.

   #include <stdio.h>  
   int main()  
   {  
      int i=0;  
      while(1)  
      {  
          i++;   
          printf("i is :%d",i);  
      }  
      return 0;  
   }  

The above code defines a ‘while’ loop that runs infinitely without a specified condition, continuously updating the value of ‘i.’

Output:

   Infinite Loop in C
  1. do..while loop
    You can also create an infinite loop using a ‘do..while’ loop:
   do  
   {  
       // Body of the loop..  
   } while(1);  

In this ‘do..while’ loop, ‘1’ is used as the condition, ensuring the loop runs infinitely.

  1. goto statement
    An infinite loop can be defined using the ‘goto’ statement:
   infinite_loop;  
   // Body statements.  
   goto infinite_loop;  

The ‘goto’ statement transfers control to the ‘infinite_loop’ label, creating an infinite loop.

  1. Macros
    You can use a macro constant to create an infinite loop:
   #include <stdio.h>  
   #define infinite for(;;)  
   int main()  
   {  
      infinite  
      {  
          printf("hello");  
      }  
      return 0;  
   }  

In this example, the macro ‘infinite’ is defined as ‘for(;;),’ and its use in the program creates an infinite loop.

Output:

   Infinite Loop in C

Exiting an Infinite Loop
To exit an infinite loop, you can use the ‘break’ statement, as shown in the following example:

#include <stdio.h>  
int main()  
{  
   char ch;  
   while(1)  
   {  
       ch = getchar();  
       if(ch == 'n')  
       {  
           break;  
       }  
       printf("hello");  
   }  
   return 0;  
}  

In this code, the ‘while’ loop executes infinitely until the user enters ‘n.’ The ‘if’ statement with the ‘break’ keyword allows the loop to exit.

Unintentional Infinite Loops

Unintentional infinite loops can occur due to code bugs. Beginners should be cautious and consider the following measures to identify unintentional infinite loops:

  1. Carefully examine semicolons in loop conditions to avoid mistakenly placing them in the wrong position, which can lead to an infinite loop.
   #include <stdio.h>  
   int main()  
   {  
      int i=1;  
      while(i<=10);  
      {  
         printf("%d", i);  
         i++;  
      }  
      return 0;  
   }  
  1. Check logical conditions carefully to ensure the correct use of relational operators instead of assignment operators.
   #include <stdio.h>  
   int main()  
   {  
      char ch = 'n';  
      while(ch == 'y')  
      {  
         printf("hello");  
      }  
      return 0;  
   }  
  1. Avoid using incorrect loop conditions that cause the loop to execute indefinitely.
   #include <stdio.h>  
   int main()  
   {  
      for(int i = 1; i >= 1; i++)  
      {  
         printf("hello");  
      }  
      return 0;  
   }  
  1. Be cautious when using the ‘break’ keyword in nested loops, as it only terminates the nearest loop, not the entire loop structure.
   #include <stdio.h>  
   int main()  
   {  
      while(1)  
      {  
         for(int i = 1; i <= 10; i++)  
         {  
            if(i % 2 == 0)  
            {  
               break;  
            }  
         }  
      }  
      return 0;  
   }  
  1. Be aware of floating-point errors when using floating-point values inside loops. Consider using appropriate conditions to avoid unintentional infinite loops.
   #include <stdio.h>  
   int main()  
   {  
      float x = 3.0;  
      while (x != 4.0)  
      {  
         printf("x = %f\n", x);  
         x += 0.1;  
      }  
      return 0;  
   }  

In this code, the loop runs infinitely due to floating-point representation.

Solution: Use an appropriate condition, such as (x <= 4.0), to prevent an unintentional infinite loop.


Discover more from PiEmbSysTech

Subscribe to get the latest posts sent to your email.

Leave a Reply

Scroll to Top

Discover more from PiEmbSysTech

Subscribe now to keep reading and get access to the full archive.

Continue reading