Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Is `while(true)` preferable to `for(;;)` to get an infinite loop?

The compiler probably optimizes the condition check out, so I doubt it really matters.



When I tested this in the distant past, I found that all the compilers I tried generated exactly the same assembly for both loops.

I've seen it said that "The most optimizing compiler is the most normalizing compiler." [1] The infinite loop structure is pretty easy to normalize.

The only real difference that I saw was that some compiler front ends would produce warnings that the condition in the while loop was always true, where they seemed to assume the empty for must be intentional.

[1] https://www.cl.cam.ac.uk/techreports/UCAM-CL-TR-705.pdf


The compiler won't care unless you "#define true 0".

The "for(;;)" loop is easier and faster for the human to parse. It stands out in source code.

With the "while(true)", you have to pay attention. It could be "while(tnie)" or "while(trua)". Whenever you see it, you have to read carefully.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: