Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Infinite Loop : Determining and breaking out of Infinite loop

How would you determine a loop is a infinite loop and will break out of it.

Does anyone has the algorithm or can assist me on this one.

Thanks

like image 212
Pankaj Gupta Avatar asked Dec 12 '22 04:12

Pankaj Gupta


1 Answers

There is no general case algorithm that can determine if a program is in an infinite loop or not for every turing complete language, this is basically the Halting Problem.

The idea of proving it is simple:

  1. Assume you had such an algorithm A.
  2. Build a program B that invokes A on itself [on B].
  3. if A answers "the program will halt" - do an infinite loop
  4. else [A answers B doesn't halt] - halt immidiately

Now, assume you invoke A on B - the answer will be definetly wrong, thus A doesn't exist.

Note: the above is NOT a formal proof, just a sketch of it.

like image 158
amit Avatar answered Feb 23 '23 05:02

amit