I use while true loop in many projects. It serves me well and it's not over-complicated. So, why it is a bad idea to use while true loop infinitely running background processes. Thanks for your answers.
-
4`while (true)` is not inherently bad practice. Certain uses of `while (true)` are bad practice, mostly busy-waits where other kinds of waits would be more appropriate, but there's nothing wrong with `while (true)` itself. – user2357112 Apr 15 '15 at 09:20
-
2Why do you say it's a bad idea? – user2357112 Apr 15 '15 at 09:21
-
As long as you know what you're doing and you have a clean way of getting out of it, it's not a bad practice. – Morb Apr 15 '15 at 09:22
-
What gave you the idea that's it's bad in the first place? – deceze Apr 15 '15 at 09:23
-
My project leader doesn't allow this kind of loop in my code, he's arguing that it has to be replaced by while(!exit_flag) – Panchito91 Apr 15 '15 at 09:26
-
It is done to keep the code clean. You can read my answer and comment on it. – Alexey Apr 15 '15 at 09:28
3 Answers
My project leader doesn't allow this kind of loop in my code, he's arguing that it has to be replaced by
while(!exit_flag)
That's very debatable. while (!exit_flag)
suggests that the loop has a natural exit condition which it will reach at some specific point by itself, e.g. something counting from 0 to 100 and then leaving the loop. However, if you are writing this loop to create a permanent daemon or event loop, then the point of this loop is to keep the program running indefinitely. Nothing says indefinitely better than while (true)
.
There's no real technical difference between both; it's just a matter of readability and expression of intent. Of course, you'll have to square this with the people who will read that code in the end. If their policy is while (!exit_flag)
, so be it.

- 510,633
- 85
- 743
- 889
It is not a bad practice, it just means that you did not think your code through.
The condition is required to tell the loop when to finish looping. If you have a terminating point, then why not put it as a condition?
var i = 0;
while(true){
i++;
if(i == 100)
break;
}
This is fine code. But so is this:
var i = 0;
while(i != 100){
i++;
}
Both methods are correct, but if someone else is reading your code, it is much easier to see when the loop will stop iterating if it is right there in the condition.

- 3,607
- 8
- 34
- 54
Hm, ask the one who claimed it was "a bad idea".
What I could think of is, that any loop running indefinitely must come to an end at some point, unless, of course, you expect your program to run forever.
If while is not given any criteria to terminate, there would need to be some other kind of mechanism to terminate the loop hidden somewehere inside the looping code, like return, break or (yuck - Basic - but that's probably where the statement origins - a "Goto" statement jumping somewehere outside the loop) to terminate the loop.
Code like this may be hard to read and debug, and formerly was an indication that a programmer did not care much about writing clean code. Nevertheless nowadays with event driven applications and structured error handling (see try ... catch structures) there are clean and easy to read ways to exit any kind of loop any time, so while (true) can be used without hassles.
Greetz
ALina

- 75
- 7