c语言笔记(翁恺3.3.2~3.3.4)if-else,switch

本文详细介绍了C语言中的分段函数,包括级联的if-else if结构,以及3.3.4节中关于如何根据输入值进行逐级判断的情况。此外,还探讨了switch-case语句的效率优势,它允许直接跳转到匹配的case执行,通过break控制退出,简化了多条件判断的流程。
摘要由CSDN通过智能技术生成

分段函数

#include<stdio.h>
int main()
{
    int f;
    if(x<0){
    f=-1;
}else if(x==0){
f=0;
}else{
f=2*x;
}
单一出口

级联的if-else if

if(exp1)

st2;

else if(exp 2)

st 2;

else

st3;

3.3.4

#include <stdio.h>
int main()
{
    if(type==1)
      printf("您好");
    else if(type==2)
      printf("早上好");
    else if(type==3)
      printf("晚上好");
    else if(type==4)
      printf("再见");
    else
      printf("啊,什么啊?");
return 0;
}

如果输入type==3,那么计算机会一步一步从type=1开始判断一直判断到3正确才会输出。

上面例子改成switch-case

#include <stdio.h>
int main()
{  int type;

   scanf("%d",&type);

   switch(type){
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值