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:
We can create an infinite loop through various loop structures. The following are the loop structures through which we will define the infinite loop:
Let's see the infinite 'for' loop. The following is the definition for the infinite for loop:
for(; ;) { // body of the for loop. }
As we know that all the parts of the 'for' loop are optional, and in the above for loop, we have not mentioned any condition; so, this loop will execute infinite times.
Let's understand through an example.
#include <stdio.h> int main() { for(;;) { printf("Hello rookienerd"); } return 0; }
In the above code, we run the 'for' loop infinite times, so "Hello rookienerd" will be displayed infinitely.
Output
Now, we will see how to create an infinite loop using a while loop. The following is the definition for the infinite while loop:
while(1) { // body of the loop.. }
In the above while loop, we put '1' inside the loop condition. As we know that any non-zero integer represents the true condition while '0' represents the false condition.
Let's look at a simple example.
#include <stdio.h> int main() { int i=0; while(1) { i++; printf("i is :%d",i); } return 0; }
In the above code, we have defined a while loop, which runs infinite times as it does not contain any condition. The value of 'i' will be updated an infinite number of times.
Output
do..while loop
The do..while loop can also be used to create the infinite loop. The following is the syntax to create the infinite do..while loop.
do { // body of the loop.. }while(1);
The above do..while loop represents the infinite condition as we provide the '1' value inside the loop condition. As we already know that non-zero integer represents the true condition, so this loop will run infinite times.
goto statement
We can also use the goto statement to define the infinite loop.
infinite_loop; // body statements. goto infinite_loop;
In the above code, the goto statement transfers the control to the infinite loop.
Macros
We can also create the infinite loop with the help of a macro constant. Let's understand through an example.
#include <stdio.h> #define infinite for(;;) int main() { infinite { printf("hello"); } return 0; }
In the above code, we have defined a macro named as 'infinite', and its value is 'for(;;)'. Whenever the word 'infinite' comes in a program then it will be replaced with a 'for(;;)'.
Output
Till now, we have seen various ways to define an infinite loop. However, we need some approach to come out of the infinite loop. In order to come out of the infinite loop, we can use the break statement.
Let's understand through an example.
#include <stdio.h> int main() { char ch; while(1) { ch=getchar(); if(ch=='n') { break; } printf("hello"); } return 0; }
In the above code, we have defined the while loop, which will execute an infinite number of times until we press the key 'n'. We have added the 'if' statement inside the while loop. The 'if' statement contains the break keyword, and the break keyword brings control out of the loop.
Unintentional infinite loops
Sometimes the situation arises where unintentional infinite loops occur due to the bug in the code. If we are the beginners, then it becomes very difficult to trace them. Below are some measures to trace an unintentional infinite loop:
#include <stdio.h> int main() { int i=1; while(i<=10); { printf("%d", i); i++; } return 0; }
In the above code, we put the semicolon after the condition of the while loop which leads to the infinite loop. Due to this semicolon, the internal body of the while loop will not execute.
#include <stdio.h> int main() { char ch='n'; while(ch='y') { printf("hello"); } return 0; }
In the above code, we use the assignment operator (ch='y') which leads to the execution of loop infinite number of times.
#include <stdio.h> int main() { for(int i=1;i>=1;i++) { printf("hello"); } return 0; }
The above code will execute the 'for loop' infinite number of times. As we put the condition (i>=1), which will always be true for every condition, it means that "hello" will be printed infinitely.
#include <stdio.h> int main() { while(1) { for(int i=1;i<=10;i++) { if(i%2==0) { break; } } } return 0; }
In the above code, the while loop will be executed an infinite number of times as we use the break keyword in an inner loop. This break keyword will bring the control out of the inner loop, not from the outer loop.
#include <stdio.h> int main() { float x = 3.0; while (x != 4.0) { printf("x = %f\n", x); x += 0.1; } return 0; }
In the above code, the loop will run infinite times as the computer represents a floating-point value as a real value. The computer will represent the value of 4.0 as 3.999999 or 4.000001, so the condition (x !=4.0) will never be false. The solution to this problem is to write the condition as (k<=4.0).