Full Stack Developer
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.
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:
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( ; ; )
{
// 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:-
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:-
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.
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:-
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:-
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
}
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:-
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:-
As in the above code, the goto statement becomes the infinite loop.
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:-
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.
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.