1 / 45

第五章 循环结构程序设计

第五章 循环结构程序设计. 概述 C 语言可实现循环的语句: 用goto 和 if 构成循环 while 语句 do ~ while 语句 for 语句. while. 假 (0). expr. 真 ( 非 0). 循环体. while 语句 一般形式 :. while(表达式) 循环体语句;. 执行流程 :. 特点:先判断表达式,后执行循环体 说明: 循环体有可能一次也不执行 While语句中的表达式必须用圆括号括住 循环体可为任意类型语句 下列情况,退出 while 循环 条件表达式不成立(为零)

zudora
Download Presentation

第五章 循环结构程序设计

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 第五章 循环结构程序设计

  2. 概述 • C语言可实现循环的语句: • 用goto 和 if 构成循环 • while 语句 • do ~ while 语句 • for 语句

  3. while 假(0) expr 真(非0) 循环体 • while语句 • 一般形式: while(表达式) 循环体语句; • 执行流程:

  4. 特点:先判断表达式,后执行循环体 • 说明: • 循环体有可能一次也不执行 • While语句中的表达式必须用圆括号括住 • 循环体可为任意类型语句 • 下列情况,退出while循环 • 条件表达式不成立(为零) • 循环体内遇break,return,goto • 无限循环: while(1) 循环体;

  5. 循环初值 循环变量增值 循环条件 循环终值 循环体 例 用while循环求 /*ch5_2.c*/ #include <stdio.h> main() { int i,sum=0; i=1; while(i<=100) { sum=sum+i; i++; } printf("%d",sum); }

  6. /*ch5_2.c*/ #include <stdio.h> main() { int i,sum=0; i=0; while(i<100) {i++; sum=sum+i; } printf("%d",sum); }

  7. 从键盘连续输入若干个字符(以回车换行为结束标志),统计字符个数(不含回车换行符)从键盘连续输入若干个字符(以回车换行为结束标志),统计字符个数(不含回车换行符) # include <stdio.h> main() { int t=0; char c; while((c=getchar())!=‘\n’) t++; printf(“number n=%d\n”,n); }

  8. 练习1 判断下列程序段的输出结果 int x=0,s=0; while(!x!=0) s+=++x; printf(“%d”,x); 练习2 判断下面程序段的运行结果 a=1;b=2;c=2; while(a<b<c) {t=a; a=b; b=t; c--;} Printf(“%d,%d,%d”,a,b,c);

  9. 练习3 运行结果: 1*1=1 2*2=4 3*3=9 4*4=16 5*5=25 6*6=36 7*7=49 8*8=64 9*9=81 10*10=100 /*ch5_21.c*/ #include <stdio.h> main() { int i=1; while(i<=10) { printf("%d*%d=%d\n",i,i,i*i); i++; } }

  10. do 循环体 while 真(非0) expr 假(0) • do~while语句 • 一般形式: do 循环体语句; while(表达式); • 执行流程:

  11. 循环体 While循环 假(0) expr 真(非0) 循环体 • 特点:先执行循环体,后判断表达式 • 说明: • 至少执行一次循环体 • While语句中的表达式必须用圆括号括住,后面要加一个分号 • do~while可转化成while结构

  12. 例 用do~while循环求 /*ch5_3.c*/ #include <stdio.h> main() { int i,sum=0; i=1; do { sum+=i; i++; }while(i<=100); printf("%d",sum); }

  13. 例 while和do~while比较 /*ch5_4.c*/ #include <stdio.h> main() { int i,sum=0; scanf("%d",&i); do { sum+=i; i++; }while(i<=10); printf("%d",sum); } main() { int i,sum=0; scanf("%d",&i); while(i<=10) { sum+=i; i++; } printf("%d",sum); }

  14. 判断下列程序的输出结果: main() {int a=1,b=10; do {b-=a;a++;} while(b--<0); printf(“a=%d,b=%d”,a,b); }

  15. 例:用下列公式计算π的值。 π/4=1-1/3+1/5-1/7+…+1/n(精度要求为|1/n|<10-6) 设:n —分母 s—符号分子 pi —累加器 t —存放某一项的值 #include<math.h> main() {float pi=0.0,n=1.0,s=1.0,t; do {t=s/n; 求当前项 pi=pi+t; 将当前项累加到pi中 s=s*(-1.0); 改变当前项的符号 n=n+2;} 产生下一个当前项的分母 while(fabs(t)>=1e-6); printf(“%f”,4*pi); }

  16. for expr1 假(0) expr2 真(非0) 循环体 expr3 • for语句 • 一般形式: for([expr1] ;[ expr2] ;[ expr3]) 循环体语句; • 执行流程:

  17. 例 用for循环求 #include <stdio.h> main() { int i,sum=0; for(i=1;i<=100;i++) sum+=i; printf("%d",sum); } • for语句一般应用形式: for(循环变量赋初值;循环条件;循环变量增值) { 循环体语句; } • for语句可以转换成while结构 expr1; while(expr2) { 循环体语句; expr3; }

  18. 说明: (1) “循环变量赋初值”、“循环继续条件”和“循环变量增值”部分均可缺省,甚至全部缺省,但其间的分号不能省略。 (2) 当循环体语句组仅由一条语句构成时,可以不使用复合语句形式。 (3) “循环变量赋初值”表达式既可以是给循环变量赋初值的赋值表达式,也可以是与此无关的其他表达式(如逗号表达式)。例如: for(i=1;i<=100;i++)sum+=i; /*有关的赋值表达式*/ for(sum=0;i<=100;i++)sum+=i; /*无关的表达式(i在for之前赋值)*/ for(sum=0,i=1;i<=100;i++)sum+=i; /*逗号表达式*/ (4)“循环继续条件”部分是一个逻辑量,除一般的关系表达式或逻辑表达式外,也允许是数值或字符表达式。

  19. 例:#include<stdio.h> main( ) { int i=0; for(;i<10;i++) putchar(‘a’+i); } 例:#include<stdio.h> main( ) { int i=0; for(i=0;i<10;i++) putchar(‘a’+i); } 例:#include<stdio.h> main( ) { int i=0; for(;i<10;) putchar(‘a’+(i++)); } 例:#include<stdio.h> main( ) { int i=0; for(;i<10;putchar(‘a’+i),i++) ; } 运行结果:abcdefghij

  20. main() { int i,j,k; for(i=0,j=100;i<=j;i++,j--) { k=i+j; printf("%d+%d=%d\n",i,j,k); } } #include<stdio.h> main() { char c; for(;(c=getchar())!='\n';) printf("%c ",c); } #include <stdio.h> main() { int i,c; for(i=0;(c=getchar())!='\n';i+=3) printf("%c ",i+c); }

  21. 循环结构的嵌套 若循环语句中的循环体内又完整地包含另一个或多个循环语句,称为循环嵌套。前面介绍的三种循环都可以相互嵌套。循环的嵌套可以多层,但每一层循环在逻辑上必须是完整的。

  22. 外循环 内循环 内循环 • 循环的嵌套 • 三种循环可互相嵌套,层数不限 • 外层循环可包含两个以上内循环,但不能相互交叉 • 嵌套循环的执行流程 (1) while() { …… while() { …… } …... } (4) for( ; ;) { …… do { …… }while(); …… while() { …… } …... } (3) while() { …… do { …… }while( ); ……. } (2) do { …… do { …… }while( ); …... }while( );

  23. main() {int i,b,k=0; for(i=1;i<=5;i++) {b=i%2; while(b-- >=0) k++; } printf(“%d,%d”,k,b); } 8,-2

  24. 从键盘输入65 , 14 main() {int m,n; printf(“enter m,n:”); scanf(“%d,%d”,&m,&n); while(m!=n) { while(m>n) { m-=n; while(n>m)n-=m;} } printf(“m=%d\n”,m); }

  25. i 1 2 3 1 9 j 2 4 6 2 18 6 3 9 27 3 4 8 4 36 12 5 5 10 45 15 12 54 6 6 18 63 7 14 7 21 72 16 24 8 8 9 9 18 81 27 …………….. 例 循环嵌套,输出九九表 /*ch5_5.c*/ #include <stdio.h> main() { int i,j; for(i=1;i<10;i++) printf("%4d",i); printf("\n---------------------------------------\n"); for(i=1;i<10;i++) {for(j=1;j<10;j++) printf(“%4d",i*j); printf(“\n”); } }

  26. for(i=1;i<10;i++) {for(j=1;j<10;j++) printf(“%4d",i*j); printf(“\n”); } 外循环 i=1 假(0) i<10 内循环 真(非0) j=1 假(0) j<10 真(非0) printf j++ i++

  27. 输出下列图形 main() {int i,j,k; for(i=1;i<=5;i++) {for(j=5;j>=i;j--) printf(“ ”); for(k=1;k<=2*i-1;k++) printf(“@”); printf(“\n”); } } @ @@@ @@@@@ @@@@@@@ @@@@@@@@@

  28. 辅助控制语句 break语句 功能:在循环语句和switch语句中,终止并跳出循环体或开关体 说明: break只能终止并跳出最近一层的结构 break不能用于循环语句和switch语句之外的任何其它语句之中

  29. while do 假(0) …… break; …... expr 真(非0) …… break; …… while 真(非0) expr 假(0)

  30. for switch expr expr1 case 假(0) expr2 const 1 const 2 const n default 真(非0) 语句组1 break; 语句组2 break; 语句组n break; 语句组 break; …... …… break; …... expr3

  31. break举例:输出圆面积,面积大于100时停止 #define PI 3.14159 main() { int r; float area; for(r=1;r<=10;r++) { area=PI*r*r; if(area>100) break; printf("r=%d,area=%.2f\n",r,area); } }

  32. main() { int i,j,a=0; for(i=0;i<2;i++) {for(j=0;j<4;j++) {if(j%2) break; a++;} a++;} Printf(“%d\n”,a); } 4

  33. while for do expr1 假(0) …… continue; …... expr 假(0) 真(非0) expr2 …… continue; …… 真(非0) while 真(非0) …… continue; …... expr 假(0) expr3 • continue语句 • 功能:结束本次循环,跳过循环体中尚未执行的语句,进行下一次是否执行循环体的判断 • 仅用于循环语句中

  34. 求输入的十个整数中正数的个数及其平均值 /*ch5_12.c*/ #include <stdio.h> main() { int i,num=0,a; float sum=0; for(i=0;i<10;i++) { scanf("%d",&a); if(a<=0) continue; num++; sum+=a; } printf("%d plus integer's sum :%6.0f\n",num,sum); printf("Mean value:%6.2f\n",sum/num); }

  35. 2 5 8 11 14 下面程序的运行结果是: #include <stdio.h> main() { int i=1; while(i<=15) if(++i%3!=2) continue; else printf(“%d”,i); }

  36. 下列程序的运行结果是: main() {int i ; for(i=1;i<=5;i++) {if(i%2) printf(“*”); else continue; printf(“#”); } printf(“$\n”); }

  37. 8 下面程序的运行结果是: #include <stdio.h> main() { int a,b; for(a=1,b=1;a<=100;a++) { if(b>=20) break; if(b%3==1) {b+=3; continue;} b-=5; } printf(“%d\n”,a); }

  38. break和continue语句对循环控制的影响 continue语句和break语句的区别是:continue语句只结束本次循环,而不是终止整个循环的执行,而break语句则是结束整个循环过程,不再判断执行循环的条件是否成立。 循环嵌套时,break和continue只影响包含它们的最内层循环,与外层循环无关。

  39. 程序举例 例一:计算1!+2!+3!+……+n! 设变量: n--表达式中n k--循环变量 表示1、2…… sum1--存放某个数的阶乘值 sum2--存放累加和

  40. 程序如下: main() { int k,n; long sum1=1,sum2=0; scanf(“%d”,&n); for(k=1;k<=n;k++) { sum1*=k; sum2+=sum1; } printf(“1!+2!+3!+…+%ld=%ld”,n,sum2); }

  41. 1 5 34 233 1597 10946 75025 514229 3524578 24157817 1 8 55 377 2584 17711 121393 832040 5702887 39088169 2 13 89 610 4181 28657 196418 1346269 9227465 63245986 3 21 144 987 6765 46368 317811 2178309 14930352 102334155 f1=1,f2=1 for i=1 to 20 输出f1,f2 f1=f1+f2 f2=f2+f1 例 求Fibonacci数列:1,1,2,3,5,8,……的前40个数

  42. 方法一: #include <stdio.h> main() { long int f1,f2; int i; f1=1; f2=1; for(i=1;i<=20;i++) { printf("%12ld %12ld ",f1,f2); if(i%2==0) printf("\n"); f1=f1+f2; f2=f2+f1; } }

  43. 方法二: main() {long int p1=1,p2=1,n=2,t; printf("%12ld",p1); printf("%12ld",p2); do {t=p1+p2; p1=p2; p2=t; printf("%12ld",t); n++; if(n%4==0) printf("\n"); } while(n<40); }

  44. ABCDEFGHIJKLMNOPQRSTUVWXYZ 例 译密码 例如 Hello,world! 译成密码:Lipps,asvph!

  45. #include <stdio.h> main() { char c; while((c=getchar())!='\n') { if((c>='a'&&c<='z')||(c>='A'&&c<='Z')) { c=c+4; if(c>'Z'&&c<='Z'+4||c>'z') c=c-26; } printf("%c",c); } }

More Related