资源描述
,*,C,语言基础教程,分支结构程序设计,主要内容,概述,if,分支结构,switch,分支结构,综合实例,常见错误,概述,分支结构,或称,选择结构,。用于处理根据判断条件决定由哪个分支处理的问题。类似的问题如:,计算一元二次方程,ax2+bx+c=0,的根,如果,b2-4ac0,,则有两个不相等的实根;如果,b2-4ac=0,,则有两个相等的实根;如果,b2-4ac0,,有一对共轭复根。,如果输入的三边能构成三角形,则计算三角形的面积。,根据成绩统计不同分数段学生的人数。,两种类型:,if,语句,switch,语句,If,分支结构,if,语句的一般形式,:,if,结构,if-else,结构,if-else if,结构,If,结构,最简单的分支结构语句,其具体格式为:,if,语句,功能描述:,如果表达式的值为真,(,非,0),,则执行语句;否则不做任何处理,直接执行,if,语句的后继语句序列。这种,if,结构又称为单分支结构语句。,示例:,if(a=b)printf(“%s”,“a equal to b”);,【,注意,】,表示相等条件时,应使用关系运算符“,=”,,不能用赋值运算符“,=”,。,If,结构,/Exam4-1.cpp,#include,void main(),int a,b,max;,printf(input two numbers:);,scanf(%d,%d,max=a;,if(maxb)max=b;,printf(max=%dn,max);,程序运行结果:,input two numbers:3,5,max=5,【,例,4-1】,输入两个整数,输出其中的大数。,用于解决双分支选择问题,其格式为:,功能描述:,如果表达式值为真,则执行语句,1,;若值为假则程序进入,else,部分,执行语句,2,。任一分支语句执行完毕后,都跳出,if-else,结构,执行其后继语句。,If-else,结构,if,语句,1,else,语句,2,If-else,结构,/Exam4-2.cpp,#include,void main(),int a,b;,printf(input two numbers:);,scanf(%d,%d,if(ab),printf(max=%dn,a);,else,printf(max=%dn,b);,【,例,4-2】,修改例,4-1,If-else,结构,/Exam4-3.cpp,#include,void main(),float weight,cost;,printf(input the weight:);,scanf(%f,if(weight=50),cost=9*weight;,else,cost=10*weight;,printf(you should pay%.2f yuans.,cost);,【,例,4-3】,某商品的零售价为每公斤,10,元,批发价为,9,元,若购买量在,50,公斤以上的,可以享受批发价,给顾客购买商店的数量,计算顾客应付款数额。,程序运行结果:,input two weight:30,you should pay 300.00 yuans.,再次运行结果:,input two weight:60,you should pay 540.00 yuans.,解决多分支选择问题,其格式为:,功能描述:,如果表达式,1,的值为真,则执行语句,1,,否则如果表达式,2,为真,则执行语句,2,,,,如果所有,if,后的表达式都不为真,则执行语句,n+1,。,If-else if,结构,if,语句,1,else if,语句,2,else if,语句,n,else,语句,n+1,If-else if,结构,/Exam4-4.cpp,#include ,void main(),char c;,printf(input a character:);,c=getchar();,if(c=0&c=A&c=a&cb),printf(“%d”,a),;,else,printf(“%d”,b),;,if,语句的内嵌语句可以是单个或多个语句,当包含多个语句时,必须用花括号“,”,括起来形成一个复合语句。例如:,if(a+bc&a+cb&b+ca),s=0.5*(a+b+c);,area=sqrt(s-a)*(s-b)*(s-c);,printf(“%fn”,area);,else,printf(“,不能构成三角形,n”);,else,子句不能作为语句单独使用,它是,if,语句的子句,必须与,if,配对使用。例如,:,y=y+;,else (,错误,),y=y-;,if,结构的一般形式,if,语句的嵌套,在,if,或,else,子句中出现,if,或,if-else,语句的形式称为,if,语句的嵌套,。,嵌套的形式:,if(,表达式,1),if(,表达式,2),语句,1,else,语句,2,else,if(,表达式,3),语句,3,else,语句,4,内嵌,if,内嵌,if,if,语句的嵌套,【,例,4-5】,比较两个数的大小关系。,/Exam4-5.cpp,#include ,void main(),int a,b;,printf(please input A and B:);,scanf(%d,%d,if(a!=b),if(ab)printf(ABn);,else printf(Ab)printf(“A!Bn”);,else,printf(“A=Bn”);/else,与最外层的,if,配对,if,与,else,的配对,if,语句的嵌套,【,例,4-6】,编写程序,输入,x,的值,求下面分段函数的值。,/Exam4-6.cpp,#include,void main(),float x,y;,printf(please input x:);,scanf(%f,if(x1),y=x;,else,if(x1),if(x=10)y=5*x-17;,else y=2*x+1;,else,y=x;,switch,结构形式,功能描述:,先计算,switch,后面括号内的表达式值,然后将值与,case,后面的表达式值进行比较。若找到匹配的,case,,则执行该,case,后面的语句序列,直到遇到,break,语句为止(跳出,switch,)。若没有找到,则执行,default,后面的语句,直到,switch,语句结束。若找不到相匹配的,case,子句且不存在,default,语句,则跳过,switch,语句体,继续执行,switch,后面的下一条语句。,switch,分支结构,switch(,表达式,),case,常量表达式,1,:语句,1,case,常量表达式,2,:语句,2,case,常量表达式,n,:语句,n,default:,语句,n+1,【,例,4-7】,输入一个数字,如果该数字是,17,,则分别输出星期一,星期日的英文单词,否则,提示输出错误。,/Exam4-7.cpp,#include ,void main(),int a;,printf(input integer number:);,scanf(%d,switch(a),case 1:printf(Mondayn);,case 2:printf(Tuesdayn);,case 3:printf(Wednesdayn);,case 4:printf(Thursdayn);,case 5:printf(Fridayn);,case 6:printf(Saturdayn);,case 7:printf(Sundayn);,default:printf(errorn);,程序运行结果:,input integer number:6,Saturday,Sunday,error,switch,结构形式,switch,结构形式,注意几点:,switch,后的表达式的值必须是整型或字符型或枚举型。,case,后的表达式又称为分支标号,它是常量表达式,通常不允许包含变量和函数调用,而且每个,case,的常量表达式的值不允许相同。,每个,case,代表一个分支,其后的语句是该分支执行的功能,允许是多条语句,且可以不用,括起来。,switch,后的表达式找到相匹配的,case,后,并以此标号为入口,顺序执行后续的程序,直到遇到,break,语句为止。,switch(a),case 1:printf(Mondayn);,break,;,case 2:printf(Tuesdayn);,break,;,case 6:printf(Saturdayn);,break,;,case 7:printf(Sundayn);,break,;,default:printf(errorn);,运行结果:,input integer number:6,Saturday,switch,结构形式,多个,case,可共用一组执行语句。例如:,switch(x),case 0:,case 1:,语句,1;break;,default,是可选项,,default,语句出现的位置不会影响到程序的执行结果。即,default,可置于,case,前面、中间或后面,但是程序编译时,总是将它放在最后处理。,switch,语句可以嵌套。,switch,结构形式,void main(),int i=1,j=2;,switch(i),case 1:printf(case 1 in outern);,case 2:printf(case 2 in outern);,switch(j)/,嵌套的,switch,结构,case 1:printf(case 1 in innern);,case 2:printf(case 2 in innern);break;,case 3:printf(case 3 in outern);,运行结果:,case 1 in outer,case 2 in outer,case 2 in inner,case 3 in outer,switch,结构形式,【,例,4-8】,计算器程序。用户输入运算数和四则运算符,输出计算结果。,/Exam4-8.cpp,#include ,void main(),float a,b;,char c;,printf(input expression:a+(-,*,/)b n);,scanf(%f,%c,%f,switch(c),case+:printf(%fn,a+b);break;,case-:printf(%fn,a-b);break;,case*:printf(%fn,a*b);break;,case/:printf(%fn,a/b);break;,default:printf(input errorn);,程序运行结果:,input expression:a+(-,*,/)b,10,*,2,20.000000,解决除,0,问题,与,if,语句嵌套,case/:,if(b=0)printf(“Divided by zero!n”);,else printf(%fn,a/b);,break;,If,与,switch,switch,语句只能对等值进行测试,若测试值涉及某个较大的范围,,if,语句适用。,switch,语句只能对整型数进行测试,如果对浮点数进行测试,需要,if,语句。,若测试一个整型变量取几个不同值的情况,则用,switch,语句比较简明。,根据学生的分数输出其成绩等级:,int grade,;,/.if(grade=85 else,printf(“errorn”);,综合实例,【,例,4-9】,求一点所在的象限。,/Exam4-9.cpp,#include,void main(),float x,y;,printf(“Input the coordinate of point:n”);,printf(“x=”);,scanf(“%f”,printf(“y=”);,scanf(“%f”,if(x0),if(y0),printf(“The point is in lst quadrant.n”);,else,printf(“The point is in 4th quadrant.n”),else,if(y0),printf(“The point is in 2nd quadrant.n”);,else,printf(“The point is in 3rd quadrant.n”);,程序运行结果:,Input the coordinate of point:,x=5,y=3,The point is in lst quadrant.,再次运行结果:,Input the coordinate of point:,x=-2,y=7,The point is in 2nd quadrant.,综合示例,【,例,4-10】,已知某公司员工的保底薪水为,500,,某月所接工程的利润,profit,(整数)与利润提成的关系如下(计量单位:元):,profit1000,没有提成;,1000,profit2000,提成,10%,;,2000,profit5000,提成,15%,;,5000,profit10000,提成,20%,;,10000,profit,提成,25%,。,综合实例,/Exam4-10.cpp,#include,void main(),long profit;,int grade;,float salary=500;,printf(Input profit:);,scanf(%ld,grade=(profit1)/1000;,switch(grade),case 0:break;/profit1000,case 1:salary+=profit*0.1;break;/1000,profit2000,case 2:,case 3:,case 4:salary+=profit*0.15;break;/2000,profit5000,case 5:,case 6:,case 7:,case 8:,case 9:salary+=profit*0.2;break;/5000,profit10000,default:salary+=profit*0.25;/10000,profit,printf(salary=%.2fn,salary);,程序运行结果:,Input profit:3500,salary=4025.00,综合实例,【,例,4-11】,输入一个不超过,5,位的正整数,要求,:,求出它是几位数,;,分别打印出每位数字,;,按逆序打印出各位数字。,/Exam4-11.cpp,#include,main(),long int num;,int n1,n2,n3,n4,n5,bits;,scanf(“%ld”,/,计算数的位数,if(num9999)bits=5;,else if(num999)bits=4;,else if(num99)bits=3;,else if(num9)bits=2;,else bits=1;,printf(“bits=%dn”,bits);,n5=num/10000;/,计算万位上的数,n4=(int)(num-n5*10000)/1000;/,计算千位上的数,n3=(int)(num-n5*10000-n4*1000)/100;/,计算百位上的数,n2=(int)(num-n5*10000-n4*1000-n3*100)/10;/,计算十位上的数,n1=(int)(num-n5*10000-n4*1000-n3*100-n2*10);/,计算个位上的数,switch(bits),case 5:printf(“Every bitis:%d,%d,%d,%d,%dn”,n5,n4,n3,n2,n1);,printf(“Reverse is:%d,%d,%d,%d,%dn”,n1,n2,n3,n4,n5);,break;,case 4:printf(“Every bit is:%d,%d,%d,%dn”,n4,n3,n2,n1);,printf(“Reverse is:%d,%d,%d,%dn”,n1,n2,n3,n4);,break;,case 3:printf(“Every bit is:%d,%d,%dn”,n3,n2,n1);,printf(“Reverse is:%d,%d,%dn”,n1,n2,n3);,break;,case 2:printf(“Every bit is:%d,%dn”,n2,n1);,printf(“Reverse is:%d,%dn”,n1,n2);,break;,case 1:printf(“Bit is:%dn”,n1);,printf(“Reverse is:%dn”,n1);,程序运行结果:,1234,bits=4,Every bit is:1,2,3,4,Reverse is:4,3,2,1,常见错误,if,条件表达式表示错误。例如:,if(abc),正确的表示:,if(ab&bc),复合语句书写时忘记“,”,。,if-else,语句中,,else,之前的语句分号丢失。例如:,if(ab),printf(“%d”,a),else,printf(“%d”,b);,switch,语句中漏写,break,,使程序结果错误。,END!,
展开阅读全文