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
193
votes
6 answers

How to create an infinite loop in Windows batch file?

This is basically what I want in a batch file. I want to be able to re-run "Do Stuff" whenever I press any key to go past the "Pause". while(true){ Do Stuff Pause } Looks like there are only for loops available and no while loops in…
sooprise
  • 22,657
  • 67
  • 188
  • 276
185
votes
21 answers

Is "for(;;)" faster than "while (true)"? If not, why do people use it?

for (;;) { //Something to be done repeatedly } I have seen this sort of thing used a lot, but I think it is rather strange... Wouldn't it be much clearer to say while(true), or something along those lines? I'm guessing that (as is the reason…
Chris Cooper
  • 17,276
  • 9
  • 52
  • 70
165
votes
9 answers

Optimizing away a "while(1);" in C++0x

Updated, see below! I have heard and read that C++0x allows an compiler to print "Hello" for the following snippet #include int main() { while(1) ; std::cout << "Hello" << std::endl; } It apparently has something to do with…
Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
153
votes
7 answers

Is there an expression for an infinite iterator?

Is there a straight-forward expression that can produce an infinite iterator? This is a purely theoretical question. No need for a "practical" answer here :) For example, it is easy to use a generator expression to make a finite iterator: my_gen =…
hugomg
  • 68,213
  • 24
  • 160
  • 246
129
votes
18 answers

How can I change the EditText text without triggering the Text Watcher?

I have an EditText field with a Customer Text Watcher on it. In a piece of code I need to change the value in the EditText which I do using .setText("whatever"). The problem is as soon as I make that change the afterTextChanged method gets called…
user1143767
  • 1,461
  • 2
  • 13
  • 13
117
votes
20 answers

Which is the correct C# infinite loop, for (;;) or while (true)?

Back in my C/C++ days, coding an "infinite loop" as while (true) felt more natural and seemed more obvious to me as opposed to for (;;) An encounter with PC-lint in the late 1980's and subsequent best practices discussions broke me of this habit.…
Bob Kaufman
  • 12,864
  • 16
  • 78
  • 107
102
votes
8 answers

How to run a script forever?

I need to run my Python program forever in an infinite loop.. Currently I am running it like this - #!/usr/bin/python import time # some python code that I want # to keep on running # Is this the right way to run the python program forever? #…
user2467545
93
votes
6 answers

Seemingly endless loop terminates, unless System.out.println is used

I had a simple bit of code that was supposed to be an endless loop since x will always be growing and will always remain larger than j. int x = 5; int y = 9; for (int j = 0; j < x; j++) { x = x + y; } System.out.println(y); but as is, it prints…
Omar
  • 1,081
  • 1
  • 9
  • 14
82
votes
12 answers

Endless loop in C/C++

There are several possibilities to do an endless loop, here are a few I would choose: for(;;) {} while(1) {} / while(true) {} do {} while(1) / do {} while(true) Is there a certain form which one should choose? And do modern compilers make a…
magu_
  • 4,766
  • 3
  • 45
  • 79
75
votes
6 answers

Why isn't setTimeout cancelling my loop?

I wondered how many times can a JavaScript while statement (in Chrome's console) can increment a variable in a millisecond, so I quickly wrote this snippet directly into console: var run = true, i = 0; setTimeout(function(){ run = false; },…
rev
  • 1,861
  • 17
  • 27
66
votes
6 answers

How is a StackOverflowException detected?

TL;TR When I asked the question I assumed a StackOverflowException is a mechanism to prevent applications to run infinitely. This is not true. A StackOverflowException is not being detected. It is thrown when the stack does not have the capacity to…
Noel Widmer
  • 4,444
  • 9
  • 45
  • 69
65
votes
2 answers

Infinite loop breaks method signature without compilation error

I am wondering why is the following code allowed in Java, without getting compilation error? In my opinion, this code breaks method signature by not returning any String. Could someone explain what I'm missing here? public class Loop { private…
Simo Martomaa
  • 526
  • 4
  • 9
64
votes
3 answers

Why does this method result in an infinite loop?

One of my coworkers came to me with a question about this method that results in an infinite loop. The actual code is a bit too involved to post here, but essentially the problem boils down to this: private IEnumerable GoNuts(IEnumerable
Jim Mischel
  • 131,090
  • 20
  • 188
  • 351
63
votes
11 answers

CPU friendly infinite loop

Writing an infinite loop is simple: while(true){ //add whatever break condition here } But this will trash the CPU performance. This execution thread will take as much as possible from CPU's power. What is the best way to lower the impact on…
Adi
  • 5,113
  • 6
  • 46
  • 59
58
votes
6 answers

Ending an infinite while loop

I currently have code that basically runs an infinite while loop to collect data from users. Constantly updating dictionaries/lists based on the contents of a text file. For reference: while (True): IDs2=UpdatePoints(value,IDs2) …
Brian HK
  • 860
  • 1
  • 8
  • 18
1
2 3
99 100