收藏 分享(赏)

第五章 循环程序设计.ppt

上传人:gnk289057 文档编号:8011272 上传时间:2019-06-04 格式:PPT 页数:65 大小:278KB
下载 相关 举报
第五章 循环程序设计.ppt_第1页
第1页 / 共65页
第五章 循环程序设计.ppt_第2页
第2页 / 共65页
第五章 循环程序设计.ppt_第3页
第3页 / 共65页
第五章 循环程序设计.ppt_第4页
第4页 / 共65页
第五章 循环程序设计.ppt_第5页
第5页 / 共65页
点击查看更多>>
资源描述

1、Chapter5 Repetition Structures,双语课堂 C,. Counter-controlled structure 计数器控制的循环,. Flag-controlled stucture 标记控制的循环,. Nested control structures 嵌套的控制结构,. The do-while repetition do-while循环结构,. The while repetition structure while循环结构,. The for repetition structure for循环结构,Repetition Structure,Repetitio

2、n Structure Group of instructions computer executes repeatedly while some condition remains trueCounter-controlled repetition (计数控制循环) Definite repetition: know how many times loop will execute Control variable used to count repetitions,Example:calculate 1+2+3+4+1000how to develop a program?!,Sentin

3、el-controlled repetition (条件控制循环) Indefinite repetition (当条件满足后,退出循环) Used when number of repetitions not known Sentinel(哨兵) value indicates “end of data“,calculate the approximation of until the last of absolute value is less than 10-6 .,how to develop a program?!,程序员指定当某个条件一直为真时重复执行某个动作如果条件“购物单上还有

4、其他商品”为真时,就执行动作“购买下一个商品,并把它从购物单上划掉” 如果该条件一直为真,这个动作就会重复执行 最终,该条件为假,循环过程就会终止,程序将执行这个循环结构之后的第一个代码语句,双语课堂 C,. Four form (1)”while” statement (2)”do while” statement (3)”for” statement (4)combination of “if” and “goto”,The while Repetition Structure,Whilewhile (condition) statement;The statement could be

5、a single statement or a compound statement 这种结构称为“当型循环”,双语课堂 C,condition,statement,false,true,int product=2; while (product=1000)product=2*product;,Counter-Controlled Repetition,Counter-controlled Repetition Repeat the cycle until the counter reaches a specific value Definite repetition times of cyc

6、le times are known,双语课堂 C,Example:,Calculate the expression:1+2+3+100,The flowchart:,Begin,int i=1,sum=0,i=100,sum=sum+i,i+,output result,End,true,false,#include void main( ) int i=1 , sum=0;while(i=100) sum=sum+i; /* sum:累加器 */i+; /* i:计数器,动态修改循环的变量,设置结束情况 */ printf(“1+2+50=%d”,sum); ,Example:A cla

7、ss of ten students took a quiz. The grades (integers in the range 0 to 100) for this quiz are available to you. Determine the class average on the quiz.Analyse:输入每个学生的成绩,累计总成绩,计算平均分,显示结果。采用计数器控制的循环来输入学生的成绩 每输入一个成绩就进行累加,Example: Pseudocode: 设置总分(total)为0 设置分数计数器(counter)为1 while 分数计数器=10输入下一个分数(grade

8、)把该分数加到总分上对分数计数器加1 班级平均分(average)为总分除以10 显示班级平均分,双语课堂 C,双语课堂 C,#include void main() int counter, grade, total, average;total=0;counter=1;while (counter=10) printf(“Enter grade:“);scanf(“%d“, ,initializationexecute looptermination,Result,双语课堂 C,Enter grade:60 Enter grade:70 Enter grade:65 Enter grade

9、:76 Enter grade:73 Enter grade:56 Enter grade:78 Enter grade:64 Enter grade:89 Enter grade:76 Class average is 70,Sentinel-controlled repetition,Example开发出一个班级平均分计算程序,每次执行该程序,都能够处理任意数量的学生分数。indefinite repetitiontimes of cycle times are unknown,双语课堂 C,思考:学生人数未知,何时结束输入? 使用标记(flag)指定“数据输入的结束” 标记值不应与正常的

10、用户输入混淆 本例可以采用-1 但同时标记值也是合法的输入 当用户输入标记值时,循环结束 哨兵(sentinel),双语课堂 C,#include void main() float average;int counter, grade, total;total = 0;counter = 0;printf(“Enter grade, -1 to end:“);scanf(“%d“, ,双语课堂 C,average = (float)total / counter;printf(“Class average is %fn“, average); ,Enter grade, -1 to end:

11、70 Enter grade, -1 to end:60 Enter grade, -1 to end:65 Enter grade, -1 to end:-1 Class average is 65.00,The do-while Repetition Structure,do-while dostatementwhile(condition);(1) The statement could be a single statement or a compound statement (2)称为“直到型循环”,双语课堂 C,condition,statement,true,false,Exam

12、ple: Calculate the expression: 1+2+100,双语课堂 C,The flowchart,i=100?,sum=sum+i i=i+1,int i , sum,Begin,i=1 , sum=0,false,true,output result,End,#include void main() int i, sum;sum=0;i=1;do sum = sum + i;i+;while(i=100);printf(“1+2+.+100=%dn“,sum); ,do-while and while,Compare while and do-while,双语课堂 C,

