C++ Infinite Loops: Why Do They Occur?

  • Context: C/C++ 
  • Thread starter Thread starter ineedhelpnow
  • Start date Start date
  • Tags Tags
    C++ Infinite Loops
Click For Summary

Discussion Overview

The discussion revolves around the causes and implications of infinite loops in C++ programming. Participants explore various scenarios that can lead to infinite loops, including programming constructs and conditions that prevent termination.

Discussion Character

  • Exploratory
  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • Some participants suggest that infinite loops occur primarily because the continuation condition remains true indefinitely.
  • Others argue that there are multiple reasons for infinite loops, including skipping conditional statements, failure to reach a break condition, and waiting for events that may never happen.
  • A participant provides examples illustrating how specific coding practices can lead to infinite loops, such as incrementing a variable incorrectly or relying on conditions that may not be met.
  • Another participant notes that infinite loops can be intentionally used in certain applications, such as animations or server sockets, where continuous execution is desired until an external condition is met.

Areas of Agreement / Disagreement

Participants express differing views on the causes of infinite loops, with some focusing on the logical conditions while others highlight practical coding scenarios. The discussion remains unresolved regarding the completeness of the reasons provided for infinite loops.

Contextual Notes

Some examples provided by participants depend on specific programming constructs and may not universally apply to all coding situations. The discussion also reflects varying levels of understanding among participants regarding the implications of infinite loops.

ineedhelpnow
Messages
649
Reaction score
0
why do infinite loops occur? is it just because the condition is always true?
 
Technology news on Phys.org
Well, yes. Otherwise.. the loop would terminate.

...
 
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)
 
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)

What other reason could there be? Every loop construct repeats for as long as its termination condition is false, or, equivalently, the continuation condition is true (the converse is not true because you can break out of a loop early with e.g. "break" or even "goto", but you cannot "continue" a loop if the continuation condition has become false - well, you could goto back to the start, but that's abusing the loop construct).

Now an infinite loop occurs if and only if it never terminates, that is, always continues, which is thus logically equivalent to the continuation condition always remaining true. There is no other possibility.



That said, in a less abstract sense, some people refer to as "infinite loops" any construct of the form:

Code:
while (true)
{
    /* do some work here */

    if (condition1)
        break;

    /* more work */

    if (condition2)
        break;

    /* etc. */
}

That is, it's not really infinite, but it looks infinite, and it let's the programmer run a loop for an arbitrary amount of time with more flexibility than a single termination condition offers. Those are especially common in parsing code for some grammars and other state-machine-style algorithms.
 
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.
 
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.

makes sense. if i could thank this post a million times, i would. although i haven't got to the 'i' stuff in my homework yet, i get what you're saying. (Bigsmile)
 

Similar threads

Replies
18
Views
3K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 11 ·
Replies
11
Views
1K
  • · Replies 29 ·
Replies
29
Views
10K
  • · Replies 16 ·
Replies
16
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 12 ·
Replies
12
Views
5K
  • · Replies 17 ·
Replies
17
Views
3K