C06 (branch statement)

1. If statement analysis

1.1 if statement analysis

  • The if statement chooses to execute the statement according to the conditions
  • else cannot exist independently and always matches the closest if
  • else statement can be connected with other if statements

Insert picture description here

1.2 Points to note about zero value comparison in if statements

  • bool type variables should appear directly in the condition and cannot be compared

  • When comparing variables to 0, 0 should appear to the left of the comparison symbol

  • Float type variables cannot be directly compared with 0, you need to define the precision

  • If it is judged in if parentheses, if you use == to compare variables and constants, the constants should be placed on the left, to avoid writing an equal sign to become an assignment statement, which causes the compilation stage to be unable to detect such errors. Assigning a variable to a constant during compilation Can be checked out (engineering experience)

Insert picture description here

2. Analysis of switch statements

  • The switch statement corresponds to a single condition with multiple branches
  • The case statement branch must have a break, otherwise it will cause branch overlap
  • The default statement is necessary to keep up to deal with special situations (to prevent some unexpected accidents, such accidents include special situations where programmers do not think comprehensively or some environmental impacts (engineering experience)

Insert picture description here

  • The value in the case statement can only be integer or character, and can only be constant
  • Arrangement order of case statements
    • Arrange the sentences in alphabetical or numerical order
    • Put the normal situation in the front, and the abnormal situation in the back
    • The default statement is only used to deal with the real default situation

3. Code analysis

3.1 Use case of if and switch statements

#include <stdio.h>

void f1(int i)
{
    if( i < 6 )
    {
        printf("Failed!\n");
    }
    else if( (6 <= i) && (i <= 8) )
    {
        printf("Good!\n");
    }
    else
    {
        printf("Perfect!\n");
    }
}

void f2(char i)
{
    switch(i)
    {
        case 'c':
            printf("Compile\n");
            break;
        
        case 'd':
            printf("Debug\n");
            break;
        
        case 'o':
            printf("Object\n");
            break;
            
        case 'r':
            printf("Run\n");
            break;
        
        default:
            printf("Unknown\n");
            break;
            
    }
}

int main()
{
    f1(5);
    f1(9);
    f1(7);
    
    f2('o');
    f2('d');
    f2('e');

    return 0;
}

3.2 Interchange of if statement and switch statement

The if statement and the switch statement are interchangeable in function. For example, the above code can be adapted to the following code:

#include <stdio.h>

void f1(int i)
{
    switch( i < 6 )
    {
        case 1:
            printf("Failed!\n");
            break;
        
        default:
            switch( (6 <= i) && (i <= 8) )
            {
                case 1:
                    printf("Good!\n");
                    break;
                    
                default:
                    printf("Perfect!\n");
                    break;
            }
            break;
    }
}

void f2(char i)
{
    if( 'c' == i )
    {
        printf("Compile\n");
    }
    else if( 'd' == i )
    {
        printf("Debug\n");
    }
    else if( 'o' == i )
    {
        printf("Object\n");
    }
    else if( 'r' == i )
    {
        printf("Run\n");
    }
    else
    {
        printf("Unknown\n");
    }
}

int main()
{
    f1(5);
    f1(9);
    f1(7);
    
    f2('o');
    f2('d');
    f2('e');

    return 0;
}

4. Summary

  • if statement is used to judge complex logic
  • The switch statement is used to judge the discrete value
  • if statement and switch statement are interchangeable in function
  • The if statement is more concise for the case of judging by branch
  • switch is more concise for multi-branch judgment
Published 123 original articles · praised 31 · 90,000 views +

Guess you like

Origin blog.csdn.net/qq_40794602/article/details/105120907