In this tutorial section we will learn what is Infinite while loop in C Language?.
What is Infinite while loop in C?
Infinite while loop: Infinite while loop in C Programming is refers to a while loop that never terminates or ends and repeats indefinitely.
Or
In Other words Infinite while loop in C programming is defined as a loop in which the test condition does not evaluate to false and the loop continues forever until an external break statement is issued explicitly.
In C programming language, a while loop is a controle flow statement or iterative statement that allows code to be executed repeatedly based on a given certain boolean condition.
Infinite while loop is useful if we want our part of the program code to run endlessly until a specific condtion is satisfied or met.
The infinite while loop can be used for time delay.Infinite while loop is an endless loop which do not have any exit condition and continues indefinitely.
Infinite loop is useful in cases where programmer needs to keep running continuously for long time period.
Following are some real examples/use case of Nested while loop
e.g.
1. Operating system: All operating system run infinitely till system does not shutdown manually.
2.Spelling checking in microsoft word.
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.
Infinite while loop in C Language
Advantage using infinite while loop in C programming
Following are some of advantage using infinite while loop in C programming.
1. Infinite while loop enables the rogram run continuously.
2. Infinite loop are used to run some code on regular basis.
Disadvantage using Nested while loop in C programming
Following are some of Disadvantage using infinite while loop in C programming.
1. More Resources: Infinite loop can be a problem in C programming because They utilize more resources indefinitely,which slow down the the performance or shut the program.
2. Another problem using infinite while loop is that it make difficult to debug a program.
3. An infinite while loop can crash program or freeze our computer.
Given Diagram is the flowchart how infinite while loop works in c programming.
"The condition is always true or 1 in the infinite while loop and never going to be False or 0, the control never comes out of the loop, and forms an Infinite Loop as shown in the the diagram."
Infinite while loop forms when a piece of code that lacks the exit condition so that it repeats indefinitely.
Let us understand infinite while loop from given diagram.
First, we Declare and initilize counter=1. Next The control jumps inside the the loops test condition, the condition is true i.e. counter==1 or true.
The loop’s entering condition is true.If the condition is true then we enter the loop and executes "C statements" present there.
Control jumps back to test condition counter==1 or true , our loop’s condition always remains true. Because of this effect, our loop runs to infinity.
This endless loop will only stop either when the application terminates or the system crashes.
Let us study real-world syntax of infinite while loop and then we will look detail into all parts of the infinte while loop.
a.
/* statements outside loop.
Declare and initilization of counter */
int counter=1;
while(counter==1)
{
/* Statements inside body of loop */
/ * Statements to be executed */
}
// statement outside loop
b.
/* statements outside loop.
Declare and initilization of counter */
while(true)
{
/* Statements inside body of loop
Statements to be executed */
}
// statement outside loop
Syntax Explanation :
a. The condition counter==1 inside the while(counter==1) is always true so the "Statements inside body of loop" executes infinite number of times and never stops as there is no exit condition mentioned .
it is always better to avoid using of infinite loop in programming.
b.The condition true inside the while(true) is also always true so the "Statements inside body of loop" executes infinite number of times and never stops as there is no exit condition mentioned .
C program to illustrate use of infinite while loop
/* C program given below displays "This is an infinite while Loop example" */
#include <stdio.h>
int main()
{
printf("\n Infinite While loop Demonstration");
while(1)
{
printf("\n This is an infinite while Loop example");
}
return(0);
}
Output:
Infinite While loop Demonstration
This is an infinite while Loop example
This is an infinite while Loop example
This is an infinite while Loop example
This is an infinite while Loop example
This is an infinite while Loop example
This is an infinite while Loop example
.............
........
.....
....
Explanation : printf("\n Infinite While loop Demonstration");
Shows the message on console.
Infinite While loop Demonstration
Next control enters into the while(1) loop and test the condition
i.e. while (1) which is true then control enters into the body of while loop and executes the
printf("\n This is an infinite while Loop example"); statement.
which shows the output "This is an infinite while Loop example" .
Next control jumps back to while(1) to test the condition and here the condition is true i.e while(1).
after that the statements inside while loop body
i.e. printf("\n This is an infinite while Loop example"); executes.
and the the lopp continues infinite times as the condition while(1) is always true.
The infinite while (1) loop may stops when the system crashes or the application terminates.
Previous Topic:-->> while loop in C || Next topic:-->>Difference Between while and do while loop in C.