Do ...... while (false)

Source: Internet
Author: User
Personal classification: Design C/C ++

Today, I found a style that I don't quite understand. Some codes are segmented and contained in the do {...} while (false) interval. In general, do while is used for loop, but here the loop condition is false, there will be no loop at all, so what is the significance?

After checking the Internet, we can conclude that using the do {...} while (false) structure can simplify the nesting of multi-level Judgment time codes.

For example, to implement a function, we need four prerequisites: A, B, C, and D. In addition, both of these prerequisites depend on the upper-level, that is, B depends on, c depends on a and B, and D depends on A, B, and C. If you follow the general syntax, it is as follows:

 

  1. If (A = true)
  2. {
  3. If (B = true)
  4. {
  5. If (C = true)
  6. {
  7. If (D = true)
  8. {
  9. // Code for implementing the Function
  10. }
  11. }
  12. }
  13. }
It may be seen that this causes multi-layer if statement nesting, and the logic looks unclear.

One solution is to use the GOTO statement. When a condition fails, the system directly jumps to the following statement segment, as shown in the following figure:

 

  1. If (A = false)
  2. Goto tag;
  3. If (B = false)
  4. Goto tag;
  5. If (C = false)
  6. Goto tag;
  7. If (D = false)
  8. Goto tag;
  9. // Code for implementing the Function
  10. Tag:
  11. ...
The style looks much better, but it is not recommended to use the GOTO statement.

In fact, you can use the do while statement to implement similar goto functions, but the code is much easier to read than the Goto style. The Code is as follows:

 

  1. Do
  2. {
  3. If (A = false)
  4. Break;
  5. If (B = false)
  6. Break;
  7. If (C = false)
  8. Break;
  9. If (D = false)
  10. Break;
  11. // Code for implementing the Function
  12. } While (false );
  13. ...
In this way, you can understand the code segment in do {...} while (false). You can use break to implement a jump function similar to goto, which is very useful in actual projects.

Do ...... while (false)

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.