Do{}while (false) magical

Source: Internet
Author: User

    • 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.