第14次(循环结构2)

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
IT Education & Training
语句 while(表达式) { 语句 }
Date: 2012年10月18日星期四
Neusoft Institute of Information
循环语句的选择
三种结构的转化关系举例:
for(i=1;i<5;i++) printf(“i=%d\n”,i);
IT Education & Training
Date: 2012年10月18日星期四
Neusoft Institute of Information
do-while结构实现
#include <stdio.h> //输入输出库函数 void main() //主函数 { int height,i; //用户身高 float sweight,weight; //标准体重,用户体重 do { printf("\n请输入您的身高,以cm为单位:"); scanf("%d",&height); printf("\n请输入您的体重,以kg为单位:"); scanf("%f",&weight); sweight=(height-100)*0.9; if((weight>(sweight*1.1))||(weight<(sweight*0.9))) printf("\n您的体重超过正常范围,请注意改善!"); else printf("\n恭喜您!您的体重很完美!"); }while(height!=0); }
IT Education & Training
Date: 2012年10月18日星期四
Neusoft Institute of Information
循环语句的选择
三种结构的转化关系:
for(表达式1;表达式2;表达式3) 语句
表达式1; While(表达式2) { 语句 表达式3; }
do { 语句 }while(表达式);
Neusoft Institute of Information
模仿练习
• • 循环结构——while循环结构 【练习1】循环输入某学生的若干门课程成绩, 并计算学生的总分,当学生输入-1时表示成绩 输入完毕 。 【练习2】用while结构计算1+2+3……+100 的值。

IT Education & Training
作业——小组必做
• 1、学以致用:寻找你身边的一个实际问题,编写解决问题的程序, 用到循环结构。例如使用循环结构实现一个常用字符的ASCII码转 换程序,即由用户输入一个字符,程序输出其对应的ASCII码值, 当用户按“ESC”键程序结束。 • 2、计票器。假设一次选举中有3名候选人,20人投票(1人1票, 且只能选择1名候选人)请用循环结构实现一个计票器,统计每名 候选人的得票数。在程序设计中,可以考虑给候选人编号为1、2、 3,投票时输入相应数字则表示投了该候选人1票,如果输入的数 字不为1/2/3,则表示是无效票,不予统计。
IT Education & Training
Date: 2012年10月18日星期四
Neusoft Institute of Information
开始
输入身高height,体重weight
Y
Height==0
N
sweight=(height-100)*0.9
(weight>sweight*1.1)||(weight<sweight*0.9)
IT Education & Training
Date: 2012年10月18日星期四
ห้องสมุดไป่ตู้
Neusoft Institute of Information
结论2——while、for、do-while的 一般格式有什么区别
• while循环和for循环都是入口条件循环,执行循
环之前先检查判断条件。
• do-while语句的流程是先执行,后判断。
Date: 2012年10月18日星期四
Neusoft Institute of Information
第三章
结 构 化 程 序 设 计
任课教师:黄伟
办公室:C7 EMAIL:huang.w@neusoft.com 课件下载网址:ftp://computer.dept.ccniit.com
IT Education & Training IT Education & Training
}
Date: 2012年10月18日星期四
Neusoft Institute of Information
小组讨论并总结

