What are some better ways to avoid the do-while(0); hack in C++?

c++, do-while

Solution

It is considered acceptable practice to isolate these decisions in a function and use `return`s instead of `break`s. While all these checks correspond to the same level of abstraction as of the function, it is quite logical approach.

For example:

void foo(...)
{
   if (!condition)
   {
      return;
   }
   ...
   if (!other condition)
   {
      return;
   }
   ...
   if (!another condition)
   {
      return;
   }
   ... 
   if (!yet another condition)
   {
      return;
   }
   ...
   // Some unconditional stuff       
}

Problem

When the code flow is like this: ``` if(check()) { ... ... if(check()) { ... ... if(check()) { ... ... } } } ``` I have generally seen this work around to avoid the above messy code flow: ``` do { if(!check()) break; ... ... if(!check()) break; ... ... if(!check()) break; ... ... } while(0); ``` What are some better ways that avoid this work-around/hack so that it becomes a higher-level (industry level) code? Any suggestions which are out of the box are welcome!

Original source

Related problems