if...else if.else if.else if.的用法是什么
1个回答

if是判断是否符合单个条件,不用考虑其他条件.

例如 if(password!=a)

printf("密码输入错误")

if..else 用于两种条件居其一的条件选择语句,

例如 if(a>b)

printf("max=%d",a);

else

printf("max=%d",b);

if...else if...else if...(后门可以有无数else if)

以上三种都是平等条件判断,if...else if...else if..举例:

if (month==1)

day=31;

else if(month==2)

day=28;

else if (month==3);

day=31;.

也可以写成 if (month==1||month==3||month==5||...)

day=31;

else if (month==4||month==6||month==8||...)

day=30;

else

day=28;

当最后只有一种可能性时,只要用else 结束就行了,不需要继续判断了.当然继续用else...if也没错,但是没必要.

另外一种是嵌套条件语句:

例如:

if(score>=60)

{

if(score=100)

printf("满分");

else if(score>=90)

printf("优秀");

else if(score>=80)

printf("良好");

else if(score>=70)

printf("中等");

else

printf("及格");

}

else

printf("不及格");

显而易见的,首先判断是否大于等于60分,如果不是,那么直接输出不及格,如果是,那么在这个条件框下继续判断,这就是嵌套条件语句了.

}