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)
Printable View
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.
....o..sorry....
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 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.