嵌套和级联

似梦清欢
• 阅读 1130

嵌套的IF-ELSE

判断三个数字的大小 嵌套和级联

#include <stdio.h>

int main()
{
    int a = 0,b = 0,c = 0;
    scanf("%d,%d,%d",&a,&b,&c);
    int max;
    if (a > b){
        if (a > c){
        max = a;
        }else{
        max = b;
        }
    }else{
        if (b > c){
        max = b;
        }else{
            max = c;
        }
    }
    printf("max=%d",max);
}
1512,4751,995
max=4751
--------------------------------
Process exited after 10.74 seconds with return value 8

::: tip if、else后面的大括号中只有一句话时可以省略大括号 ::: 省略括号后的程序如下:

#include <stdio.h>

int main()
{
    int a = 0,b = 0,c = 0;
    scanf("%d,%d,%d",&a,&b,&c);
    int max;
    if (a > b)
        if (a > c)
        max = a;
        else
        max = b;

    else
        if (b > c)
        max = b;
        else
            max = c;
    printf("max=%d",max);
}
嵌套的判断

当if的条件满足或者不满足时,要执行的语句也可以是if或if-else语句,称为嵌套的if语句。嵌套和级联

else的匹配

else总是和最近的那个if匹配。

#include <stdio.h>

int main()
{
    const int count = 20;
    int a = 0,b = 0;
    scanf("%d,%d",&a,&b);
    if (a == b)
        if (a < count)
            printf("good");
        else 
            printf("bad");    
    return 0;
}

(1)当a和b相等并大于count时,程序跳到12:

26,26
bad
--------------------------------
Process exited after 5.431 seconds with return value 0

(2)当a和b相等并小于count时,程序执行10:

16,16
good
--------------------------------
Process exited after 6.571 seconds with return value 0

(3)当a和b不相等时,程序跳到13:

16,26

--------------------------------
Process exited after 3.551 seconds with return value 0

使用大括号可以改变if和else的配对关系

#include <stdio.h>

int main()
{
    const int count = 20;
    int a = 0,b = 0;
    scanf("%d,%d",&a,&b);
    if (a == b){
        if (a < count)
            printf("good");
    }else 
            printf("bad");    
    return 0;
}

(1)当a和b相等并大于count时,程序跳到13:

26,26

--------------------------------
Process exited after 3.419 seconds with return value 0

(2)当a和b相等并小于count时,程序执行10:

15,15
good
--------------------------------
Process exited after 2.986 seconds with return value 0

(3)当a和b不相等时,程序跳到12:

15,26
bad
--------------------------------
Process exited after 4.109 seconds with return value 0

对于上述加了括号的if-else程序来说,括号内的的if语句没有对应的else语句,else语句和大括号前的if语句组合。

缩进

缩进格式不能暗示else的匹配

#include <stdio.h>

int main()
{
    const int count = 20;
    int a = 0,b = 0;
    scanf("%d,%d",&a,&b);
    if (a == b)
        if (a < count)
            printf("good");
    else 
            printf("bad");    
    return 0;
}

(1)当a和b相等并大于count时,程序执行12:

26,26
bad
--------------------------------
Process exited after 2.298 seconds with return value 0

(2)当a和b不相等时,程序跳到13:

26,16

--------------------------------
Process exited after 3.258 seconds with return value 0

::: tip 上述实验证明格式的缩进不会影响else和if的匹配,else还是会首先和最近的if匹配,如果需要改变匹配关系,需要使用大括号。 ::: ::: warning 为了避免不必要的麻烦和误解,在if和else后加上大括号,作为良好的编程习惯。 :::


级联的IF-ELSE IF

分段函数的if-else写法 表示f(x)={-1,x<0;0,x=0;2x,x>0}

#include <stdio.h>

int main()
{
    int x = 0,f = 0;
    scanf("%d",&x);
    if (x < 0){
        f = -1;
    }else{
        if(x == 0){
        f = 0;
        }else{
            f = 2 * x;
        }
    }
    printf("%d",f);
    return 0;
}

::: tip else后接if,可以连写在一起“else if” ::: 将上述程序的9、10行的else、if语句改写:

#include <stdio.h>

int main()
{
    int x = 0,f = 0;
    scanf("%d",&x);
    if (x < 0){
        f = -1;
    }else if(x == 0){
        f = 0;
    }else{
        f = 2 * x;
    }
    printf("%d",f);
    return 0;
}

嵌套和级联 上述流程图,构成了如下级联的if-else:

if(exp1)
st1;
else if(exp2)
st2;
else
st3;

程序的最前面和最后面有一个匹配的if和else,中间部分写成else if,在程序中,所有的else都对齐,在条件很多时可以节省屏幕空间,便于阅读。


上述改写过的程序还有一种表达方式,省略变量f,在每一个条件后输出结果,如下: 嵌套和级联

左边的程序优势更明显(单一出口),变量f的使用和程序执行过程无关,后续变量f可以用于其他运算,右边的程序每一步判断后直接输出结果,后续无法参与其他计算。

点赞
收藏
评论区
推荐文章

暂无数据

似梦清欢
似梦清欢
Lv1
慢慢来吧...
文章
17
粉丝
12
获赞
1