Questions tagged [infinite-loop]

An "infinite loop" is a loop in which the exit criteria are never satisfied; such a loop would perform a potentially infinite number of iterations of the loop body. The general problem of determining whether the execution of a loop with given preconditions will result in an infinite loop is undecidable; in other words, there is no algorithm to determine whether an execution of a loop will eventually terminate. This is known as the halting problem.

3528 questions
34
votes
2 answers

while(true) versus for(;;)

Possible Duplicates: Is “for(;;)” faster than “while (TRUE)”? If not, why do people use it? for ( ; ; ) or while ( true ) - Which is the Correct C# Infinite Loop? Is there any appreciable difference between while(true) (or while(1)) and for(;;)?…
kylan
  • 513
  • 1
  • 4
  • 8
34
votes
7 answers

Php for loop with 2 variables?

is it possible to do this? (here is my code) for ($i = 0 ; $i <= 10 ; $i++){ for ($j = 10 ; $j >= 0 ; $j--){ echo "Var " . $i . " is " . $k . "
"; } } I want something like this: var 0 is 10 var 1 is 9 var 2 is 8 ... But my code is…
jingleboy99
  • 705
  • 3
  • 9
  • 16
29
votes
4 answers

How to unit test a method that runs into an infinite loop for some input?

This question just occurred to my mind and I want to ask this here. The case is intentional, I just write a loop which runs infinitely. How do I go about unit testing it? I ask this because, this situation may occur anywhere in the code. Say my…
Shankar Raju
  • 4,356
  • 6
  • 33
  • 52
29
votes
4 answers

Infinite loop with cin when typing string while a number is expected

In the following loop, if we type characters as the cin input instead of numbers which are expected, then it goes into infinite loop. Could anyone please explain to me why this occurs? When we use cin, if the input is not a number, then are there…
chanwcom
  • 4,420
  • 8
  • 37
  • 49
29
votes
9 answers

Control Break out of Infinite Loop In 2010 (2013) Excel VBA

If I write code creating an infinite loop, with my new Excel, the Ctrl + Break no longer works. Neither does the Esc key, etc. I've looked all over the web and it appears that Microsoft has a bug and doesn't care to fix it. Is there a way to…
Tommy Z
  • 649
  • 5
  • 12
  • 18
28
votes
5 answers

How to handle an "infinite" IEnumerable?

A trivial example of an "infinite" IEnumerable would be IEnumerable Numbers() { int i=0; while(true) { yield return unchecked(i++); } } I know, that foreach(int i in Numbers().Take(10)) { Console.WriteLine(i); } and var q =…
Danvil
  • 22,240
  • 19
  • 65
  • 88
28
votes
3 answers

Does endless While loop take up CPU resources?

From what I understand, you write your Linux Daemon that listens to a request in an endless loop. Something like.. int main() { while(1) { //do something... } } ref: http://www.thegeekstuff.com/2012/02/c-daemon-process/ I read that…
resting
  • 16,287
  • 16
  • 59
  • 90
27
votes
11 answers

What's the difference between "while 1" and "while True"?

I've seen two ways to create an infinite loop in Python: while 1: do_something() while True: do_something() Is there any difference between these? Is one more pythonic than the other?
25
votes
9 answers

Detecting infinite loop in brainfuck program

I have written a simple brainfuck interpreter in MATLAB script language. It is fed random bf programs to execute (as part of a genetic algorithm project). The problem I face is, the program turns out to have an infinite loop in a sizeable number of…
Sundar R
  • 13,776
  • 6
  • 49
  • 76
25
votes
6 answers

How to stop an infinite loop safely in Python?

I've got a script that runs an infinite loop and adds things to a database and does things that I can't just stop halfway through, so I can't just press Ctrl+C and stop it. I want to be able to somehow stop a while loop, but let it finish it's last…
davegri
  • 2,206
  • 2
  • 26
  • 45
24
votes
5 answers

Debugging infinite loops in Haskell programs with GHCi

For the first time I've encountered an infinite loop in a Haskell program I'm writing. I've narrowed it down to a quite specific section of code, but I cannot seem to pinpoint exactly where I have a non-terminating recursive definition. I'm vaguely…
gspr
  • 11,144
  • 3
  • 41
  • 74
24
votes
11 answers

Back and forth loop Python

I want to create an infinite loop that counts up and down from 0 to 100 to 0 (and so on) and only stops when some convergence criterion inside the loop is met, so basically something like this: for i in range(0, infinity): for j in range(0, 100,…
Daniel
  • 430
  • 1
  • 4
  • 12
24
votes
2 answers

Finding where <> happened

If we get a <>, it means that Haskell had managed to detect an infinite loop. Is there a way to get ghc to tell us where this loop happened? It seems that Haskell should have this information somewhere.
yong
  • 3,583
  • 16
  • 32
24
votes
7 answers

How to terminate a program running from the Sublime Text 2 terminal once entering an infinite loop?

Even If I close sublime the process continues to go, seeing as my laptop revs until slowing down to a crawl a few minutes later. I'm on mint linux, and can't seem to kill the process from terminal either.
Aerlusch
  • 517
  • 3
  • 6
  • 11
24
votes
12 answers

How to interrupt an Infinite Loop

Though I know it'll be a bit silly to ask, still I want to inquire more about the technical perspective of it. A simple example of an infinite loop: public class LoopInfinite { public static void main(String[] args) { for (;;) { …
big zero
  • 610
  • 2
  • 8
  • 16