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)
This is a discussion on do while false within the C++ Programming forums, part of the General Programming Boards category; I have been tasked with revamping some old code, and there is the construct 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 ...
Manasij Mukherjee | gcc-4.8.0 @Arch Linux
Slow and Steady wins the race... if and only if :
1.None of the other participants are fast and steady.
2.The fast and unsteady suddenly falls asleep while running !
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.
I support http://www.ukip.org/ as the first necessary step to a free Europe.
....o..sorry....
Manasij Mukherjee | gcc-4.8.0 @Arch Linux
Slow and Steady wins the race... if and only if :
1.None of the other participants are fast and steady.
2.The fast and unsteady suddenly falls asleep while running !
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.
I support http://www.ukip.org/ as the first necessary step to a free Europe.
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"