Infinite for loop in C Language
In this tutorial section we will learn what is Infinite for loop in C Language?.
What is Infinite for loop in C?
Infinite for loop: Infinite for loop in C Programming is refers to a for loop that never terminates or ends and repeats indefinitely.
Or
In the for loop the expressions initialization, condition, and updation statements are optional whereas in the case of the do while and while loop the condition expression was mandatory. If we leave this expression as empty,then the for loop will execute for infinite time and work as infinite loop.
Following are some real examples/use case of Nested for loop
e.g.
1. 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.
2. All modern sensors use infinite loop. e.g. Temperature Sensor,IR Sensor (Infrared Sensor),Pressure Sensor,Light Sensor,Smoke Sensor, Gas and Alcohol Sensor ,Humidity Sensor
3.All modern servers use infinite loop and responds to all clients request.
4. All modern Games use infinite loop to run. While user is loged in the game accept the user request.
5. Web cam: All web cam use infinite loop when pointing to display screen.
Advantage using infinite for loop in C programming
1. Infinite for loop is useful for time delay in c programming. It is infinite/endless loop which lacks exit condition option and thereby continues indefinitely.
2.The infinite loop is generated due to an error in the programm but the advantage is that they are useful in cases where program needs to be kept running for longer periods of time.
e.g.--> i. Webservers.
ii. Banking ATM machines.
3.Infinite loop have the advantages of continuous processing,flexibility and simplicity.
4. Infinite loop is used in the missile's guidance computer,the loop started does repeat until the end of time.
Disadvantage using Nested while loop in C programming
1. Infinite loop will run indefinitely and CPU will use its maximum resources due to this CPU get overheated.
2. Infinite loop have unpredictable behaviour in Programming.
3. Infinite for loop might create hardware problem.
4. Infinite loop sometime results in unexpected outcome or system breakdowns.
Given Diagram is the flowchart how infinite for loop works in c programming.
In this tutorial let us take the visual diagram flow chart for better understanding of infinite for loop.
This is the flow chart for showing how for and infinite for loop works in C programming.
Infinite for loop Working:
1.Initilization: The Flow chart start with the initialization step, where the initilization of counter variable is set.
The infinite for loop do not have the initilization of counter variable so control jumps to test Condition.
2. Condition: It is boolean expression. The expresion is evaluated before each iteration of for loop. If the boolean expression evaluated is true the body of for loop is executed.
In the infinite for loop the condition is not mentioned so the control enter in to the infinite for loop body.
Executes the code inside the loop.
After succesful execution of the statements inside the body of infinite for loop control jumps back to update section.
3.updation:This statement executed after each iteration of the loop. This statement updates the counter variable.
In the infinite for loop the updation is not mentioned so updation will not occures and then control jumps back to test condition .
As we know from diagram no condtion is in the infinite for loop.
The control enters in to the infinite for loop body and executes the rest of statements.
The process repeats infinite number of times.
Some Examples that lead to infinite for loop in C:
1.
short int x;
for (x = 32765; x < 32768; x++)
printf("%d\n", x);
}
The Example given above is an infinite for loop. According to the condition (x<32768), the loop will execute until the value of x is less than 32768 i.e.(x < 32768). Initially, the value of x is 32765 and after each iteration, its value is incremented by 1 by the update expression (x++). But we know the value of short int type ranges from -32768 to 32767. If you try to increment the value of x beyond 32767, it falls on the negative side and this process keeps repeating indefinitely. Hence the condition (x < 32768) will always be true.
2.
int i = 1;
while(i<10)
{
printf("%d\n", i);
}
Here we have not updating the value of i. So after each succesful iteration, the value of i remains the same. As a result, the condition (i<10) will always be true. The loop to work correctly add i++;, just after the printf() statement.
programmer easily forget update expression in while and do while loop as compared to the for loop.
C program to illustrate use of infinite for loop
/* C program given below displays "Wellcome to Sankalan Data Technology..!" */
#include <stdio.h>
int main()
{
printf("\n Infinite for loop Demonstration");
for(; ; )
{
printf("\n Wellcome to Sankalan Data Technology..!");
}
return(0);
}
Output:
Wellcome to Sankalan Data Technology..!
Wellcome to Sankalan Data Technology..!
Wellcome to Sankalan Data Technology..!
Wellcome to Sankalan Data Technology..!
Wellcome to Sankalan Data Technology..!
Wellcome to Sankalan Data Technology..!
Wellcome to Sankalan Data Technology..!
.............
........
.....
....
Explanation :
printf("\n Infinite for loop Demonstration");
Shows the message on console.
Infinite for loop Demonstration
Next control enters into the for(; ; ) loop and start processing initilization section but as in the loop do not have initilization section, So the control enters into the test condition .
The for loop do not have the test condition so control jumps in to the for loop body then executes the statements printf("\n Wellcome to Sankalan Data Technology..!");
which shows the output.
Wellcome to Sankalan Data Technology..!
After that control enters into the update section of the for loop , as there is no updation.
The control jumps to test condition again as we don't have the condition in the for loop, then control enter into the for loop body and executes the statement printf("\n Wellcome to Sankalan Data Technology..!");
which shows the output.
Wellcome to Sankalan Data Technology..!
The process repeate infinite number of time.........
System may crash while execution of infinite loop.