第3章 程设计初步2.ppt

  1. 1、本文档共65页,可阅读全部内容。
  2. 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
  3. 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  4. 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
3.11 循环结构和循环语句 S=0 ? I=1?? S=S+I I++?? S=S+I I++ S=S+I I=++ 3.11 循环结构和循环语句 循环的基本结构(四个部分) 可实现循环的语句 while 语句 do ~ while 语句 for 语句 3.11.1 用while语句构成循环 while语句 程序: #include iostream using namespace std; void main() {float x,max=0; int i=0; while (i10) {cinx; if (xmax) max=x; i++; } coutmax=maxendl; } 程序: #include iostream using namespace std; void main() {float x,max; int i=1; cin x; max=x; i++; while (i=10) {cinx; if (xmax) max=x; i++; } coutmax=maxendl; } 从键盘输入10个数,找出其中的最大和最小的数.并求输入的10个数的平均值 提示: 最大和最小的数保存在哪? 如何设置最大数和最小数的初始值? #include iostream using namespace std; void main() {float x,max,min,s; int i=1; cin x; max=x; min=x;s=x; i++; while (i=10) {cinx; if (xmax) max=x; if (xmin) min=x; s=s+x; i++; } coutmax=maxendl; coutmin=minendl; cout10个数的平均值=s/10endl; } 例7: 寻找1000以内能被3、5、7同时整除的正整数 例8. 文本加密 例10: 对比下列程序: #include iostream using namespace std; void main() {int i,sum=0,n; cout求n个自然数的和;; cout\n输入n的值:; cinn; i=1; while (i=n) { sum=sum+i; i++; } coutn个自然数的和; cout=sumendl; } #include iostream using namespace std; void main() {int i,sum=0,n; cout求n个自然数的和;; cout\n输入n的值:; cinn; i=1; do { sum=sum+i; i++; }while (i=n); coutn个自然数的和; cout=sumendl; } 3.11.2 用do-while语句构成循环 例16:鸡兔共有30只,脚共有90只,问鸡兔各有多少? #include iostream using namespace std; void main( ) {int x; //x代表鸡,则兔为30-x只 for(x=0; x30; x++) if(2*x+4*(30-x)==90) { cout鸡:xendl; cout兔:30-xendl; } } 例18: 输出九九乘法表 #include iostream.h int main() { int i,j; for(i=1; i=9; i++) { for(j=1; j=9; j++) cout i * j =i*j \t; cout endl; } return 0; } 例18: 输出直角三角形九九乘法表 1*1=1 1*2=2 2*2=4 1*3=3 2*3=6 3*3=9 1*4=4 2*4=8 3*4=12 4*4=16 1*5=5 2*5=10 3*5=15 4*5=20 5*5=25 1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36 1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49 1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*

文档评论(0)

baa89089 + 关注
实名认证
内容提供者

该用户很懒,什么也没介绍

1亿VIP精品文档

相关文档