资源预览内容
第1页 / 共45页
第2页 / 共45页
第3页 / 共45页
第4页 / 共45页
第5页 / 共45页
第6页 / 共45页
第7页 / 共45页
第8页 / 共45页
第9页 / 共45页
第10页 / 共45页
亲,该文档总共45页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
第 4 章 循环,C+程序设计,2,主要内容,while 循环 do-while 循环 for 循环 循环的嵌套 转向语句 实例研究 小结及作业,C+程序设计,3,4.1 while 循环,循环是一种控制语句块重复执行的结构,是程序设计的基本概念 循环可用来控制一个操作或一个操作序列连续执行多少次 C+提供三种循环语句:while 循环,do- while循环和for 循环 while 循环的语法形式,while (表达式) 语句; ,执行顺序:先判断表达式的值,为 true 时,再执行语句,C+程序设计,4,4.1 while 循环,#include using namespace std; int main( ) int i=1,sum=0; while (i=100) sum=sum+i; i+; cout“sum=“sumendl; ,例:求1+2+3+100。用流程图表示算法,根据流程图写出程序,C+程序设计,5,4.1 while 循环,对数学学习工具程序的改进 改进一:一次生成 10 个问题,回答完毕后统计回答正确题数并给出完成整个测试所有时间,#include #include / for time function #include / for the srand and rand functions using namespace std; int main() int correctCount = 0; / Count the number of correct answers int count = 0; / Count the number of questions long startTime = time(0);,C+程序设计,6,4.1 while 循环,while (count answer;,C+程序设计,7,4.1 while 循环,/ 4. Grade the answer and display the result if (number1 - number2 = answer) cout “You are correct!n“; correctCount+; else cout “Your answer is wrong.n“ number1 “ - “ number2 “ should be “ (number1 - number2) endl; / Increase the count count+; /end of while long endTime = time(0); long testTime = endTime - startTime; cout “Correct count is “ correctCount “nTest time is “ testTime “ secondsn“; return 0; ,C+程序设计,8,4.1 while 循环,对数学学习工具程序的改进 改进二:由测试者控制是否继续生成问题,回答完毕后统计回答正确题数并给出完成整个测试所有时间,#include #include / for time function #include / for the srand and rand functions using namespace std; int main() int correctCount = 0; / Count the number of correct answers int count = 0; / Count the number of questions long startTime = time(0); char chContinue = Y;,9,4.1 while 循环,while (chContinue =Y) / Increase the count count+; / Prompt the user for confirmation? coutchContinue; ,C+程序设计,10,4.2 do-while循环,是 while 循环的一种变形,其语法形式为: do 语句; while(表达式); 执行顺序 先执行循环体语句,后判断条件;表达式为 true 时,继续执行循环体 与while 语句的比较 while 语句先判断表达式的值,为true 时,再执行语句,C+程序设计,11,4.2 do-while循环,用do-while语句求1+2+3+100,根据流程图,编写程序,#include using namespace std; int main( ) int i=1,sum=0; do sum=sum+i; i+; while (i=100); coutsum=sumendl; return 0; ,C+程序设计,12,4.3 for 循环,for 循环语法形式为: for (表达式1;表达式2;表达式3) 语句; C+中的for语句使用最为广泛和灵活,不仅可以用于循环次数已经确定的情况,而且可以用于循环次数不确定而只给出循环结束条件的情况,循环前先求解,循环条件判定,每次执行完循环体后求解,C+程序设计,13,4.3 for 循环,/用for循环语句求1+2+3+100 #include using namespace std; int main( ) int i,sum=0; for(i=1; i=100; i+) sum=sum+i; cout“sum=“sumendl; return 0; ,C+程序设计,14,4.3 for 循环,有关 for 循环的若干说明 表达式1可以是设置循环变量初值的赋值表达式 for 语句的一般格式中的“表达式1”可以省略,此时应在for语句之前给循环变量赋初值,但 “表达式1”后的分号不能省 表达式2不能省略,省略即不判断循环条件,循环无终止进行下去;若省略需要在循环体中有跳出循环的控制语句 表达式2一般是关系表达式(如i=100)或逻辑表达式(如ab & xy),但也可以是数值表达式或字符表达式,只要其值为非零,就执行循环体 表达式3包括使循环趋于结束的操作(修正循环变量),C+程序设计,15,4.3 for 循环,几种循环的比较 三种循环都可以用来处理同一问题,一般情况下它们可以互相代替 while和do-while循环,是在while后面指定循环条件的,在循环体中应包含使循环趋于结束的语句(如i+,或i=i+1等) for循环可以在表达式3中包含使循环趋于结束的操作,甚至可以将循环体中的操作全部放到表达式3中。for语句的功能更强,凡用while循环能完成的,用for循环都能实现。 用while和do-while循环时,循环变量初始化的操作应在while和do-while语句之前完成,而for语句可以在表达式1中实现循环变量的初始化,C+程序设计,16,4.3 for 循环,几种循环的使用 一般来说,如果重复次数已知,通常选择 for 循环; 如果重复次数未知,通常选择 while 循环; 当循环体必须在检验循环继续条件之前执行时可选用 do-while循环,C+程序设计,17,4.4 嵌套的循环,一个循环体内又包含另一个完整的循环结构,称为循环的嵌套 内嵌的循环中还可以嵌套循环,这就是多层循环 三种循环(while循环、do-while循环和for循环)可以互相嵌套,while( ) while( ) ,do do while( ); while( );,for( ; ; ) for( ; ; ) ,C+程序设计,18,4.4 嵌套的循环,#include #include using namespace std; int main() cout “ Multiplication Tablen“; cout “-n“; / Display the number title cout “ | “; for (int j = 1; j = 9; j+) cout setw(3) j; cout “n“;,/ Print table body for (int i = 1; i = 9; i+) cout i “ | “; for (int j = 1; j = 9; j+) / Display the product and align properly cout setw(3) i * j; cout “n“; return 0; ,使用嵌套的for循环打印乘法表,C+程序设计,19,4.5 转向语句,C+中的转向语句有 break, continue, goto 三种 break 语句 主要用在switch、while、dowhile 和 for 语句中 在switch语句中,break 用来使流程跳出switch语句,继续执行 switch 后的语句 在循环语句中,break 用来从最近的封闭循环体内跳出,C+程序设计,20,4.5 转向语句,break 语句,for(; ;) for (; ;) if (i=1) break; a=1; /break 跳至此处 , while(表达式1) if (表达式2) break; b =1; /break 跳至此处 ,C+程序设计,21,4.5 转向语句,continue 语句 只能在循环体内使用 continue语句使流程结束本次循环,进入下次循环,但并不结束整个循环 结束本次循环,程序流程转去执行对条件的判断,如果这时循环条件为真,则开始下一次循环,否则终止循环,C+程序设计,22,4.5 转向语句,continue 语句 continue 语句 和 break 语句的区别 continue 语句只结束本次循环而不是终止整个循环的执行 break 语句结束本次循环,不再进行条件判断,for (int n=100;n=200;n+) if (n%3 = 0) continue; coutnendl; ,while(表达式1) if (表达式2) continue; ,C+程序设计,23,4.5 转向语句,goto 语句 将控制从它所在的地方转移到标识符所标识的语句处 大量使用goto 语句将会使程序的流程无规律,降低程序的可读性,程序设计中尽量少用 goto 语句 小结 总是可以不使用转向控制语句的循环代码 使用转向控制语句的目的是使代码简化,程序易读,C+程序设计,24,4.6 综合示例,求循环次数: 1) while (int i=0) i-; 2) int i=5; do couti-endl; i-; while ( i !=0); 3) for (int i=0,j=10;i=j=10;i+,j-); 4) int s=3379; while (s+%2+3%2) s+;,0,无限,无限,下列语句不是死循环的是? 1) int i=100; while (1) i =i%100+1; if (i=20) break; 2) int i,sum=0; for (i=1;i+) sum=sum+1; 3) int k=0; do +k; while (k=0);,无限,20,C+程序设计,25,实例1:输入一个整数 m ,判断它是否是素数
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号