infinite for loop in C Programming

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.

Previous Topic:-->> Infinite while loop in C || Next topic:-->>Continue in for loop in C.