某211大学C语言全套课件08_03

上传人:zw****58 文档编号:47063102 上传时间:2018-06-29 格式:PDF 页数:53 大小:284.94KB
返回 下载 相关 举报
某211大学C语言全套课件08_03_第1页
第1页 / 共53页
某211大学C语言全套课件08_03_第2页
第2页 / 共53页
某211大学C语言全套课件08_03_第3页
第3页 / 共53页
某211大学C语言全套课件08_03_第4页
第4页 / 共53页
某211大学C语言全套课件08_03_第5页
第5页 / 共53页
点击查看更多>>
资源描述

《某211大学C语言全套课件08_03》由会员分享,可在线阅读,更多相关《某211大学C语言全套课件08_03(53页珍藏版)》请在金锄头文库上搜索。

1、C语言程序设计主讲教师:何震瀛 第3章 结构化程序开发基础?三种结构化的语句结构?顺序结构(sequence structure)?选择结构(selection structure)?循环结构(repetition structure)?每一个结构化的语句结构,在逻辑上可 以看成一条语句(复合语句)顺序结构?简单的顺序结构 temp=x; x=y; y=temp;?分程序(复合语句)?int temp;?temp=x;?x=y;?y=temp;?选择结构?两路选择结构?if语句?多路选择结构?switch语句if语句?if ( expression) statement?if ( expres

2、sion) statement1 else statement2?statement可以是简单语句、复合语句if语句简单例?if ( grade = 60 ) printf( “You passed!n” );print “You passed!”grade =60truefalseif语句简单例(续)?if ( grade = 60 ) printf (“You passed!n“ ); printf (“Congratulations!n“ ); if语句简单例(续)?if ( grade = 60 ) printf(“You passed!n“ ); else printf(“You f

3、ailed!n“);if语句例(续)?if (0=x) printf(“x=0n“); 等价于 if (!x) printf(“x=0n“);if语句例(续)?判断3边a,b,c0组成一个三角形 if (a+bc) else printf(“It is not a triangle.n“);?if (a+b 60) if (grade 70) printf(“You passed”); else printf(“You passed but need a tutor”); else printf(“You failed”); 嵌套if例(续)if (grade 70) printf(“You

4、passed”); else if (grade 60) printf(“You passed but need a tutor”); else printf(“You failed”); 嵌套if例(续)?求解一元一次方程ax+b=0?分析?if a=0, ?if b=0, then x is free ?if b!=0, then x is no value?else x=b/a?程序1(嵌套if)1./testif.cpp2.#include 3.void main () 4.double a,b;5.printf(“Please input a and bn“);6.scanf(“%l

5、f%lf“, 7.if (a=0)8.if (b=0)9.printf(“x is freen“);10.else11.printf(“x is no valuen“);12.else13.printf(“x=%gn“, b/a);14.?程序2(if)1./testif.cpp2.#include 3.void main () 4.double a,b;5.printf(“Please input a and bn“);6.scanf(“%lf%lf“, 7.if (a=0)9.if (a=0)11.if (a!=0)12.printf(“x=%gn“, b/a);13.比较if/嵌套if?

6、通过条件运算的 “分配律”,可以实现 if 和嵌套if 两种结构的转换?一般嵌套if的执行效率更高(判断的次数 少)?一般推荐使用嵌套if?使用嵌套if时,else总是和它前面的一个 if对应(不包括复合语句内部的if)if语句小结?是两路选择结构?每一个选择分支可以是各种语句或复合语句?使用嵌套if时,else和if的对应规则(详见P.51)?使用嵌套if可以实现多路选择结构?if if else else if else if else if 奇怪的if?下面语句执行的结果是什么??int x=1;float y=2; if ( x = y ) ; printf(“ x is equal

7、to y!n “ );奇怪的if(续)?下面语句执行的结果是什么??int x=1,y=2; if ( x = y ) printf(“ x is equal to y!n “);switch语句?switch (表达式) case 常量表达式1: 语句序列1 case 常量表达式2: 语句序列2 case 常量表达式n: 语句序列n default : 语句序列n+1 switch例?输入一个成绩(0100),计算等级(AE)。如?输入:100?输出:Your grade is A!?分析?根据“成绩/10”的值来选择case分支?值为10, 9,则grade=A;?值为8,则grade=B

