Benefits of using do {} while (0)

Source: Internet
Author: User

I have seen some programs or macros using do {} while (0). I think it's quite strange. I don't know why I wrote this, so I am Baidu...

I have a good post to share with you!

Macro I met

#define LFW(x, y) do { x = websGetVar(wp, T(#y), T("")); } while(0)
 _Api()  {     do      {         //do something      }while(0);  }

Benefits of using do {} while (0) 1. Replace {} to implement local scope. It can also be used in some implementations of C.
2. Avoid using Goto and use break to jump out.

When you execute half of the code and want to skip the remaining half, if you are in the do while loop, you can use break for this purpose. The following pseudo code:

Do {I run my execution .. I will execute... If (what conditions have met me) {ah, I want to jump to another piece of code, and the rest will not be executed, but it is not recommended to use the GOTO statement. What should I do? Break;/* view me */} I may be executed and I may be executed. I may be executed .. } While (false) to jump to this line, isn't break very convenient?

Assume that A and B are two C statements respectively. They define an order to execute their Macros in several ways, such

# Define Ab1 A; B; // X, the following statement B cannot be executed: If (Cond) Ab1; # define AB2 {A; B;} // X, the following statement compilation error: If (Cond) AB2; else ...; # define ab3 A, B // X, with operator priority problems # define ab4 do {A; B;} while (0)

The first few problems exist, and only do while (0) is better solved.


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.