php - What is the practical usage of Infinite loop : while(true)? - Stack Overflow
Do we ever do this with real life examples?
What is the fastest and best way to do an infinite loop inside the main loop?
Loops in real life
Videos
Few Use cases.
1) If you are writing a program to allow input data for as long as the user wants, it just would not work to have the script loop 30,000 times or even 300,000,000 times. Instead, the code should loop forever, constantly accepting user input until the user ends the program by pressing Ctrl-C.
2) If you have some background process monitoring your servers continuously.
3) Possibly a very good usage is when your server script is listening to a socket for connections.
4) Video Game programming use them heavily.
I differ from some of the above answers posted above which say that "infinite loops are useless".
http://devzone.zend.com/209/writing-socket-servers-in-php/ - An example of infinite loops been put to use.
Hey these are very essential in case you want to create a service kind of a thing, may be you want to monitor the server space ,i mean you want to see if there any new files added etc. you can then use such a loop and monitor 24*7.
I use it quite often in c++,in php too it can be used.
You know when you have options in your program and you have to to if-else argv argc stuff, and only THEN you actually go into the REAL infinite loop of your program.
How to do it? What is the best loop?
Someone once said that for(;;) is the best, some swear by while(1)
But then how do you (umm what's the word..) "exit gracefully" ? I've seen people having a variable like while (keep_looping), so they can get back to the main loop for cleanup and stuff and so they can return 0 or whatever other error code. But having a variable is obviously slower than just having 1 ? (what is even 1 in this case? a literal? either way it is definitely const and on the heap.)
Soo, what do you use? What should I use?