- Divine Skill Do{}while (False)
Do{}while (False) or do{}while (0), I learned in the Linux source, initially looked strange, but in the processing of continuous process is particularly useful, such as the ABC three process, a after the execution of a judgment condition if True and then execute B, If not false directly end the process, b after the execution of the same condition if true again to execute C, if not false directly end the process, finally execute C, if not the divine skill, deal with more trouble, the following is a way of writing
if (A ()) { if(B ()) { C (); } }
You can see the multi-layer nesting if, if the process is enough if nesting continues to increase, here is the divine technique
Do { if(! A ()) { break; } if ( ! B ()) { break; } C (); while false );
can see solved if nesting, and from code to conform to people's logical thinking
- Try{}catch, upgrade version Do{}while (FALSE)
Now there is a new demand, that is, after the process of ABC execution, regardless of the previous results are to execute process D, with Do{}while (FALSE) the following syntax
Do { if(! A ()) { break; } if ( ! B ()) { break; } while false );D();
In fact, the Try-catch function of high-level language can also achieve the effect of do{}while (false), use the following
Try { if(! A ()) { thrownew Exception (); } if ( ! B ()) { thrownew Exception (); } C ();} Catch (Exception e) {} finally { D ();}
Does it look like the same structure as Do{}while (false)
Using the above method can be very convenient to process the process, we hope to help
Do{}while (false) magical