第6章 循环控制.ppt

上传人:王** 文档编号:612151 上传时间:2023-12-08 格式:PPT 页数:35 大小:900.50KB
下载 相关 举报
第6章 循环控制.ppt_第1页
第1页 / 共35页
第6章 循环控制.ppt_第2页
第2页 / 共35页
第6章 循环控制.ppt_第3页
第3页 / 共35页
第6章 循环控制.ppt_第4页
第4页 / 共35页
第6章 循环控制.ppt_第5页
第5页 / 共35页
第6章 循环控制.ppt_第6页
第6页 / 共35页
第6章 循环控制.ppt_第7页
第7页 / 共35页
第6章 循环控制.ppt_第8页
第8页 / 共35页
第6章 循环控制.ppt_第9页
第9页 / 共35页
第6章 循环控制.ppt_第10页
第10页 / 共35页
亲,该文档总共35页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《第6章 循环控制.ppt》由会员分享,可在线阅读,更多相关《第6章 循环控制.ppt(35页珍藏版)》请在优知文库上搜索。

1、College of Information Science and Engineering,Wuhan University of Science and Technology1第六章 循环控制概述C语言可实现循环的语句:用goto 和 if 构成循环while 语句do while 语句for 语句循环的嵌套(难点)College of Information Science and Engineering,Wuhan University of Science and Technology2gotov功能:无条件转移语句v说明:l不能用整数作标号l只能出现在goto所在函数内,且唯一l只

2、能加在可执行语句前面l限制使用goto语句goto语句及用goto构成循环goto语句一般格式:goto 语句标号;.标号:语句;College of Information Science and Engineering,Wuhan University of Science and Technology3例 用if 和goto语句构成循环,求1001/*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=

3、6+4sum=4950+100=5050循环初值循环初值循环终值循环终值循环变量增值循环变量增值循环条件循环条件循环体循环体p113College of Information Science and Engineering,Wuhan University of Science and Technology4/*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

4、 total sum is%dn,sum);例 从键盘输入一组数据,以0结束输入,求数据和College of Information Science and Engineering,Wuhan University of Science and Technology5while语句v一般形式:while(表达式)循环体语句;v执行流程:expr循环体循环体假假(0)真真(非非0)whileCollege of Information Science and Engineering,Wuhan University of Science and Technology6v特点:先判断表达式,后执

5、行循环体v说明:l循环体有可能一次也不执行l循环体可为任意类型语句l下列情况,退出while循环u条件表达式不成立(为零)u循环体内遇break,return,gotol无限循环:while(1)循环体;while语句语句College of Information Science and Engineering,Wuhan University of Science and Technology7例例 用用while循环求循环求:1001nn/*ch6_2.c*/#include main()int i,sum=0;i=1;while(i=100)sum=sum+i;i+;printf(%d

6、,sum);循环初值循环终值循环变量增值循环条件循环体p114College of Information Science and Engineering,Wuhan University of Science and Technology8例:在屏幕上输出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注意:循环体如果有多条语句,应用花括号括起来!Col

7、lege of Information Science and Engineering,Wuhan University of Science and Technology9dowhile语句v一般形式:do 循环体语句;while(表达式);v执行流程:do循环体循环体expr假假(0)真真(非非0)whileCollege of Information Science and Engineering,Wuhan University of Science and Technology10v特点:先执行循环体,后判断表达式v说明:l至少执行一次循环体ldowhile可转化成while结构ex

8、pr循环体循环体假假(0)真真(非非0)循环体循环体While循环循环College of Information Science and Engineering,Wuhan University of Science and Technology11例 用dowhile循环求 1001nn/*ch6_4.c*/#include main()int i,sum=0;i=1;do sum+=i;i+;while(i=100);printf(%d,sum);College of Information Science and Engineering,Wuhan University of Scie

9、nce and Technology12例 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);College of Information Science and Engineering,Wuhan University of Science and Technology13for语句v一般形式:for

10、(expr1;expr2;expr3)循环体语句;v执行流程:expr2循环体循环体假假(0)真真(非非0)forexpr1expr3College of Information Science and Engineering,Wuhan University of Science and Technology14vfor语句一般应用形式:for(循环变量赋初值;循环条件;循环变量增值)循环体语句;例 用for循环求 1001nn#include main()int i,sum=0;for(i=1;i=100;i+)sum+=i;printf(%d,sum);College of Inform

11、ation Science and Engineering,Wuhan University of Science and Technology15v说明:lfor语句中expr1,expr2,expr3 类型任意,都可省略,但分号;不可省l无限循环:for(;)lfor语句可以转换成while结构expr1;while(expr2)循环体语句;expr3;College of Information Science and Engineering,Wuhan University of Science and Technology16例:#include main()int i=0;for(

12、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+);College of Information Science and Engineering,Wuhan University of Science and Technology17例:p1201.for(i=0,j=100;i=j;i+

13、,j-)k=i+j;2.for(i=0;(c=getchar()!=n;i+=c);3.for(;(c=getchar()!=n;)printf(“%c”,c);逗号表达式,包含两个赋值表达式p129 习题6.2可以借鉴College of Information Science and Engineering,Wuhan University of Science and Technology18循环的嵌套v三种循环可互相嵌套,层数不限v外层循环可包含两个以上内循环,但不能相互交叉v嵌套循环的执行流程嵌套循环的跳转,禁止:从外层跳入内层跳入同层的另一循环向上跳转College of Info

14、rmation Science and Engineering,Wuhan University of Science and Technology19(1)while()while().(2)do do while();.while();(3)while()do while();.(4)for(;)do while();while().College of Information Science and Engineering,Wuhan University of Science and Technology20例 循环嵌套,输出九九乘法表1234567891234567892468101

15、214161836912151821242791827364554637281.ijCollege of Information Science and Engineering,Wuhan University of Science and Technology21/*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);例 循环嵌套,输出九九乘法表i10printf假假(0)真真(非非0)i

16、=1j+j=1j10真真(非非0)假假(0)i+for(i=1;i10;i+)for(j=1;j10;j+)printf(j=9)?%4dn:%4d,i*j);外循环内循环循环嵌套College of Information Science and Engineering,Wuhan University of Science and Technology23功能:在循环语句和switch语句中,终止并跳出循环体或开关体说明:break只能终止并跳出最近一层的结构break不能用于循环语句和switch语句之外的任何其它语句之中辅助控制语句break语句College of Information Science and Engineering,Wuhan University of Science and Technology24exprbreak;假假(0)真真(非非0)whiledobreak;.expr假假(0)真真(非非0)whileCollege of Information Science and Engineering,Wuhan University of Scien

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

当前位置:首页 > 高等教育 > 大学课件

copyright@ 2008-2023 yzwku网站版权所有

经营许可证编号:宁ICP备2022001189号-2

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