13、main() int s=0, n;scanf(“%d”, ,main() int s=0, n;scanf(“%d”, ,1 s=3,n=3,3 s=0,n=3,1 s=3,n=3,3 s=3,n=4,1,2,1,2,The for Repetition Structure,for for ( initialization; loopContinuationTest; increment ) statement;(1)The statement could be a single statement or a compound statement (2)称为“当型循环”,双语课堂 C,No

14、semicolon,loopContinuationTest,statement,true,false,increment,initialization,双语课堂 C,#include void main( ) int counter;for (counter = 1 ; counter = 10 ; counter+)printf(“%d“, counter); ,1 2 3 4 5 6 7 8 9 10,Example:Calculate the expression: 1+2+100,#include void main( ) int i , sum=0;for(i=1;i=100;i+

15、) sum=sum+i;printf(“1+2+3+100=%d”,sum); ,Attention:for (initialization; loopContinuationTest; increment )statement; Notes: Three expressions are optional (which can also be empty) If loopContinuationTest is empty, it is assumed that it is true. So these would create an infinite loop,双语课堂 C,for (i=0

16、; ; i+)printf(“%2d” , i);,for (i=0 ; 1 ; i+)printf(“%2d” , i);,=,loopContinuationTest and initialization can be any valid expression, the expression used a comma expression,双语课堂 C,for(s=0,i=1;i=100;i+)s+=i;,for (s=0,i=1;i=100;s+=i,i+);,Initiate of value of variables,Update value of variables,The bre

17、ak Statement,breakbreak; Causes immediate exit from a while, for, dowhile or switch statement Program execution continues with the first statement after the structure Common uses of the break statement Escape early from a loop Skip the remainder of a switch statement,双语课堂 C,Example:,双语课堂 C,#include

18、void main() int x;for (x = 1; x = 10; x+) if (x = 5)break;printf(“%d “, x);printf(“nBroke out of loop at x = %dn“, x); ,1 2 3 4 Broke ot of loop at x = 5,Example: break in switch and for structure,双语课堂 C,for (i=1;i=3;i+) switch(i) case 1: printf(“*n”);break;case 2: printf(“*n”);break;case 3: printf(

19、“*n”);break; ,* * *,for (i=1;i=3;i+) if (i=1) printf(“*n”); break;if (i=2) printf(“*n”); break;if (i=3)printf(“*n”); break; ,*,The continue Statement,continue continue; Skips the remaining statements in the body of a while, for or dowhile statement Proceeds with the next iteration of the loop while

20、and dowhile Loop-continuation test is evaluated immediately after the continue statement is executed for Increment expression is executed, then the loop-continuation test is evaluated,双语课堂 C,Example: continue,双语课堂 C,#include void main() int x;for (x = 1; x = 10; x+) if (x = 5)continue;printf(“%d “,

21、x); ,1 2 3 4 6 7 8 9 10,The goto Statement,goto goto statement-label;Change the normal program of the process control immediate implement the statement that has been identified .,双语课堂 C,this is an identifier on the front of the executable statement,合理的使用: 与if语句一起构成循环结构 从循环体内跳到循环体外 特别是从多层嵌套循环的内部调到外层循

22、环,或者直接跳出,结构化编程技术限制使用 goto 语句 滥用goto语句使得程序无结构可言,可读性差,调试和维护困难,Example:,双语课堂 C,#include void main() int s=0, i=1;Loop:s = s + i;i+;if (i=100) goto Loop;printf(“1+2+.+100=%dn“, s); ,1 2 3 4 Broke ot of loop at x = 5,语句标号,nested loop,In C language, it is allowed that the loop of the three statements (whi

23、le , do-while and for ) nest and multi-nest.The following are valid :,for( ; ; ) while( ) ,for( ; ; ) for( ; ; ) ,do do while( ); while( );,while( ) do while( ); ,while( ) for( ; ; ) ,do for( ; ; ) while( );,Example:,for( i =1; i = 2; i+) for( j=1; j=2; j+)printf(“%3d”,i+j);printf(“n”); ,output: 233

24、4,for(i=1; i=2; i+)for(j=1; j=2; j+)printf(“%3d”, i+j);printf(“n”);output:2 33 4,Example: Calculate the expression:,Analyse:(1) sum: s = s + an(2)(3) |an|10-6,双语课堂 C,#include #include void main( ) int sign;float n, t, s,pi;t = 1; s = 0; n = 1.0; sign = 1;while( (fabs(t) 1e-6 ) s = s + t;n = n + 2;si

25、gn = -sign;t = sign / n;pi = s * 4;printf(“pi=%fn”, pi); ,pi=3.14153162,Example:input 10 numbers, and find out the maximum and minimum,双语课堂 C,#include void main()int i, d, max, min;printf(“Input 10 integers:n“);scanf(“%d“, ,Example:判断一个数n是否为素数? 方法:n用2 只要能够被其中任何一个数整除,则n不是素数。,#include #include void ma

26、in( ) int m, k, i;scanf(“%d“, ,Example:find out all the prime numbers between 1 to 100.,#include #include void main() int m, k, i;printf(“prime numbers between 1 to 100:”);for (m=1;mk) printf(“%4d“, m); /* prime number */ ,Example: Print graphAnalyse: 10 rows 第n行有n个星号,双语课堂 C,* * * * * * * * * *,双语课堂

27、 C,#include void main() int m, n;for(n=1;n=10;n+) for(m=1;m=n;m+)printf(“*“);printf(“n“); ,Thinking! Print graph,双语课堂 C,* *,Example:求Fibonacci数列的40个数。 Fibonacci数列为:,1, 1, 2, 3, 5, 8, 13, ,#include void main ( ) long int f1, f2;int i;f1 = 1; f2 = 1;for( i =1; i = 20; i+) printf(“%12ld %12ld”, f1, f2);f1 = f1 + f2;f2 = f2 + f1; ,Control Structures,Control Structures in C language.,双语课堂 C,Sequence Structure,Selection Structures,switch,if,if-else,双语课堂 C,Repetition Structures,while,do-while,for,Unstructured Program,Unstructured flowchart,双语课堂 C,goto,

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 企业管理 > 管理学资料

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报