分段函数
#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){