循环语句学习.pptx
《循环语句学习.pptx》由会员分享,可在线阅读,更多相关《循环语句学习.pptx(45页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。
1、C+程序设计1主要内容while 循环do-while 循环for 循环循环的嵌套转向语句实例研究小结及作业第1页/共45页C+程序设计24.1 while 循环循环是一种控制语句块重复执行的结构,是程序设计的基本概念循环可用来控制一个操作或一个操作序列连续执行多少次C+提供三种循环语句:while 循环,do-while循环和for 循环while 循环的语法形式while(表达式)语句;可以是复合语句,其中必须含有改变条件表达式值的语句执行顺序:先判断表达式的值,为 true 时,再执行语句第2页/共45页C+程序设计34.1 while 循环#include using namespac
2、e std;int main()int i=1,sum=0;while(i=100)sum=sum+i;i+;coutsum=sumendl;例:求1+2+3+100。用流程图表示算法,根据流程图写出程序开始int i=1,sum=0;i=100?sum=sum+i;i+;Tcoutsum=sumendl;结束F第3页/共45页C+程序设计44.1 while 循环对数学学习工具程序的改进改进一:一次生成 10 个问题,回答完毕后统计回答正确题数并给出完成整个测试所有时间#include#include /for time function#include /for the srand and
3、 rand functionsusing 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);第4页/共45页C+程序设计54.1 while 循环 while(count 10)/1.Generate two random single-digit integers srand(time(0);int number1=rand()%10;int numbe
4、r2=rand()%10;/2.If number1 number2,swap number1 with number2 if(number1 number2)int temp=number1;number1=number2;number2=temp;/3.Prompt the student to answer what is number1?number2?cout What is number1 -number2 answer;第5页/共45页C+程序设计64.1 while 循环 /4.Grade the answer and display the result if(number1
5、-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;retu
6、rn 0;第6页/共45页C+程序设计74.1 while 循环对数学学习工具程序的改进改进二:由测试者控制是否继续生成问题,回答完毕后统计回答正确题数并给出完成整个测试所有时间#include#include /for time function#include /for the srand and rand functionsusing namespace std;int main()int correctCount=0;/Count the number of correct answers int count=0;/Count the number of questions long
7、startTime=time(0);char chContinue=Y;第7页/共45页C+程序设计84.1 while 循环 while(chContinue=Y)/Increase the count count+;/Prompt the user for confirmation?coutchContinue;第8页/共45页C+程序设计94.2 do-while循环是 while 循环的一种变形,其语法形式为:do 语句;while(表达式);执行顺序 先执行循环体语句,后判断条件;表达式为 true 时,继续执行循环体与while 语句的比较while 语句先判断表达式的值,为tru
8、e 时,再执行语句可以是复合语句,其中必须含有改变条件表达式值的语句第9页/共45页C+程序设计104.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;开始int i=1,sum=0;i=100?sum=sum+i;i+;Tcoutsum=sumendl;结束F第10页/共45页C+程序设计114.3 for 循环for 循环语法形式为:for
9、(表达式1;表达式2;表达式3)语句;C+中的for语句使用最为广泛和灵活,不仅可以用于循环次数已经确定的情况,而且可以用于循环次数不确定而只给出循环结束条件的情况循环前先求解循环条件判定每次执行完循环体后求解第11页/共45页C+程序设计124.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;coutsum=sumendl;return 0;int i=1,sum=0;for(;i=100;i+)for(int i=1,sum=0;
10、i=100;i+)第12页/共45页C+程序设计134.3 for 循环有关 for 循环的若干说明表达式1可以是设置循环变量初值的赋值表达式for 语句的一般格式中的“表达式1”可以省略,此时应在for语句之前给循环变量赋初值,但“表达式1”后的分号不能省表达式2不能省略,省略即不判断循环条件,循环无终止进行下去;若省略需要在循环体中有跳出循环的控制语句表达式2一般是关系表达式(如i=100)或逻辑表达式(如ab&xy),但也可以是数值表达式或字符表达式,只要其值为非零,就执行循环体表达式3包括使循环趋于结束的操作(修正循环变量)第13页/共45页C+程序设计144.3 for 循环几种循环
11、的比较三种循环都可以用来处理同一问题,一般情况下它们可以互相代替while和do-while循环,是在while后面指定循环条件的,在循环体中应包含使循环趋于结束的语句(如i+,或i=i+1等)for循环可以在表达式3中包含使循环趋于结束的操作,甚至可以将循环体中的操作全部放到表达式3中。for语句的功能更强,凡用while循环能完成的,用for循环都能实现。用while和do-while循环时,循环变量初始化的操作应在while和do-while语句之前完成,而for语句可以在表达式1中实现循环变量的初始化第14页/共45页C+程序设计154.3 for 循环几种循环的使用一般来说,如果重复
12、次数已知,通常选择 for 循环;如果重复次数未知,通常选择 while 循环;当循环体必须在检验循环继续条件之前执行时可选用 do-while循环第15页/共45页C+程序设计164.4 嵌套的循环一个循环体内又包含另一个完整的循环结构,称为循环的嵌套内嵌的循环中还可以嵌套循环,这就是多层循环三种循环(while循环、do-while循环和for循环)可以互相嵌套while()while()do do while();while();for(;)for(;)第16页/共45页C+程序设计174.4 嵌套的循环#include#include using namespace std;int m
13、ain()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循环打印乘法表第17页/共45页C+程序设计184.5 转向语句C+中的转向语句有 br
14、eak,continue,goto 三种break 语句主要用在switch、while、dowhile 和 for 语句中在switch语句中,break 用来使流程跳出switch语句,继续执行 switch 后的语句在循环语句中,break 用来从最近的封闭循环体内跳出语句1语句2第18页/共45页C+程序设计194.5 转向语句break 语句for(;)for(;)if(i=1)break;a=1;/break 跳至此处 while(表达式1)if(表达式2)break;b=1;/break 跳至此处第19页/共45页C+程序设计204.5 转向语句continue 语句只能在循环体
15、内使用continue语句使流程结束本次循环,进入下次循环,但并不结束整个循环结束本次循环,程序流程转去执行对条件的判断,如果这时循环条件为真,则开始下一次循环,否则终止循环语句1语句2第20页/共45页C+程序设计214.5 转向语句continue 语句continue 语句 和 break 语句的区别continue 语句只结束本次循环而不是终止整个循环的执行break 语句结束本次循环,不再进行条件判断for(int n=100;n=200;n+)if(n%3=0)continue;coutnendl;while(表达式1)if(表达式2)continue;第21页/共45页C+程序设
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 循环 语句 学习
限制150内