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
56
votes
8 answers

Cancel infinite loop execution in jsfiddle

When you get an infinite loop in jsfiddle in Chrome, your only choice (that I know of) is to close the tab. Of course, this means you lose all your work in the current window! Is there an easy way to stop an infinitely executing script? I have…
mellamokb
  • 56,094
  • 12
  • 110
  • 136
53
votes
2 answers

how do I create an infinite loop in JavaScript

I want to create an infinite loop in JavaScript. What are some ways to achieve this: eg for (var i=0; i
Elise Chant
  • 5,048
  • 3
  • 29
  • 36
44
votes
5 answers

How to handle infinite loop caused by invalid input (InputMismatchException) using Scanner

So, I'm getting stuck with this piece of code: import java.util.InputMismatchException; import java.util.Scanner; public class ConsoleReader { Scanner reader; public ConsoleReader() { reader = new Scanner(System.in); …
mateusmaso
  • 7,843
  • 6
  • 41
  • 54
44
votes
1 answer

What is the difference between while(true) and for(;;) in PHP?

Is there any difference in PHP between while(true) and for(;;) besides syntax and readability?
Alastair
  • 1,710
  • 2
  • 17
  • 33
44
votes
5 answers

Java: Infinite Loop Convention

What is the convention for an infinite loop in Java? Should I write while(true) or for(;;)? I personally would use while(true) because I use while loops less often.
Justin
  • 24,288
  • 12
  • 92
  • 142
43
votes
4 answers

Dictionary infinite loop is exiting unexpectedly

I was experimenting with various ways of creating an infinite loop in Python (other than the usual while True), and came up with this idea: x = {0: None} for i in x: del x[i] x[i+1] = None # Value doesn't matter, so I set it to None …
Suraj Kothari
  • 1,642
  • 13
  • 22
43
votes
8 answers

Read all lines with BufferedReader

I want to type a multiple line text into the console using a BufferedReader and when I hit "Enter" to find the sum of the length of the whole text. The problem is that it seems I'm getting into an infinite loop and when I press "Enter" the program…
deadpixels
  • 769
  • 1
  • 12
  • 21
42
votes
2 answers

Prolog successor notation yields incomplete result and infinite loop

I start to learn Prolog and first learnt about the successor notation. And this is where I find out about writing Peano axioms in Prolog. See page 12 of the PDF: sum(0, M, M). sum(s(N), M, s(K)) :- sum(N,M,K). prod(0,M,0). prod(s(N), M, P) :- …
41
votes
3 answers

How do I run long term (infinite) Python processes?

I've recently started experimenting with using Python for web development. So far I've had some success using Apache with mod_wsgi and the Django web framework for Python 2.7. However I have run into some issues with having processes constantly…
Hubro
  • 56,214
  • 69
  • 228
  • 381
41
votes
6 answers

Is it OK for a class constructor to block forever?

Let's say I have an object that provides some sort of functionality in an infinite loop. Is is acceptable to just put the infinite loop in the constructor? Example: class Server { public: Server() { for(;;) { //... …
CaptainCodeman
  • 1,951
  • 2
  • 20
  • 33
41
votes
6 answers

Are compilers allowed to eliminate infinite loops?

Can optimizing compiler delete infinite loops, which does not changes any data, like while(1) /* noop */; From analyzing a data flow graph compiler can derive, that such loop is "dead code" without any side effects. Is deleting of infinite loops…
osgx
  • 90,338
  • 53
  • 357
  • 513
39
votes
16 answers

Are infinite for loops possible in Python?

Is it possible to get an infinite loop in for loop? My guess is that there can be an infinite for loop in Python. I'd like to know this for future references.
Britni.M
  • 459
  • 1
  • 4
  • 6
35
votes
8 answers

Unexpected endless byte for loop

I have the following loop: for (byte i = 0 ; i < 128; i++) { System.out.println(i + 1 + " " + name); } When I execute my programm it prints all numbers from -128 to 127 in an infinite loop. Why does this happen?
DarkLeafyGreen
  • 69,338
  • 131
  • 383
  • 601
35
votes
2 answers

Strange behavior of Enumerator.MoveNext()

Could someone explain why this code is running in infinity loop? Why MoveNext() return true always? var x = new { TempList = new List { 1, 3, 6, 9 }.GetEnumerator() }; while (x.TempList.MoveNext()) { Console.WriteLine("Hello World"); }
ivamax9
  • 2,601
  • 24
  • 33
35
votes
4 answers

Is while(1); undefined behavior in C?

In C++11 is it Undefined Behavior, but is it the case in C that while(1); is Undefined Behavior?
Tony The Lion
  • 61,704
  • 67
  • 242
  • 415