C ++ do while 0 usage and significance, dowhile

Source: Internet
Author: User

C ++ do while 0 usage and significance, dowhile



/* Usage and significance of do while 0 * // recently, do while 0 has been seen in many codes. At first glance, didn't this do anything? Why. Is that an alternative? Of course not. // 1.0 help define complex macros to avoid errors when referencing: // suppose you want to define a macro like this # define DOSOMETHING () \ func1 (); \ func2 (); // call int a = 3; if (a> 3) DOSOMETHING () // expand if (a> 3) func1 (); func2 (); // no matter how fun2 is executed. // if it is wrapped up, it should be like this when it is expanded. if (a> 3) // both functions can be executed to do {func1 (); func2 () ;}while (0); // 2.0 avoid using goto to control the Program Stream uniformly: do {int * I = new int (); if (I = func1 () {break;} if (I = func2 () {break ;}} while (0); // 3.0 avoid empty macros caused by warning // due to different architecture constraints in the kernel, empty macros are often used. during compilation, the empty Macro will provide a warning. To avoid such a warning, you can use do {} while (0) to define the empty macro: # define EMPTYMICRO do {} while (0)











Meaning of an example of dowhile (0)

Avoid using goto.
In the past, the traditional practice of c was to use goto to handle exceptions. However, goto is not easy to manage. So we replaced it with while (0 ).

Logic in C language or usage in dowhile

I don't know what you are talking about. First you enter-1, the screen prints-1, and then judge that a <0 is true and continues to run. If you input 2, the screen prints 2, then judge that a is not true and the program ends.

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: info-contact@alibabacloud.com 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.