The branch structure of C language entry notes

For students who are just getting started, you must not be impatient, don't say you want to finish your knowledge all at once. Don’t just learn what you want to do. The reason why I have to say these uses is what I used to be. But the more you do this, the less you can learn things and you can't make things. In the end, you still have to learn to learn, accumulate and then apply. Okay, let’s not talk too much nonsense, let’s take a look at the knowledge points this time-branch structure

  • The branch structure is to execute specific instructions after logical operations. Commonly used logical operations are 大于>,小于<,等于==,小于等于<=,大于等于>=.
  • Since C language does not use boolean variables, it uses 0 to represent false and non-zero to true to represent the result of logical operations

one example

#include <stdio.h>

int main()
{
    int a = 3;
    if(a >= 3)
    {
        printf("%d大于等于3\n");
    }
    else
    {
        printf("%d小于3\n");
    }
    return 0;
}

Run to view results

About branch structure

This branch structure is composed of if...else...judgment statements. The branch structure is also relatively simple to understand, but it is very important. A complex problem inevitably requires judgment sentences.

//if...else...分支结构
if(条件语句)  //先运算if中的语句,判断结果是真是假,0为假,非0即真
{
    //如果判断为真,执行此代码块
}
else
{
    //反之,执行此代码块
}

About if

  • if can exist alone
if(条件语句)
{
    //为真执行
}
  • Multiple judgments
if(条件语句)
{
    //满足条件执行
}
else if(条件语句)
{
    //满需条件执行
}
.
.      //多个条件
.
else if(条件语句)
{
    //满足田间执行
}
else
{
    //一个条件都不满足时执行
}

Another example

#inlcude<stdio.h>

int main()
{
    int a;
    scanf("%d",&a);
    switch(a % 2)   //%对2取余运算,结果就是0或1
    {
        case 1: printf("输入的是一个奇数"); break;
        case 0: printf("输入的是一个偶数\n"); break;
        default: printf("鬼知道发生了什么\n");
    }
}

When there are many branches of problem processing, if branch is used, using if is not very beautiful and its readability is relatively poor. Therefore, use the switch statement. A switch is a power supply plant. If an area needs electricity, it sends a message to the power supply plant called area, and then the information sent in different areas is also different, that is area是变量, the power plant receives the area variable and stores it through the area Information, the power supply plant decides to supply power to that area.
Explanation of the above example: First, the user enters a number, which is stored in the variable a, and the remainder of a is taken. The result is 0 or 1, and then the switch receives this signal, and search for the information consistent with the received information in the following options. Just execute the latter 语句序列.
Regarding break: each of the casefollowing statement sequences can be used breakor not, if not applicable, then when the matching information is found, the following case will continue to be executed, regardless of whether it matches or not, until the end. Therefore, break should be used according to needs.
default: When the information does not match the case, the sequence of statements following default will be executed.


Dividing line


Well, the main content of the branch statement has been shared, because the article example is relatively simple. So, I will show some more examples at the end of the article.

  • Triangle judgment

Enter three integers, which are the three sides of a triangle, to determine whether a triangle can be formed

#include <stdio.h>

int main()
{
    printf("输入三个数字,用空格隔开:\n");
    scanf("%d %d %d",&a,&b,&c);
    //三角形判定:两边之和大于第三边
    if(a+b>c && a+c>b && b+c>a)
    {
        printf("可以构成三角形\n");
    }
    else
    {
        printf("不可以构成三角形\n");
    }
    return 0;

}

Three numbers sort

Input three integers, and then output three integers from small to large

#include <stdio.h>

int main()
{
    int a,b,c;
    int t;
    scanf("%d %d %d",&a,&b,&c);
    //比较任意两个数,讲较小的一个数和与他比较的那个数交换位置,最终a<b<c
    //输出a,b,c
    if(a>b)
    {
        t=a;
        a=b;
        b=t;
    }
    if(a>c)
    {
        t=a;
        a=c;
        c=t;
    }
    if(b>c)
    {
        t=b;
        b=c;
        c=t;
    }
    printf("%d %d %d\n",a,b,c);
}

Guess you like

Origin blog.csdn.net/weixin_36382492/article/details/80714405