C language notes: branch statement

content

1.if statement (notes)

1.1 else matches the closest if

2. switch statement

2.1 break in switch statement

2.2 default clause


1.if statement (notes)

1.1 else matches the closest if

#include <stdio.h>
int main()
{
    int a = 0;
    int b = 2;
    if(a == 1)
        if(b == 2)
            printf("hehe\n");
    else
        printf("haha\n");
    return 0;
}
//什么都不会显示,因为else是和与自己最近的if匹配
//注:该代码可能会让人误以为else与if(a == 1)匹配

Modified as follows:

//适当的使用{}可以使代码的逻辑更加清楚。
//代码风格很重要
#include <stdio.h>
int main()
{
    int a = 0;
    int b = 2;
    if(a == 1)
   {
        if(b == 2)
       {
            printf("hehe\n");
       }
   }
    else
   {
         printf("haha\n");
   }       
    return 0;
}                                //haha

Good programming habits:

When judging whether two values ​​are equal in the if brackets, it is recommended to put the constant on the left side of == to facilitate error correction in case of errors

For example, if (a == 4), it is easy to write if (a = 4), but the program will still be executed, so it is easy to find errors. And when we write if(4 = a), the compiler will report an error directly , so that it is easier to find the error location when writing mistakes.

Such as:

#include <stdio.h>
int num = 1;
if(5 == num)
{
    printf("hehe\n");
}  //当错写为5 = num的时候,编译器会直接报错,方便我们找到错误的位置

2. switch statement

A switch statement is also a branching statement. Often used in multi-branch situations.

Note: The switch parentheses must be integer constants

2.1 break in switch statement

In the switch statement, we can't directly implement the branch, and use it with break to achieve the real branch.

Such as:

#include <stdio.h>
int main()
{
    int day = 0;
    switch(day)
   {
        case 1:
            printf("星期一\n");
            break;
        case 2:
            printf("星期二\n");
            break;
        case 3:
            printf("星期三\n");
            break;    
        case 4:
            printf("星期四\n");
            break;    
        case 5:
            printf("星期五\n");
            break;
        case 6:
            printf("星期六\n");
            break;
        case 7:
            printf("星期天\n");    
            break;
   }
    return 0;
}

Sometimes our needs change:

1. When inputting 1-5, the output is "weekday";

2. When input 6-7, output "weekend";

So modify the code to:

#include <stdio.h>
//switch代码演示
int main()
{
    int day = 0;
    switch(day)
   {
        case 1:
        case 2:
        case 3:
        case 4:
        case 5:
            printf("weekday\n");
            break;
        case 6:
        case 7:
            printf("weekend\n");
            break;
   }
    return 0;
}

Good programming habits :

Add a break statement after the last case statement.

(The reason for this is to avoid forgetting to add a break statement after the previous last case statement)

2.2 default clause

When the value of the switch expression does not match the values ​​of all case labels, the statements following the default clause are executed.

Therefore, only one default clause can appear in each switch statement.

But it can appear anywhere in the statement list, and the statement flow will execute the default clause as if it were a case label.

Good programming habits :

It's a good practice to put a default clause in every switch statement, and maybe even add a break after it.

practise:

//m = ? n = ?
#include <stdio.h>
int main()
{
    int n = 1;
    int m = 2;
    switch (n)
   {
    case 1:
            m++;                   //m = 3
    case 2:
            n++;                   //n = 2
    case 3:
            switch (n)
           {//switch允许嵌套使用
             case 1:
                    n++;
             case 2:
                    m++;           //m = 4
                    n++;           //n = 3
                    break;
           }
    case 4:
            m++;                   //m = 5
            break;
    default:
            break;
   }
    printf("m = %d, n = %d\n", m, n);  //m = 5 n = 3
    return 0;
}

Guess you like

Origin blog.csdn.net/m0_62934529/article/details/123143970