第6章 循环控制

上传人:无*** 文档编号:239648794 上传时间:2024-02-10 格式:PPT 页数:37 大小:504.50KB
返回 下载 相关 举报
第6章 循环控制_第1页
第1页 / 共37页
第6章 循环控制_第2页
第2页 / 共37页
第6章 循环控制_第3页
第3页 / 共37页
点击查看更多>>
资源描述
College of Information Science and Engineering,Wuhan University of Science and Technology第六章 循环控制概述C语言可实现循环的语句:用goto 和 if 构成循环while 语句do while 语句for 语句循环的嵌套(难点)1College of Information Science and Engineering,Wuhan University of Science and Technologygotov功能:无条件转移语句v说明:l不能用整数作标号l只能出现在goto所在函数内,且唯一l只能加在可执行语句前面l限制使用goto语句goto语句及用goto构成循环goto语句一般格式:goto 语句标号;.标号:语句;2College of Information Science and Engineering,Wuhan University of Science and Technology例 用if 和goto语句构成循环,求/*ch6_1_1.c*/#include main()int i,sum=0;i=1;loop:if(i=100)sum+=i;i+;goto loop;printf(%d,sum);sum=0+1sum=1+2=3sum=3+3=6sum=6+4sum=4950+100=5050循环初值循环初值循环终值循环终值循环变量增值循环变量增值循环条件循环条件循环体循环体p1133College of Information Science and Engineering,Wuhan University of Science and Technology/*ch6_1.c*/#include main()int number,sum=0;read_loop:scanf(%d,&number);if(!number)goto print_sum;sum+=number;goto read_loop;print_sum:printf(The total sum is%dn,sum);例 从键盘输入一组数据,以0结束输入,求数据和4College of Information Science and Engineering,Wuhan University of Science and Technologywhile语句v一般形式:while(表达式)循环体语句;v执行流程:expr循环体循环体假假(0)真真(非非0)while5College of Information Science and Engineering,Wuhan University of Science and Technologyv特点:先判断表达式,后执行循环体v说明:l循环体有可能一次也不执行l循环体可为任意类型语句l下列情况,退出while循环u条件表达式不成立(为零)u循环体内遇break,return,gotol无限循环:while(1)循环体;while语句语句6College of Information Science and Engineering,Wuhan University of Science and Technology例例 用用while循环求循环求:/*ch6_2.c*/#include main()int i,sum=0;i=1;while(i=100)sum=sum+i;i+;printf(%d,sum);循环初值循环终值循环变量增值循环条件循环体p1147College of Information Science and Engineering,Wuhan University of Science and Technology例:在屏幕上输出110的平方/*ch6_3.c*/#include main()int i=1;while(i=10)printf(%d*%d=%dn,i,i,i*i);i+;运行结果:1*1=12*2=43*3=94*4=165*5=256*6=367*7=498*8=649*9=8110*10=100注意:循环体如果有多条语句,应用花括号括起来!8College of Information Science and Engineering,Wuhan University of Science and Technologydowhile语句v一般形式:do 循环体语句;while(表达式);v执行流程:do循环体循环体expr假假(0)真真(非非0)while9College of Information Science and Engineering,Wuhan University of Science and Technologyv特点:先执行循环体,后判断表达式v说明:l至少执行一次循环体ldowhile可转化成while结构expr循环体循环体假假(0)真真(非非0)循环体循环体While循环循环10College of Information Science and Engineering,Wuhan University of Science and Technology例 用dowhile循环求/*ch6_4.c*/#include main()int i,sum=0;i=1;do sum+=i;i+;while(i=100);printf(%d,sum);11College of Information Science and Engineering,Wuhan University of Science and Technology例 while和dowhile比较/*ch6_5.c*/#include 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);12College of Information Science and Engineering,Wuhan University of Science and Technologyfor语句v一般形式:for(expr1;expr2;expr3)循环体语句;v执行流程:expr2循环体循环体假假(0)真真(非非0)forexpr1expr313College of Information Science and Engineering,Wuhan University of Science and Technologyvfor语句一般应用形式:for(循环变量赋初值;循环条件;循环变量增值)循环体语句;例 用for循环求#include main()int i,sum=0;for(i=1;i=100;i+)sum+=i;printf(%d,sum);14College of Information Science and Engineering,Wuhan University of Science and Technologyv说明:lfor语句中expr1,expr2,expr3 类型任意,都可省略,但分号;不可省l无限循环:for(;)lfor语句可以转换成while结构expr1;while(expr2)循环体语句;expr3;15College of Information Science and Engineering,Wuhan University of Science and Technology例:#include main()int i=0;for(i=0;i10;i+)putchar(a+i);运行结果:abcdefghij#includemain()int i=0;for(;i10;i+)putchar(a+i);#includemain()int i=0;for(;i10;)putchar(a+(i+);#includemain()int i=0;for(;i10;putchar(a+i),i+);16College of Information Science and Engineering,Wuhan University of Science and Technology例:p1201.for(i=0,j=100;i=j;i+,j-)2.k=i+j;2.for(i=0;(c=getchar()!=n;i+=c);3.for(;(c=getchar()!=n;)printf(“%c”,c);逗号表达式,包含两个赋值表达式p129 习题6.2可以借鉴17College of Information Science and Engineering,Wuhan University of Science and Technology循环的嵌套v三种循环可互相嵌套,层数不限v外层循环可包含两个以上内循环,但不能相互交叉v嵌套循环的执行流程嵌套循环的跳转,禁止:从外层跳入内层跳入同层的另一循环向上跳转18College of Information Science and Engineering,Wuhan University of Science and Technology(1)while()while().(2)do do while();.while();(3)while()do while();.(4)for(;)do while();while().19College of Information Science and Engineering,Wuhan University of Science and Technology例 循环嵌套,输出九九乘法表1234567891234567892468101214161836912151821242791827364554637281.ij20College of Information Science and Engineering,Wuhan University of Science and Technology/*ch6_6.c*/#include main()int i,j;for(i=1;i10;i+)printf(%4d,i);printf(n-n);for(i=1;i10;i+)for(j=1;j10;j+)printf(j=9)?%4dn:%4d,i*j);例 循环嵌套,输出九九乘法表21i10printf假假(0)真真(非非0)i=1j+j=1j10真真(非非0)假假(0)i+for(i=1;i10;i+)for(j=1;j10;j+)printf(j=9)?%4dn:%4d,i*j);外循环内循环循环嵌套22College of Information Science and Engineering,Wuhan University of Science and Technology功能:在循环语句和switch语句中,终止并跳出循环体或开关体说明:break只能终止并跳出最近一层的结构break不能用于循环语句和switch语句之外的任何其它语句之中辅助控制语句break语句23College of Information Science and Engineering,Wuhan University of Science and Technologyexprbreak;假假(0)真真(非非0)whiledobreak;.expr假假(0)真真(非非0)while24College of Information Science and Engineering,Wuhan University of Science and Technologyexpr2break;.假(0)真(非0)forexpr1expr3switchexpr语句组语句组1break;语句组语句组2break;语句组语句组nbreak;语句组语句组break;.const 1const 2const ndefaultcase 25College of Information Science and Engineering,Wuhan University of Science and Technology例 break举例:输出圆面积,面积大于100时停止/*ch6_7.c*/#define PI 3.14159main()int r;float area;for(r=1;r100)break;printf(r=%d,area=%.2fn,r,area);26College of Information Science and Engineering,Wuhan University of Science and Technology例:小写字母转换成大写字母,直至输入非小写字母字符#include main()int i,j;char c;while(1)c=getchar();if(c=a&c=z)putchar(c-a+A);else break;27continue语句v功能:结束本次循环,跳过循环体中尚未执行的语句,进行下一次是否执行循环体的判断v仅用于循环语句中exprcontinue;假假(0)真真(非非0)while真真(非非0)docontinue;.expr假假(0)whileexpr2continue;.假假(0)真真(非非0)forexpr1expr328College of Information Science and Engineering,Wuhan University of Science and Technology例 求输入的十个整数中正数的个数及其平均值#include/*ch6_10.c*/main()int i,num=0,a;float sum=0;for(i=0;i10;i+)scanf(%d,&a);if(a=0)continue;num+;sum+=a;printf(%d plus integers sum:%6.0fn,num,sum);printf(Mean value:%6.2fn,sum/num);29College of Information Science and Engineering,Wuhan University of Science and Technology程序举例 p124t=1,pi=0,n=1.0,s=1当|t|1e-6pi=pi+tn=n+2s=-st=s/npi=pi*4输出pi分子:分子:1,-1,1,-1分母:分母:1,3,5,7,.30College of Information Science and Engineering,Wuhan University of Science and Technology例 求Fibonacci数列:1,1,2,3,5,8,的前40个数f1=1,f2=1for i=1 to 20输出f1,f2f1=f1+f2f2=f2+f11534233159710946750255142293524578241578171855377258417711121393832040570288739088169213896104181286571964181346269922746563245986321144987676546368317811217830914930352102334155p12531College of Information Science and Engineering,Wuhan University of Science and Technology例 判断m是否素数 p126读入mk=mi=2当ikm被i整除真假用break结束循环i=i+1ik+1真假输出:m”是素数”输出:m”不是素数”在此基础上,试编程输出100200 之间的全部素数。32College of Information Science and Engineering,Wuhan University of Science and Technology例 译密码 p128例如 Hello,world!译成密码:Lipps,asvph!26个字母按顺序构成封闭的环状,将任意一字母变成其后的第4个字母.非字母字符不变33College of Information Science and Engineering,Wuhan University of Science and Technology#includemain()char c;while(c=getchar()!=n)if(c=a&c=A&cZ&cz)c=c-26;printf(“%c”,c);例 译密码34College of Information Science and Engineering,Wuhan University of Science and Technology课堂练习:1、int k=10;while(k=0)k=k-1;2、int k=10;while(k=0)k-;3、while(!E);中表达式!E等价于什么?4、for(i=1;i+4;);执行后i=?执行结束后k=?35College of Information Science and Engineering,Wuhan University of Science and Technology鸡兔共有30只,脚共有90只,以下程序求各有多少鸡和兔。请填空。for(x=1;x=29;x+)y=30-x;if()printf(“%d,%dn”,x,y);36College of Information Science and Engineering,Wuhan University of Science and Technology作业:用循环程序完成下列各题p1296.2、6.3、6.6总结:本章主要介绍循环控制用到的语句和方法while,do while,for另外还有起辅助作用的break,continue语句难点:尽快熟悉循环控制程序的基本技巧!多动手练习!37
展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 管理文书 > 施工组织


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!