8、;?值为7,则grade=C;?值为6,则grade=D;?值为5、4、3、2、1、0,则grade=E;?程序11.#include 2.void main()3.int iPoints; char cGrade= ;4.printf(“Input yout points=“); scanf(“%d“,5.switch (iPoints/10)6.case 10:cGrade=A;break;7.case 9: cGrade=A;break;8.case 8: cGrade=B;break;9.case 7: cGrade=C;break;10.case 6: cGrade=D;break;

9、11.case 5: cGrade=E;break;12.case 4: cGrade=E;break;13.case 3: cGrade=E;break;14.case 2: cGrade=E;break;15.case 1: cGrade=E;break;16.case 0: cGrade=E;break;17.default: printf(“Illegal points!n“);18.19.printf(“Yout grade is %c!n“,cGrade);20.?程序21.#include 2.void main()3.int iPoints; char cGrade= ;4.p

10、rintf(“Input yout points=“); scanf(“%d“,5.switch (iPoints/10)6.case 10:7.case 9: cGrade=A;break;8.case 8: cGrade=B;break;9.case 7: cGrade=C;break;10.case 6: cGrade=D;break;11.case 5:12.case 4:13.case 3:14.case 2:15.case 1:16.case 0: cGrade=E;break;17.default: printf(“Illegal points!n“);18.19.printf(

11、“Yout grade is %c!n“,cGrade);20.switch语句小结?是多路选择结构?每一个选择分支可以是各种语句的简单组合 (不要求是复合语句)?只能针对整型表达式(含字符型表达式)或枚举 型表达式设选择分支?case语句后只能跟常量表达式,不能含变量?break语句的作用?default语句的作用使用switch语句的建议?各个分支除了没有语句之外,一般以break语 句结束。?在所有case语句之后才使用default语句。?避免使用复杂、难理解的判断表达式,如不可 避免,可以考虑采用if语句来实现。?如计算个人收入调节税,建议采用if语句。if else if else

12、 if 循环结构?3.3.1 while语句?3.3.2 dowhile语句?3.3.3 for语句while语句?while ( expression ) statement?执行过程1.计算expression的值2.测试expression的值:值为true(即非0)时转步骤3值为false(即0)时结束while语句3.执行循环体statement,然后转步骤1?循环体:循环执行的部分简单while例?/输入一行字符,计算输入的字符个数1.#include 2.void main()3.int i=0;4.while (getchar()!=n)5.i+;6.printf(“你输入了%

13、d个字符n“,i);7.例3.12(while)/输入班级学生考试成绩,求平均成绩。输入为负数时结束。1.#include 2.void main()3.int sum=0, count=0, mark;4.while (1)5.printf(“输入成绩(小于0结束)n“);6.scanf(“%d“,7.if (mark2.void main()3.int c, nwhite, nother, ndigit;4.nwhite=nother=ndigit=0;5.printf(“Enter string linen“);6.while (c=getchar()!=n)7.switch (c)8.

14、case0:case1:case2:case3:case4:9.case5:case6:case7:case8:case9:10.ndigit+;break;11.case :casen:caset:12.nwhite+;break;13.default:nother+;break;14.15.16.printf(“digit=%dtwhite space=%dtother=%dn“,ndigit,nwhite,nother);17.do-while语句?do statement while ( expression )?执行过程1.执行循环体statement2.计算expression的值

15、3.测试expression的值:值为true(即非0)时转步骤1值为false(即0)时结束do-while语句简单do-while例?/求s=1+2+1001.s=0;2.i=1;3.do4.s+=i;5.i+;6.while (i0);int i=100; while(i0) i-; for(i=100;i0;i-) 1,2,4,8,16,32,64,1024int i=1; do i*=2; while (i=1024);int i=1; while(i=1024) i*=2; for(i=1;i=1024;i*= 2) Other sequences: 1,-2,3,-4,5,-6,7,-8, 1,1/2,1/4,1/8,1/16,1/32,1/64, 1,1/2,1/3,1/4,1/5,1/6,1/7, 1,2,3,5,8,13,21,34, 三种循环结构适用场合?三种结构往往可以互相转换?for一般用于确定循环次数的循环结构; while和do-while一般用于不确定循环 次数的循环结构?do-while结构用于至少循环一次的场 合; while结构用于至少循

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 高等教育 > 其它相关文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号