《选择控制结构 》PPT课件.ppt

上传人:牧羊曲112 文档编号:5677362 上传时间:2023-08-08 格式:PPT 页数:51 大小:809KB
返回 下载 相关 举报
《选择控制结构 》PPT课件.ppt_第1页
第1页 / 共51页
《选择控制结构 》PPT课件.ppt_第2页
第2页 / 共51页
《选择控制结构 》PPT课件.ppt_第3页
第3页 / 共51页
《选择控制结构 》PPT课件.ppt_第4页
第4页 / 共51页
《选择控制结构 》PPT课件.ppt_第5页
第5页 / 共51页
点击查看更多>>
资源描述

《《选择控制结构 》PPT课件.ppt》由会员分享,可在线阅读,更多相关《《选择控制结构 》PPT课件.ppt(51页珍藏版)》请在三一办公上搜索。

1、第5章 选择控制结构,C语言程序设计,本章学习内容,算法的描述方法 用于单分支控制的if语句 用于双分支控制的if-else语句 用于多路选择的switch语句 break语句在switch语句中的作用 关系运算符 条件运算符 逻辑运算符 程序测试,生活中的问题求解:Problem:烤蛋糕(Baking a Cake)How to solve:Start 将烤箱预热 准备一个盘子 在盘子上抹上一些黄油 将面粉、鸡蛋、糖和香精混合在一起搅拌均匀 将搅拌好的面粉团放在盘子上 将盘子放到烤箱内 End,5.1生活中与计算机中的问题求解(Problem Solving Process),分治策略(Di

2、vide and Conquer Strategy),Problem:准备早餐(Prepare a Breakfast),1.Start2.准备早餐3.End,1.Start2.准备早餐 2.1 准备一个金枪鱼三明治 2.2 准备一些薯条 2.3 冲一杯咖啡3.End,分治策略(Divide and Conquer Strategy),1.Start2.准备早餐 2.1 准备一个金枪鱼三明治 2.1.1 拿来两片面包 2.1.2 准备一些金枪鱼酱 2.2 准备一些薯片 2.3 冲一杯咖啡3.End,分治策略(Divide and Conquer Strategy),1.Start2.准备早餐

3、2.1 准备一个金枪鱼三明治 2.1.1 拿来两片面包 2.1.2 准备一些金枪鱼酱 2.2 准备一些薯片 2.2.1 将土豆切成片 2.2.2 油炸这些土豆片 2.3 冲一杯咖啡3.End,分治策略(Divide and Conquer Strategy),分治策略(Divide and Conquer Strategy),1.Start2.准备早餐 2.1 准备一个金枪鱼三明治 2.1.1 拿来两片面包 2.1.2 准备一些金枪鱼酱 2.2 准备一些薯片 2.2.1 将土豆切成片 2.2.2 油炸这些土豆片 2.3 冲一杯咖啡 2.3.1 烧些开水放入杯中 2.3.2 在水杯中加入一些咖啡

4、和糖3.End,5.2算法的概念及其描述方法,面向对象程序=对象+消息面向过程的程序=数据结构+算法计算机中的算法(Algorithm)为解决一个具体问题而采取的、确定的、有限的操作步骤,仅指计算机能执行的算法A specific and step-by-step set of instructions for carrying out a procedure or solving a problem,usually with the requirement that the procedure terminate at some point,5.2算法的概念及其描述方法,算法的特性有穷性在合

5、理的时间内完成确定性,无歧义 如果x0,则输出Yes;如果x0,则输出No有效性 能有效执行负数开平方没有输入或有多个输入 有一个或多个输出,5.2算法的概念及其描述方法,算法的描述方法自然语言描述传统流程图(Flowchart)在1966年,Bohra 与 Jacopini 提出N-S结构化流程图1973年,美国学者I.Nassi 和 B.Shneiderman 提出伪码(Pseudocode)表示,流程图(Flowchart),Flowchart represents algorithm graphically.,计算机中的问题求解过程,Example:买苹果,计算价钱Calculate

