If语句的结构§两种结构§1if(条件)指令;§2if(条件)指令x;§else指令y;§这两种是独立的,完整的结构;各自只算一条语句。§没有if的else是错误的;§在else和它的配对if之间的”指令x,指令y”必须是一条语句,或者一条符合语句。if语句的嵌套CPU指令1CPU指令2AB?指令XYN§指令x是一个语句,或者是一个复合语句{…}§可以是各种独立的完整语句§当然也可以是再一个if语句if语句的嵌套§if(ab)§if(x==y)指令3;CPU指令1CPU指令2AB?指令3YNX==Y?YNelse部分,也可以嵌套§指令x和y都可以是if语句主角2指令1主角1指令2AB?指令xYN指令yif语句嵌套:一般形式:if(expr1)if(expr2)statement1elsestatement2elseif(expr3)statement3elsestatement4内嵌if内嵌ifif(expr1)if(expr2)statement1elsestatement2内嵌ifif(expr1)if(expr2)statement1elsestatement3内嵌ifif(expr1)statement1elseif(expr3)statement3elsestatement4内嵌if嵌套应用:格式:if(expr1)statement1elseif(expr2)statement2elseif(expr3)statement3…...[elsestatementn]expr1statemnt1非0=0expr2expr3statemntnstatemnt3statemnt2非0非0=0=0执行过程:例:if(salary1000)tax=0.4;elseif(salary800)tax=0.3;elseif(salary600)tax=0.2;elseif(salary400)tax=0.1;elsetax=0;/*ch4_2.c*/#includestdio.hmain(){inta,b;printf(Enterintegera:);scanf(%d,&a);printf(Enterintegerb:);scanf(%d,&b);if(a==b)printf(a==b\n);elseprintf(a!=b\n);}例输入两个数并判断两数相等否运行:Enterintegera:12Enterintegerb:12a==b运行:Enterintegera:12Enterintegerb:9a!=b/*ch4_3.c*/#includestdio.hmain(){charc;printf(Enteracharacter:);c=getchar();if(c0x20)printf(Thecharacterisacontrolcharacter\n);elseif(c='0'&&c='9')printf(Thecharacterisadigit\n);elseif(c='A'&&c='Z')printf(Thecharacterisacapitalletter\n);elseif(c='a'&&c='z')printf(Thecharacterisalowerletter\n);elseprintf(Thecharacterisothercharacter\n);}例判断输入字符种类运行:Enteracharacter:Thecharacterisacontrolcharacter运行:Enteracharacter:8Thecharacterisadigit运行:Enteracharacter:DThecharacterisacapitalletter运行:Enteracharacter:hThecharacterisalowerletter运行:Enteracharacter:,Thecharacterisothercharacter例输入两数并判断其大小关系/*ch4_4.c*/#includestdio.hmain(){intx,y;printf(Enterintegerx,y:);scanf(%d,%d,&x,&y);if(x!=y)if(xy)printf(XY\n);elseprintf(XY\n);elseprintf(X==Y\n);}运行:Enterintegerx,y:12,23XYEnterintegerx,y:12,6XYEnterintegerx,y:12,12X==Yif~else配对原则:缺省{}时,else总是和它上面离它最近的未配对的if配对if(……)if(……)if(……)else…...else…...else…...嵌套的if配对例:if(a==b)if(b==c)printf(“a==b==c”);elseprintf(“a!=b”);修改:if(a==b){if(b==c)printf(“a==b==c”);}elseprintf(“a!=b”);实现if~else正确配对方法:加{}例考虑下面程序输出结果:main(){intx=100,a=10,b=20;intv1=5,v2=0;if(ab)if(b!=15)if(!v1)x=1;elseif(v2)x=10;x=-1;printf(“%d”,x);}结果:-1switch语句(开关分支语句)一般形式:switch(表达式){caseE1:语句组1;break;caseE2:语句组2;break;…….caseEn:语句组n;break;[default:语句组;break;]}执行过程:switch表达式语句组1语句组2语句组n语句组…...E1E2Endefaultcase说明:E1,E2,…En是常量表达式,且值必须互不相同C语言根据表达式的值,找到指令入口找到入口之后,顺序执行下去,直到遇到break跳出如:……case‘A’:case‘B’:case‘C’:printf(“score60\n”);break;……..例switch(score){case5:printf(“Verygood!”);case4:printf(“Good!”);case3:printf(“Pass!”);case2:printf(“Fail!”);default:printf(“dataerror!”);}运行结果:score为5时,输出:Verygood!Good!Pass!Fail!dataerror!缺了break;例break只能跳出一层switchvoidmain(){intx=1,y=0,a=0,b=0;switch(x){case1:switch(y){case0:a++;break;case1:b++;break;}case2:a++;b++;break;case3:a++;b++;}printf(“\na=%d,b=%d”,a,b);}运行结果:a=2,b=1/*ch4_5.c*/#includestdio.hmain(){intc;printf(Entermornorhorother:);c=getchar();switch(c){case'm':printf(\nGoodmorning!\n);break;case'n':printf(\nGoodnight!\n);break;case'h':printf(\nHello!\n);break;default:printf(\n????????\n);break;}}例根据输入字母输出字符串