The do { } while(0)
construct in that context, is an obfuscated way to write goto's without actually writing it out. It is used by cargo cult programmers who somewhere read that gotos are bad but didn't understand why.
Here the example of Dariusz' answer rewritten with a proper goto
and a meaningfull label.
beginAtomicOperationSequence();
ret = doSomething();
if (ret < 0) goto handle_error;
ret = doSomething2();
if (ret < 0) goto handle_error;
...
handle_error:
if (ret < 0) {
switch (ret) {
//handle error
}
rollbackAboveOperations();
} else {
commitAboveOperations();
}
As can be seen it is shorter and completely obvious. That you even had to ask for the purpose of that construct shows that it is a bad idea.
I am vehement in my answer, because I have a colleague who uses and abuses it in our project and I haven't found one redeeming value to it. It gets even more annoying when you add some real loops in the mix. Then the fun starts to find where the break
and continue
will lead you to.