6、and display the price of a number of apples if the quantity in kg and price per kg are given.,quantity pricePerkg,price,price=quantity*pricePerkg,Input,Process,Output,First identify the input and output of the problem.,顺序结构(Sequence Structure),给变量赋值赋值表达式语句 赋值表达式;price=quantity*pricePerkg;输入输出数据标准库函数

7、调用语句 scanf(%d,【例5.1】计算两整数的最大值,num1 num2,max,?,Input,Process,Output,Double Selection,选择结构(分支结构)(Selection Structure),5.3关系运算符与关系表达式,5.4用于单分支控制的条件语句(Single Selection),step a,condition,step m,step n,step b,true,false,Pseudocode Structurestep aif startstep mstep nend_ifstep b,if Statement,The structure

8、is similar to single selection(flowchart),Syntax:if(expression)statement;orif(expression)statement1;statement2;,if Statement,The structure is similar to single selection(flowchart),Syntax:if(expression)statement;orif(expression)statement1;statement2;,#include main()int a,b,max;printf(Input a,b:);sca

9、nf(%d,%d,Input a,b:_,Input a,b:20 15_,Input a,b:20 15max=20_,【例5.1】计算两整数的最大值,Pseudocode StructureStep aif startStep mStep nend_ifelse startStep xStep yend_elseStep z,Step a,condition,Step m,Step n,Step z,true,false,Step x,Step y,5.5用于双分支控制的条件语句(Double Selection),if-else Statement,The structure is si

10、milar to double selection(flowchart),Syntax:if(expression)statement1;else statement2;,or if(expression)statement1;statement3;else statement2;statement4;,Flowchart:Calculate the Maximum,Input a and b,Output max,a b?,max b,max a,Start,End,【例5.2】计算两整数的最大值,scanf(%d,%d,if(a b)max=a;else max=b;,Turn Flowc

11、hart to C Program,【例5.2】计算两整数的最大值,printf(max=%dn,max);,#include main()int a,b,max;printf(Input a,b:);scanf(%d,%d,if(a b)max=a;if(a=b)max=b;,【例5.2】计算两整数的最大值,#include main()int a,b,max;printf(Input a,b:);scanf(%d,%d,max=a b?a:b;,5.6条件运算符和条件表达式,【例5.3】,5.7用于多分支控制的条件语句(Multiple Selection),Multi-way ifSte

12、p aif(expression1)Step m if(expression2)Step n Step z,Step a,expression1,Step m,Step n,Step z,true,false,expression2,true,false,5.7用于多分支控制的条件语句(Multiple Selection),Cascaded ifStep aif(expression1)Step m else if(expression2)Step n else Step x Step z,Step a,expression1,Step m,Step n,Step z,true,false,

13、expression2,true,false,Step x,5.8用于多路选择的switch语句,The structure is similar to multiple selection(flowchart),switch(expression)case value1:statement1;break;case value2:statement2;break;default:statementX;break;,Important Rule!,switch(expression)case value1:statement1;break;case value2:statement2;break

14、;default:statementX;break;,5.8用于多路选择的switch语句,注意!,Example:switch(month)case 1:printf(Januaryn);break;case 2:printf(Februaryn);break;case 3:printf(Marchn);break;default:printf(Othersn);break;printf(End);,January_,JanuaryEnd _,5.8用于多路选择的switch语句,Example:switch(month)case 1:printf(Januaryn);break;case

15、2:printf(Februaryn);break;case 3:printf(Marchn);break;default:printf(Othersn);break;printf(End);,March_,MarchEnd _,5.8用于多路选择的switch语句,Example:switch(month)case 1:printf(Januaryn);break;case 2:printf(Februaryn);break;case 3:printf(Marchn);break;default:printf(Othersn);break;printf(End);,5.8用于多路选择的swi

16、tch语句,Example:switch(month)case 1:printf(Januaryn);break;case 2:printf(Februaryn);case 3:printf(Marchn);break;default:printf(Othersn);break;printf(End);,5.8用于多路选择的switch语句,Example:switch(month)case 1:printf(Januaryn);break;case 2:printf(Februaryn);case 3:printf(Marchn);break;default:printf(Othersn);

17、break;printf(End);,February_,March _,End _,5.8用于多路选择的switch语句,Example:switch(month)case 1:printf(Januaryn);break;case 2:printf(Februaryn);case 3:printf(Marchn);break;default:printf(Othersn);break;printf(End);,And if month=1?,And if month=34?,5.8用于多路选择的switch语句,【例5.5】计算器程序,编程设计一个简单的计算器程序,要求用户从键盘输入如下形

18、式的表达式:操作数1 运算符op 操作数2 然后,计算并输出表达式的值 指定的运算符为 加(+)减(-)乘(*)除(/),main()int data1,data2;/*定义两个操作符*/char op;/*定义运算符*/printf(Please enter the expression:);scanf(%d%c%d,【例5.5】,思考题,语句if(0=data2)的必要性避免“除零错误”1998年11月,科学美国人杂志描述了美国导弹巡洋舰约克敦号上的一起事故,除零错导致军舰推进系统的关闭为什么不用if(data2=0)?如果要求输入的算术表达式中的操作数和运算符之间可以加入任意多个空格符,

19、那么程序如何修改?,main()int data1,data2;char op;printf(Please enter the expression:);scanf(%d%c%d,【例5.5】,思考题,如果要求对浮点数进行运算,那么程序如何修改?修改例5.5程序,使其能进行浮点数的算术运算,同时允许使用字符*、x与X作为乘号,并且允许输入的算术表达式中的操作数和运算符之间加入任意多个空格符。,main()float data1,data2;char op;printf(Please enter the expression:);scanf(%f%c%f,【例5.6】,Symbol Descri

20、ption&与(AND)当且仅当两者都为真,则结果为真|或(OR)只要两者中有一个为真,结果就为真!非(NOT),5.9逻辑运算符和逻辑表达式(Logic Operators and Logic Expression),!&|,高,低,ch是大写英文字母(ch=A)&(ch=Z)判断某一年year是否是闰年的条件是满足下列二者之一能被4整除,但不能被100整除;能被400整除;year%4=0&year%100!=0|year%400=0 优先级:%=(!=)&|(year%4=0)&(year%100!=0)|(year%400=0),5.9逻辑运算符和逻辑表达式(Logic Operato

21、rs and Logic Expression),Example:(a=1)&(b+=5),(0=1)&(b+=5),0&(b+=5),0,复合表达式(Compound Expression)的值,尽量使用最少的操作数来确定表达式的值,这就意味着表达式中的某些操作数可能不会被计算,5.10本章扩充内容,测试的主要方式给定特定的输入,运行被测软件检查软件的输出是否与预期结果一致测试用例的选取方法尽量覆盖所有分支,减少重复覆盖测试的目的通过运行测试用例找出软件中的Bug成功的测试在于发现迄今为止尚未发现的Bug测试人员的主要任务是站在使用者的角度,通过不断使用和攻击,尽可能多地找出Bug测试的过程

22、就像黑客的攻击过程,专门找软件漏洞,5.10本章扩充内容,采用测试用例,通过运行程序查找程序错误的方法实质是一种抽样检查,彻底的测试是不可能的彻底的测试不现实,要考虑时间、费用等限制,不允许无休止的测试测试只能证明程序有错,不能证明程序无错 测试能提高软件质量,但提高软件质量不能依赖于测试,软件测试方法的分类,白盒测试(结构测试)在完全了解程序的结构和处理过程的情况下,按照程序内部的逻辑测试程序,检验程序中的每条逻辑路径是否都能按预定要求正确工作主要用于测试的早期黑盒测试(功能测试)把系统看成一个黑盒子,不考虑程序内部的逻辑结构和处理过程,只根据需求规格说明书的要求,设计测试用例,检查程序的功能是否符合它的功能说明主要用于测试的后期,作业,习题5.1 5.9实验题本章实验题,Questions and answers,

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 生活休闲 > 在线阅读


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号