In many C/C++ macros I\'m seeing the code of the macro wrapped in what seems like a meaningless do while loop. Here are examples.
#define FOO(X
The above answers explain the meaning of these constructs, but there is a significant difference between the two that was not mentioned. In fact, there is a reason to prefer the do ... while to the if ... else construct.
The problem of the if ... else construct is that it does not force you to put the semicolon. Like in this code:
FOO(1)
printf("abc");
Although we left out the semicolon (by mistake), the code will expand to
if (1) { f(X); g(X); } else
printf("abc");
and will silently compile (although some compilers may issue a warning for unreachable code). But the printf statement will never be executed.
do ... while construct does not have such problem, since the only valid token after the while(0) is a semicolon.