if语句知识小结
**if语句是用来判定所给定的条件是否满足,根据判定的结果(真或假)决定执行给出的两种操作之一。**
if语句的3种形式
C语言提供3种形式的if语句。
1.if(表达式)语句
eg:if (x>y)printf("%d",x);
2.if(表达式)语句1 else 语句2
eg:if (x>y)
printf("%d",x);
else
printf("%d",y);
3.if(表达式) 语句1
else if(表达式2)语句2
else if (表达式3)语句3
.
.
.
else if (表达式m) 语句m
else 语句 n
eg:
if (number>500) cost =0.15;
else if (number>300) cost =0.10;
else if(number>100) cost=0.075;
else if(number>50) cost=0.05;
else cost=0;
#include<stdio.h>
void main()
{
int a=5,b=4,c=3,d=2;//先对数进行定义
if (a>b>c) //a>b>c是判定条件,可知a=5>b=4,所以为1(真),1>3为假,所以不执行
printf("%d\n", d);
else if ( (c-1 >= d) = =1) //可知条件为真,d的值不改变,输出d+1=2+1=3
printf("%d\n", d+1);
else
printf("%d\n", d+2);
}