I have been tasked with revamping some old code, and there is the construct
all over the place. is there any utility to this at all, or is this person simply a nutjob?Code:do { //stuff } while(false)
I have been tasked with revamping some old code, and there is the construct
all over the place. is there any utility to this at all, or is this person simply a nutjob?Code:do { //stuff } while(false)
Well...<//stuff> should never get executed..
MAYBE he put it there as extra code.....i.e to add if required later ...
Within the loop, are there any things like this?
If the loop has no break statements, then it seems fairly pointless.Code:if ( error ) break;
Also, multi-line macros are often written as such loops, so they can be used in the context of being a single statement (note the lack of a final ; )
Eg.
> Well...<//stuff> should never get executed..Code:#define FOO(x,y) do { \ printf("%d\n", x ); \ printf("%d\n", y ); \ while ( 0 )
Actually, stuff gets executed exactly once - the test (which always fails) is at the end of the loop.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
the difference between while and do...while is the do block is always executed at least once.
actually googling this thread title just turned up some articles on this structure, and there are some defenses of it (he uses 1 of the example cases, but mostly it seems useless).
Hmph, I get that feeling every day
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
If it's inside a #define then it's actually someone's use of the smartest way to wrap multi-line #defines. If not, then it's probably used as a glorified goto.
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"