循环:if
if语句的形式:C语言把任何非零和非null的值假定为true,把0或null假定为false.
if(条件){
条件成立要做的事
}
if(条件){
条件成立要做的事
}else{条件不成立要做的事
}
if(条件1){
条件1成立要做的事
}else if(条件2){
条件2成立,条件1不成立要做的事
}else if(条件3){
}
......
}else{
以上条件都不成立要做的事
}
问号表达式(三目运算符)
char c;
if(3>1){
c='y';
}else{
c='n';
}
等价于 c=3>1?'y':'n'
逗号表达式
result =表达式1,表达式2,表达式3
int temp=20;
int t2=(temp--,temp--,temp--);//t2=18
if(t2){
printf("temp=%d\n",temp);
printf("t2=%d\n",t2);
}
计算工资
#include<stdio.h>
int main(){
int salary=0;
int pay=0;
printf("请输入工资:");
scanf("%d",&salary);
//开始计算
if(salary<5000){
pay=salary;
} else if(salary>5000&&salary<8000){
int tax=(salary-5000)*0.1;
pay=salary-tax;
}else if(salary>8000&&salary<10000){
int tax=(salary-8000)*0.15+3000*0.1;
pay=salary-tax;
}else{
int tax=(salary-10000) *0.2+3000*0.1+2000*0.15;
pay=salary-tax;
}
printf("到手工资:%d",pay);
return 0;
}
优化计算工资
#include<stdio.h>
int main(){
int salary=0;
int tax=0;
printf("请输入工资:");
scanf("%d",&salary);
//开始计算
if(salary<5000){
tax=0;
} else if(salary>5000&&salary<8000){
tax=(salary-5000)*0.1;
}else if(salary>8000&&salary<10000){
tax=(salary-8000)*0.15+3000*0.1;
}else{
tax=(salary-10000) *0.2+3000*0.1+2000*0.15;
}
printf("到手工资:%d",salary-tax);
return 0;
}
年龄转英文实战
#include<stdio.h>
int main(){
int age=0;
//保存个位数
char* gewei[] ={"","one"," two ","three ","four ","five"," seven ","eight"," nine"};
char* temp[]={"ten"," eleven"," twelve ","thirteen ","fourteen"," fifteen"," sixteen ","seventeen ","eighteen","nineteen"};
char* shiwei[]={"twenty ","thirty","forty ","fifty ","sixty ","seventy"," eighty ","ninety"};
printf("请输入年龄:");
scanf("%d",&age);
//[one two three four five seven eight nine ]
//[ten eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen]
// [twenty thirty forty fifty sixty seventy eighty ninety]
char* name;
if(age<10){
//直接访问个位数数组
name=gewei[age];
}else if(age>=10&&age<19){
int index=age%10;//获取个位数
name=temp[index] ;
}else{
//获取个位数
int g=age%10;
//获取十位数
int s=age/10;
char* sString=shiwei [s-2];
char* gString=gewei[g];
printf("your age is %s-%s\n",sString,gString);
return 0;
}
printf("your age is %s\n",name);
return 0;
}