C程序设计第五章循环_第1页
C程序设计第五章循环_第2页
C程序设计第五章循环_第3页
C程序设计第五章循环_第4页
C程序设计第五章循环_第5页
已阅读5页,还剩21页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、C Control Statements: Looping 电子对抗研究所C语言可实现循环的语句语言可实现循环的语句nwhile语句语句ndo while语句语句nfor语句语句ngoto语句和语句和if语句构成循环语句构成循环循环控制 循环的本质循环的本质:不断地重复某种动作。 对计算机程序而言,循环必须具备两个重要因素:对计算机程序而言,循环必须具备两个重要因素:1. 在一定的条件下,重复执行一组指令2. 必然出现不满足条件的情况,使循环终止nThis is the general form of the while loop: while (expression) statement n

2、The statement part can be a simple statement with a terminating semicolon, or it can be a compound statement enclosed in bracesThe while Statement执行流程执行流程expr循环体假(0)真(非0)whileWhile语句特点和说明语句特点和说明v特点:先判断表达式,后执行循环体v说明:l循环体有可能一次也不执行l循环体可为任意类型语句l下列情况,退出while循环u条件表达式不成立(为零)u循环体内遇break,return,gotol无限循环: wh

3、ile(1) 循环体;用while循环求 1001nn/*ch5_2.c*/#include main() int i,sum=0; i=1; while(i=100) sum=sum+i; i+; printf(%d,sum);循环初值循环终值循环变量增值循环条件循环体显示显示110的平方的平方/*ch5_21.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=100dowhil

4、e语句语句do 循环体语句; while(表达式);v执行流程:do循环体expr假(0)真(非0)whiledowhile语句特点语句特点v特点:先执行循环体,后判断表达式v说明:l至少执行一次循环体ldowhile可转化成while结构expr循环体假(0)真(非0)循环体While循环用do-while循环求 1001nn#include main() int i,sum=0; i=100; dosum=sum+i;while(i-); printf(%d,sum);循环条件循环体for语句语句for(expr1 ; expr2 ; expr3) 循环体语句;v执行流程:expr2循环体

5、假(0)真(非0)forexpr1expr3for语句一般应用形式语句一般应用形式for(循环变量赋初值;循环条件;循环表达式)循环体语句;v说明:lfor语句中expr1, expr2 ,expr3 类型任意,都可省略,但 分号;不可省l无限循环: for(;)lfor语句可以转换成while结构expr1;while(expr2)循环体语句;expr3;for语句举例语句举例例:#include main( ) int i=0; for(i=0;i10;i+) putchar(a+i); 运行结果:abcdefghij例:#include main( ) int i=0; for(;i10

6、;i+) putchar(a+i); 例:#include main( ) int i=0; for(;i10;putchar(a+i),i+) ; 循环的嵌套循环的嵌套v三种循环可互相嵌套,层数不限v外层循环可包含两个以上内循环,但不能相互交叉v嵌套循环的执行流程(1) while() while() . (2) do do while( ); . while( );(3) while() do while( ); . (4) for( ; ;) do while(); while() .内循环外循环内循环v嵌套循环的跳转禁止:l从外层跳入内层l跳入同层的另一循环l向上跳转i10printf

7、假(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);外循环内循环循环的嵌套(图解)内循环goto语句及用语句及用goto构成循环构成循环n功能:无条件转移语句n说明:n不能用整数作标号n只能出现在goto所在函数内,且唯一n只能加在可执行语句前面n限制使用goto语句 goto 语句标号; .标号:语句; 使用使用If和和goto语句构成循环语句构成循环/*ch5_1.c*/#include main() int i,sum=0; i=1;loop: if(i=100)

8、 sum+=i; i+; goto loop; printf(%d,sum);sum=0+1sum=1+2=3sum=3+3=6sum=6+4sum=4950+100=5050循环初值循环终值循环变量增值循环条件循环体1001nn辅助控制语句辅助控制语句nbreak语句 The break Statement n功能:在循环语句和switch语句中,终止并跳出循环体或开关体n说明:nbreak只能终止并跳出最近一层的结构nbreak不能用于循环语句和switch语句之外的任何其它语句之中nA break statement in a loop causes the program to bre

9、ak free of the loop that encloses it and to proceed to the next stage of the program. 图解图解exprbreak;假(0)真(非0)whiledobreak;.expr假(0)真(非0)while图解图解expr2break;.假(0)真(非0)forexpr1expr3switchexpr语句组1break;语句组2break;语句组nbreak;语句组break;.const 1const 2const ndefaultcase continue语句The continue Statement n功能:结

10、束本次循环,跳过循环体中尚未执行的语句,进行下一次是否执行循环体的判断n仅用于循环语句中nThis statement can be used in the three loop forms. When encountered, it causes the rest of an iteration to be skipped and the next iteration to be started. nIf the continue statement is inside nested structures, it affects only the innermost structure co

11、ntaining it. exprcontinue;假(0)真(非0)while真(非0)docontinue;.expr假(0)whileexpr2continue;.假(0)真(非0)forexpr1expr3continuenAnother use for continue is as a placeholder. For example, the following loop reads and discards input up to, and including, the end of a line:nwhile (getchar() != n) ; nSuch a techniq

12、ue is handy when a program has already read some input from a line and needs to skip to the beginning of the next line. The problem is that the lone semicolon is hard to spot. more readable while (getchar() != n) continue; nDont use continue if it complicates rather than simplifies the code. Consider the following fragment, for example:while (ch = getchar() ) != n) if (ch = t) continue; putchar(ch); continuenThis loop skips over the tabs and quits only when a newline character is encounter

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论