- #1
ineedhelpnow
- 651
- 0
why do infinite loops occur? is it just because the condition is always true?
ineedhelpnow said:that's the only reason? because my teacher was all like "make sure you understand infinite loops! it's very critical that you understand why they happen!"...(Dull)
while (true)
{
/* do some work here */
if (condition1)
break;
/* more work */
if (condition2)
break;
/* etc. */
}
i=0;
while ( i != 9)
doSomething();
i+=2;
end while
while(true)
x = approximateRoot();
if(abs(x-root) < 1e-n)
break;
end if
end while
while(true)
int flag = listenPort( portNumber);
if(flag == 1)
break;
end if;
end while
//moving a ball in a coordinate
while(true)
paintBall(++x , ++y);
Thread.Sleep (Time.milliseconds);
end while
ZaidAlyafey said:It is always the programmer work to avoid infinite loops. You have to make the condition as precise as possible so that you are not stuck. If the algorithm requires a finite number of steps you have to make sure that it terminates. An infinite loop can occur for many reasons :
1. You skip the conditional statement by incrementing more than one
Code:i=0; while ( i != 9) doSomething(); i+=2; end while
As we see from the example the i's are even so the conditional statement will never occur.
2. The break never occurs
Sometimes you don't know what are the maximum number of iterations to terminate. Fore example
Code:while(true) x = approximateRoot(); if(abs(x-root) < 1e-n) break; end if end while
As we see from the example the loop approximates the root of a certain function and compares with the real root. If the absolute error is small enough we terminate. But what happens if the function to approximate the root never gets close to the real root. The loop will never terminate because the sequence will diverge.
3. Waiting for something that never happens
Especially in internet related applications you create a socket to listen to a port waiting for a certain port to wait for clients.
Code:while(true) int flag = listenPort( portNumber); if(flag == 1) break; end if; end while
The loop runs for an undetermined number of iterations and waits for the client to connect then breaks. Hence that provides a useful application.
4. Loops that never terminate upon execution
Sometimes you want a loop that always runs until we close the application.
Code://moving a ball in a coordinate while(true) paintBall(++x , ++y); Thread.Sleep (Time.milliseconds); end while
Infinite loops are great when creating animations and games. A continuously running thread to handle a certain animating object will run upon executing the program. The ball will move for a certain number of pixels then waits for a certain time , after that it continues moving. This gives impression of the ball moving smoothly.
An infinite loop in C++ is a loop that continues to execute indefinitely without ever ending. This means that the loop's condition is always true, causing it to never terminate.
Infinite loops can occur in C++ due to a mistake in the code, such as forgetting to update the loop's condition or accidentally creating a condition that is always true. It can also happen intentionally in certain programming situations.
An infinite loop can cause a program to become unresponsive and crash if it is not terminated. It can also consume a lot of system resources, slowing down the overall performance of the program.
To prevent an infinite loop, you should ensure that the loop's condition will eventually become false, or include a break statement to exit the loop. It is also helpful to thoroughly test your code and double check your loop conditions.
The best way to debug an infinite loop is to use a debugger tool to step through your code and track the values of variables and conditions within the loop. You can also add print statements to see where the loop is getting stuck. Additionally, using a code review or asking for help from a colleague can also help identify the cause of the infinite loop.