Three basic flow of control

Source: Internet
Author: User
Tags compact

Recently reread K&r's "C programming language", read the "Control Flow" chapter, found that the original learning is too not solid, so look up information and summed up with you to share. One: Sequential structures are executed in order, with the most, nothing to say. Two: Select structure 1.if-else for single condition judgment,Precautions :
    1. If multiple layers are nested without curly braces, then else is paired with the most inner if, so in order to cause ambiguity, multilayer nesting is best to add curly braces. Millions cannot express hierarchical relationships in an abbreviated way, not with languages such as Python.
    2. Generally do not use if (a = = 0) notation, and if (!a), indicating "if a is not equal to 1", very intuitive.
If-else Simple deformation:
  1. Nesting, Code:
        
       
    1. if (n > 0)
    2. {
    3. if (a > b)
    4. {
    5. z = a;
    6. }
    7. else
    8. {
    9. z = b;
    10. }
    11. }
    (Although the curly braces of the first if do not add, but the curly braces are easier to read)
  2. Multiple conditions are met at the same time, code:
     
         
        
    1. if (a>b && b>c)
    When multi-layer if nesting and no else, you can use this formal simplification, such as the above code equivalent to:
        
       
    1. if (a>b)
    2. {
    3. if (b>c)
    4. {
    5. /* code */
    6. }
    7. }
    Is the first one more compact and clearer?
  3. Any one of several conditions can be satisfied, code:
      
         
       
    1. if (a>b || b>c)
  4. ? : Structs can write more compact code, but with less readability, it's not possible.
2.else If do not write if else for the determination of the multi-path condition. Multiple lateral if and no else, this structure can be used instead of:
Peer Else Code:
  
 
  1. if (a > b)
  2. {
  3. /* code */
  4. }
  5. if (b > c)
  6. {
  7. /* code */
  8. }
  9. if (c > d)
  10. {
  11. /* code */
  12. }
Replace with the else if structure code:
  
 
  1. if (a > b)
  2. {
  3. /* code */
  4. }
  5. else if (b > c)
  6. {
  7. /* code */
  8. }
  9. else if (c > d)
  10. {
  11. /* code */
  12. }
  13. else
  14. {
  15. ;
  16. }
Is the second more readable? 3.switch Precautions:
    1. Each case is followed by a break, which should be annotated when there is no need to add it. Also for readability, to prevent others from reading your code when you forget to add.
    2. Adding the default branch is a good habit that can be used to catch exceptions.
    3. This approach requires that both switch and case must be constants and do not allow variables.
Both switch and else if can be multiplexed, comparing the two:
    1. The switch code is more streamlined, and in many cases it is more efficient than else if, for example, the Swich method is much better when matching the state constants, so the multi-channel selection is preceded by a switch method.
    2. The former can only be used for constants, which are constants and variables.
Three: Circular structure 1.while cycle not much to say. 2.for loops Theoretically, it can be implemented with a while. However, the number of cycles known is used for loops, while loops are used in the case where the number of cycles is unknown. 3.do while loops are less useful, but sometimes easier to use.

From for notes (Wiz)

Three basic flow of control

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.