And it's not like I don't use 'while' -- in a bash script it looks normal to me -- it's just I would never even think to use one in C or C++. And when I read it it looks weird to me.
- 'for' is simply my only looping construct. Now that C++ supports 'for(auto x : some_container) x.blahblah();' this is especially true.
- the old 'for(init; test; increment)' syntax put all your control right up front so as you read the code you already have in mind the range of when the body will run. A 'while(not_exit) { ... }' doesn't tell you that you're, say striding over the even elements or whatnot -- it's really just the equivalent of an open loop with an 'if(...) break;' somewhere in the body. And let's not get into the botch of 'do..while'
I'd be very surprised if there was any difference in terms of the generated code. Personally I use while(true).
A strange choice by the committee I'd be interested in reading more about.
Coming from an embedded background, the form "while (true) { // do nothing of interest }" is seen a lot in embedded code. If your code is interrupt driven, or running certain RTOSes, you would usually see this at the end of main(), since everything happens in interrupts. You might also see this in error handlers, and "unimplemented" interrupt handlers during development, as a way to trap the program without halting / resetting the system completely. If this kind of loop was optimized out / you returned-from-main on your embedded system, interesting and unintended things may happen.
While I've never had problems with these particular loops being optimized out (that I can remember!), I do remember having to do other vary silly hacks to try and tell the compiler not to optimize things out, because something is happening in hardware that it wasn't aware of, so I wonder if this is just a practical way of making sure that this kind of code keeps working with newer compilers, even in the face of aggressive optimization.
LLVM has since added a new intrinsic: https://reviews.llvm.org/D38336
This will allow us to fix the bug.
Example: https://godbolt.org/g/md86qX
while(true) // infinite loop! Red flag!
repeat // I have no idea what this will do.
Likewise do...while is in retrospect, a mistake, because it documents the control strategy in a bad place from a humans-understanding-code point of view.
Thanks gumby.
I simply didn't catch that they were referring only to infinite loops. I though they were debating the usefulness of while versus for, in general.