C++ Infinite loop
The term "infinite loop" refers to a loop that does not terminate the loop according to the condition. In some cases, an infinite loop may be required in programming, or a loop may become infinite when the exit condition becomes false again and again.
Applications
An infinite loop is useful for programs that receive user input and continuously generate output until the user exits the program manually.
The infinite loop can be utilized in the following scenarios:
1) If you're developing an application that allows the user to submit the long data having the loop 30,000 or 300,000,000 times. The code run infinitely and receiving the user input until the user presses Ctrl-C to terminate the application.
2) If you have a constant monitoring operation running on your servers.
3) As the server answers all client requests, it runs in an eternal loop. It breaks the endless cycle when the administrator manually shuts down the server.
4) When your server script listens for connections on a socket, this could be a useful feature.
5) Infinite loops are frequently used in video game programming.
There are various loop structures in C that can be used to produce an infinite loop. Following are the looping structures that define the endless loop:
- For loop
- While loop
- Do-While loop
Infinite For Loop
A condition in for loop terminates the loop. The condition can be left blank if we wanted. We may also remove the initialization and repeat statements from the for loop. These fields are optional and can be left empty. Because the condition will never be false, the control will never exit the loop, which produce an infinite loop.
For loop syntax
for (
<initial statement(s)>;
<Condition expression>;
<Repeat step(s)>
)
{
<Loop statement(s)>;
}
Infinite For loop:
for(;;);
for(;;)
{
}
Infinite For Loop with True for Condition
For loop uses a boolean expression that evaluates the condition to be true or false. Instead of giving an expression, we can substitute the boolean value “true” for the condition which results an infinite loop.
Program:
#include <iostream>
using namespace std;
int main()
{
for (; true; )
{
cout << "javaTpoint" << endl;
}
}
Output:
javaTpoint
javaTpoint
javaTpoint
javaTpoint
javaTpoint
..
..
..
The string ‘’javaTpoint” will print at a time on the console infinitely. To terminate the execution of the program press CTRL and C together on the keyboard.
Infinite while loop in C
There is a condition statement in the while loop. This is a required expression that cannot be omitted. As a result, we must provide a true condition here. "TRUE", "1", "1==1", and so on are the TRUE statements.
While loop syntax
while (<Condition expression>)
{
<Loop statement(s)>;
}
Infinite While loop
while (1);
while (1)
{
}
Infinite While Loop with TRUE condition
You can use a condition that always evaluates to true. But, you cannot use a TRUE boolean value in the condition of the while loop for infinite loop. The criterion 1 == 1 or 0 == 0 is always true.
The condition is always true and the loop will While loop run infinitely.
Program:
#include <iostream>
using namespace std;
int main()
{
While(1==1)
{
cout << "javaTpoint" << endl;
}
}
Output:
javaTpoint
javaTpoint
javaTpoint
javaTpoint
javaTpoint
..
..
The string “javaTpoint” prints one by one infinitely as the given condition is always true. To terminate the execution of the program press CTRL+C on the keyboard.
Infinite Do-While loop:
There is a condition statement in the while loop. This is a required expression that cannot be omitted. As a result, we must provide a true condition here. "TRUE", "1", "1==1", and so on are the TRUE statements.
Do-While loop syntax
do
{
<Loop statement(s)>;
} while (<Condition expression>);
Infinite Do-While loop:
do {
} while(1);
Infinite do-while Loop with No Update to Control Variables
When you forget to update the variables involved in the condition, you can end up with infinite loops.
We have set variable “i” to 0 in the below example and want to print a string to the console until the value of i becomes 10.
In the example below, we would not use the update statement to increment ”i” for printing javaTpoint 10 times. So, the value of i would never change and the code will run infinitely with value of i. This result in an infinite do-while loop.
Program:
#include <iostream>
using namespace std;
int main()
{
i=0;
do
{
cout << "javaTpoint" << endl;
}while(i<10)
}
Output:
javaTpoint
javaTpoint
javaTpoint
javaTpoint
javaTpoint
..
..
Long Running Loops
It's recommended to add some delay to each loop. Otherwise, the process may consume all CPU cycles and cause the system to freeze. This may be required when the programmer is waiting for a condition to occur. A sleep statement releases the task to the scheduler and allowing other tasks to continue normally. This statement causes the current thread to suspend execution for a specified period.
For Loop;
for(;;)
{
Sleep(1);
}
While Loop:
while(1)
{
Sleep(1);
}
Do-While Loop:
do
{
Sleep(1);
}
while(1);
Debug infinite loops
A program enters an infinite loop and continue to execute the body of the loop. This type of problem may not occur very often and may occur due to a rare instance. On the other hand, these problems are simple to troubleshoot or debug.
We should write the break statement for checking the condition from where the loop become infinite. To get to the root of the problem, we need to figure out why the condition isn't becoming false.