• •
一般情况,如何选择采用while还是for或do-while 结构 while、for、do-while的一般格式有什么区别 while和do-while执行过程有什么区别
• do-while循环是退出条件循环,判断条件在
执行循环之后进行检查,这样循环体中的语 句至少被执行一次。
IT Education & Training
Date: 2012年10月18日星期四
Neusoft Institute of Information
学以致用
• 寻找你身边的一个实际问题,编写解决问题的程序,用到 循环结构。例如:假设你在某公司工作,老板要你打印一 张表,买主可以用它来购买特定部件的费用。单个部件的 价格从1元到9元不等。因此,你要打印一张像下面这样的 数字表: • 部件费用明细表 • 1 2 3 4 5 6 7 8 9 • 2007年4月7日
IT Education & Training
Date: 2012年10月18日星期四
Neusoft Institute of Information
作业——每人必做
• 1、编写程序实现功能:求出1+2+3+4+ … +100
的值,并显示结果。 • 2、编写程序实现功能:求出n!= 1×2×3×…×n的值,并显示结果。 • 3、写一个小型计算器。
附:小型计算器程序编写要求:
• • • • • 1.有简单运算选择界面 2.采用循环实现菜单显示 3.采用switch结构实现菜单的选择 4.运算对象为两个操作数,从键盘输入 5.运算结果输出
IT Education & Training
Date: 2012年10月18日星期四
Neusoft Institute of Information
IT Education & Training
Date: 2012年10月18日星期四
Neusoft Institute of Information
附:小型计算器简单介绍
IT Education & Training
Date: 2012年10月18日星期四
Neusoft Institute of Information
IT Education & Training
Date: 2012年10月18日星期四
Neusoft Institute of Information
结论1——一般情况,如何选择采用 while还是for或do-while结构
• 当确定需要循环时,首先要确定需要入口条件循环 还是退出条件循环。 • 假定需要入口条件循环,在循环涉及到初始化和更 新变量时使用for循环较为适当,而在其他条件下使 用while循环更好些。对涉及到索引计数的循环用 for循环是更好的选择。 • 对于那些至少需要执行一次循环的情况,应该把dowhile循环做为首选。
do-while 语句应注意
与while 语句的区别: • 1.进入循环体后,先执行一次,再判断至少执行一次循 环体。 • 2.判断条件次数和循环体执行次数相同。 • 3.while ( )后的';'(分号)不能少 • 4.C语言中do-while 结构中条件表达式为真时执行循环 体,否则退出,与其他语言相区别。
IT Education & Training
Date: 2012年10月18日星期四
Neusoft Institute of Information
问题分析
• 算法分析 输入:多个用户的身高、体重 判断:身高为0,退出程序 处理: (1)根据公式标准体重=(身高-100)×0.9进行计算 (2)判断多个用户的体重是否在标准体重的正负10%内 输出:多个用户的体重是否正常 控制结构之三:while循环结构 • 算法描述——流程图
Date: 2012年10月18日星期四
Neusoft Institute of Information
while语句注意事项
• 1.其特点为"先判断,再执行",循环体可能一次也不执行。 • 2.当循环体为多个语句时,必须用{ }括起来 • 3.循环最终能够结束,则条件判断的表达式值能够满足退 出条件,执行一次循环体后,表达式或循环体的某个值必 定有变化。 • 4.条件表达式的计算比循环体的执行多一次, • (不满足循环条件的那一次)
i=1; while(i<5) { printf(“i=%d\n”,i); i++; } i=1; while(i<5) { printf(“i=%d\n”,i);
i++;
i=1; do { printf(“i=%d\n”,i); i++; }while(i<5);
IT Education & Training
Neusoft Institute of Information
模仿练习


【练习3】用do-while结构计算1+2+ 3……+100的值。 【练习4】计算正整数num的各位数字之 积。
IT Education & Training
Date: 2012年10月18日星期四
Neusoft Institute of Information
Y 输出体重超标
N
输出体重正常
输入身高height,体重weight
结束
IT Education & Training
Date: 2012年10月18日星期四
Neusoft Institute of Information
while结构实现
#include <stdio.h> //输入输出库函数 void main() //主函数 { int height,i; //用户身高 float sweight,weight; //标准体重,用户体重 while(height!=0) { printf("\n请输入您的体重,以kg为单位:"); scanf("%f",&weight); sweight=(height-100)*0.9; if((weight>(sweight*1.1))||(weight<(sweight*0.9))) printf("\n您的体重超过正常范围,请注意改善!"); else printf("\n恭喜您!您的体重很完美!"); printf("\n请输入您的身高,以cm为单位:"); scanf("%d",&height); } }
IT Education & Training
Date: 2012年10月18日星期四
Neusoft Institute of Information
选择三种循环的一般思路
• • 如果循环次数已知,用for 如果循环次数未知,用while


如果循环体至少要执行一次,用do-while
只是思路,不是定律
IT Education & Training
Date: 2012年10月18日星期四
Neusoft Institute of Information
while语句结构
while(条件表达式) { 循环体语句; ……; }
IT Education & Training
Date: 2012年10月18日星期四
IT Education & Training
Date: 2012年10月18日星期四
Neusoft Institute of Information
结论3——while和do-while执行过程有什 么区别
• while循环是入口条件循环,在每次执行之 前先检查判断条件,这样循环中的语句就有 可能一次也不执行。
IT Education & Training
Date: 2012年10月18日星期四
Neusoft Institute of Information
do-while语句结构
do {
循环体语句; ……; } while(条件表达式);
IT Education & Training
Date: 2012年10月18日星期四
Date: 2012年10月18日星期四
Neusoft Institute of Information
问题:多人的健康状况
当你想为更多的人进行测试,且测试人数无法准 确统计时。你越来越需要建立一个可对任意人数 健康进行测试,且开始和终止都可由你控制的软 件。 标准体重=(身高-100)×0.9(单位:千克) 其数值的正负10%为健康。
相关文档
最新文档