Explaining Infinite Loop in C with Example

Explaining Infinite Loop in C with Example
Techiio-author
Written by Shuvhojit DebMarch 1, 2022
10 min read
C
1 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 1 VIEWS SHARE
Techiio-author
Shuvhojit Deb

Full Stack Developer

In this article, we'll know about Infinite Loop in C.

Introduction

An infinite loop is a looping construct that does not terminate the loop and executes the loop forever. It is also called an indefinite loop or an endless loop. It either produces a continuous output or no output.

When to use an infinite loop

An infinite loop is useful for those applications that accept the user input and generate the output continuously until the user exits from the application manually. In the following situations, this type of loop can be used:

  • All the operating systems run in an infinite loop as it does not exist after performing some task. It comes out of an infinite loop only when the user manually shuts down the system.
  • All the servers run in an infinite loop as the server responds to all the client requests. It comes out of an indefinite loop only when the administrator shuts down the server manually.
  • All the games also run in an infinite loop. The game will accept the user requests until the user exits from the game.

Functions and Examples of Infinite Loop

An unintentionally infinite loop gets created by a bug in the code, by mistake, or by specifying the condition which never becomes false. An intentionally infinite loop is explicitly created to achieve some requirement in an application. The loop structures we can use to create intentionally or explicitly infinite loops and run the code specified in a loop repeatedly or infinite times. So we can use the following loops to create an infinite loop –

  • for loop
  • while loop
  • do-while loop
  • go to statement
  • C macros

1. For loop:

Syntax:-

for( ; ; )
{
// some code which run infinite times
}

In the above syntax, three parts of the for loop that are initialized, condition, and increment/ decrement are not provided, which means no start value no end condition. So the loop runs for infinite times.

Next, we write the c code to understand the infinite for loop working more clearly with the following example.

Code:-

#include <stdio.h>
void main()
{  int i = 10;
for( ; ;)
{
printf("%d\n",i);
}
}

Output:-

blogpost

As in the above code, the for loop is running for infinite times and printing the I value that is 10 infinitely.

Next, we write the c code to show the kind of mistakes that can lead to an infinite loop in for loop –

Code:-

#include <stdio.h>
void main()
{  short int x;
for (x = 32765; x< 32768; x++)
{
printf("%d\t", x);
}
}

Output:-

blogpost

As above the loop is running infinite times because short int ranges are -32768 to 32767, so when i is the increment above to 32767 it becomes negative and hence the condition becomes always true.

2. While Loop:

Syntax:-

while(1)
{
// some code which run infinite times
}

In the above syntax, the condition pass is 1 (non zero integers specify true condition), which means the condition is always true and runs for infinite times.

Next, we write the c code to create the infinite loop by using the while loop with the following example.

Code:-

#include <stdio.h>
void main()
{  int i = 10;
while(1)
{
printf("%d\t",i);
i++;
}
}

Output:-

blogpost

As in the above code while loop runs infinite times because the condition always becomes true and the I value is updated infinite times.

Next, we write the c code to show the kind of mistakes that can lead to an infinite loop in for loop –

Code:-

#include <stdio.h>
void main()
{  int i = 10;
while(i<100)
{
printf("%d\t",i);
}
}

Output:-

blogpost

As in the above code, the mistake is updating of I value is missing which leads to an infinite loop.

Other than this some more mistakes which can lead to an infinite loop are:

If Semicolon is placed in the wrong position may lead to an infinite loop.

Example:-

while(cond);
{
//code
}

If logical conditions are wrong by mistake, we used an assignment operator (=) instead of a relational operator (= =) may lead to an infinite loop.

Example:-

while(inp='y')
{
//code
}

If loop condition mismatch may lead to an infinite loop.

Example:-

for(int i=0;i>=0;i++)
{
//code
}

3. Do-While Loop:

Syntax:-

do
{
// some code which run infinite times
}  while(1);

Next, we write the c code to create the infinite loop by using the do-while loop with the following example.

Code:-

#include <stdio.h>
void main()
{  int i = 10;
do
{
printf("%d\t",i);
i++;
}  while(i);
}

Output:-

blogpost

4. Goto Statement

Syntax:-

label:
// some code which run infinite times
goto label;

Next, we write the c code to create the infinite loop by using the goto statement with the following example.

Code:-

#include <stdio.h>
void checkEven(int num)
{
if (num%2 == 0)
goto even_no;
else
goto odd_no;
even_no:
printf("The number is even.\t");
goto even_no;
odd_no:
printf("The number is odd.\t");
goto odd_no;
}
void main() {
int i = 10;
checkEven(i);
}

Output:-

blogpost

As in the above code, the goto statement becomes the infinite loop.

5. Macros:

To create the infinite loop we can use a macro that defines the infinite loop. Next, we write the c code to create the infinite loop by using macro with the following example.

Code:-

#include<stdio.h>
#define macro_name for( ; ; )
void main()
{
int i=10;
macro_name
{
printf("%d\t", i);
}
}

Output:-

blogpost

As in the above code, the macro is defined whose value is for(;;). Later in the main function macro is used by its name, whenever the name of macro comes it gets replaced by its value.

Conclusion

An infinite loop is a loop that rehashes endlessly and doesn't end. A program can have an infinite loop intentionally or unintentionally as we have seen previously. We have seen different ways of making an infinite loop and the answer for emerging from an infinite loop is the utilization of break statement.

C Language
Programming
Infinite Loop
Loops in C
1 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 1 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance