欢迎来到第壹文秘! | 帮助中心 分享价值,成长自我!
第壹文秘
全部分类
  • 幼儿/小学教育>
  • 中学教育>
  • 高等教育>
  • 研究生考试>
  • 外语学习>
  • 资格/认证考试>
  • 论文>
  • IT计算机>
  • 法律/法学>
  • 建筑/环境>
  • 通信/电子>
  • 医学/心理学>
  • ImageVerifierCode 换一换
    首页 第壹文秘 > 资源分类 > PPT文档下载
    分享到微信 分享到微博 分享到QQ空间

    第6章 循环控制.ppt

    • 资源ID:570040       资源大小:900.50KB        全文页数:35页
    • 资源格式: PPT        下载积分:10金币
    快捷下载 游客一键下载
    账号登录下载
    三方登录下载: 微信开放平台登录 QQ登录
    下载资源需要10金币
    邮箱/手机:
    温馨提示:
    快捷下载时,如果您不填写信息,系统将为您自动创建临时账号,适用于临时下载。
    如果您填写信息,用户名和密码都是您填写的【邮箱或者手机号】(系统自动生成),方便查询和重复下载。
    如填写123,账号就是123,密码也是123。
    支付方式: 支付宝    微信支付   
    验证码:   换一换

    加入VIP,免费下载
     
    账号:
    密码:
    验证码:   换一换
      忘记密码?
        
    友情提示
    2、PDF文件下载后,可能会被浏览器默认打开,此种情况可以点击浏览器菜单,保存网页到桌面,就可以正常下载了。
    3、本站不支持迅雷下载,请使用电脑自带的IE浏览器,或者360浏览器、谷歌浏览器下载即可。
    4、本站资源下载后的文档和图纸-无水印,预览文档经过压缩,下载后原文更清晰。
    5、试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。

    第6章 循环控制.ppt

    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只能加在可执行语句前面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=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 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特点:先判断表达式,后执行循环体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,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注意:循环体如果有多条语句,应用花括号括起来!College 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结构expr循环体循环体假假(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 Science 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(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 Information 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(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+,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 Information 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例 循环嵌套,输出九九乘法表1234567891234567892468101214161836912151821242791827364554637281.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=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

    注意事项

    本文(第6章 循环控制.ppt)为本站会员(p**)主动上传,第壹文秘仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知第壹文秘(点击联系客服),我们立即给予删除!

    温馨提示:如果因为网速或其他原因下载失败请重新下载,重复下载不扣分。




    关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

    copyright@ 2008-2023 1wenmi网站版权所有

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

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

    收起
    展开