James R. Twine wrote:
An excellent explaination! However, I will offer one more use of the do/while(0) block. I have seen it used as a way to provide goto-like functionality without actually using the word goto. For example, if you needed to jump out of the middle of the block, simply executing a break will take you to the bottom of the block, simulating a goto to a label at the end of the block. It was a bit confusing the first time I saw code that used the do/while(0) like that...
People that code like that do so for job-security. You should NEVER do that. If you actually look at the assembly code produced by the compiler when you write a do-while(0) with a break instead of just using goto, you'll notice it is exactly the same as when you do write a goto. Although, your reasoning does offer another good reason for making this a function instead of a macro. Instead of a goto or break, you just return if you get to a state that needs such. Much cleaner, and you won't have someone asking you what the heck you are doing when they have to maintain your code. If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac