if(条件)
stl;
else if(条件)
st2;
else
st3;
级联的让程序更标准容易看
下面是两个分段函数不同写法
#include <stdio.h>
int main()
{
int x;
scanf("%d",&x);
int f=0;
if (x <0){
f =-1;
}else{
if (x ==0){
f = 0;
} else {
f = 2*x;
}}
printf ("%d\n",f);
return 0;
}
这样乱敲的就很乱
这是级联的
#include <stdio.h>
int main()
{
int x;
scanf("%d",&x);
int f=0;
if ( x < 0 ) {
f = -1;
} else if ( x == 0 ){
f = 0;
} else if ( x > 5 ) {
f = 2 * x;
} else {
f = 3 * x;
}
printf("%d\n", f );
return 0;
}
级联的简单的说就是上下else对齐 大括号对齐 有多种写法