Updated April 1, 2023
Introduction to Infinite Loop in C
A loop that repeats indefinitely and does not terminate is called an infinite loop. An infinite loop also called as endless loop or indefinite loop. An infinite loop is most of the time create by the mistake, but it does not mean that infinite loop is not require or not useful. Infinite loop can be use in an application where the application code is to be keep running for infinite until it is stopped example web server or where user input is to be accept and generate continuous output until user exits, operating system processes, games and so all.
Functions and Examples of Infinite Loop in C
The infinite loop in a program can be created in two ways:
- Unintentionally
- Intentionally
Unintentionally infinite loop gets create by bug in the code, by mistake or by specifying the condition which never becomes false. And intentionally infinite loop explicitly creates to achieve some requirement in an application. The loop structures we can use to create intentionally or explicitly infinite loop and run the code specified in a loop to repeatedly or infinite times. So we can use the following loops do 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 part of the for loop that is initialize, condition and increment/ decrement is not provided, which means no start value no end condition. So the loop run 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:
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 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:
As above the loop is running infinite times because short int ranges is -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 integer specify true condition), which means the condition always true and the runs for infinite times.
Next we write the c code to create the infinite loop by using while loop with the following example.
Code:
#include <stdio.h>
void main()
{ int i = 10;
while(1)
{
printf("%d\t",i);
i++;
}
}
Output:
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 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:
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 mistake which can lead to an infinite loop are:
- If Semicolon placed in the wrong position may lead to an infinite loop.
Example:
while(cond);
{
//code
}
- If logical conditions wrong by mistake, we used 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 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:
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 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:
As in the above code the goto statement becomes the infinite loop.
5. Macros
To create the infinite loop we can use macro which 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:
As in the above code the macro is defined whose value is for(;;). Later in a 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 repeats indefinitely and does not terminate. A program can have infinite loop by intentionally or unintentionally as we have seen above. We have seen various ways to create an infinite loop and the solution to come out from infinite loop is use of break statement.
Recommended Articles
This is a guide to Infinite Loop in C. Here we discuss the Introduction to Nested Loop in C and its working along with the examples and code implementation. You can also go through our other suggested articles to learn more –