第5章选择控制结构.ppt
《第5章选择控制结构.ppt》由会员分享,可在线阅读,更多相关《第5章选择控制结构.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),分
2、治策略 (“Divide and Conquer“ Strategy ),Problem: 准备早餐( Prepare a Breakfast),1. Start 2. 准备早餐 3. End,1. Start 2. 准备早餐 2.1 准备一个金枪鱼三明治 2.2 准备一些薯条 2.3 冲一杯咖啡 3. End,分治策略 (“Divide and Conquer“ Strategy ),1. Start 2.准备早餐 2.1 准备一个金枪鱼三明治 2.1.1 拿来两片面包 2.1.2 准备一些金枪鱼酱 2.2 准备一些薯片 2.3 冲一杯咖啡 3. End,分治策略 (“Divide and
3、Conquer“ Strategy ),1. Start 2.准备早餐 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. Start 2.准备早餐 2.1 准备一个金枪鱼三明治 2.1.1 拿来两片面包 2.1.2 准备一些金枪鱼酱 2.2 准备一些薯片 2.2.1 将土豆切成片 2.2.2
4、油炸这些土豆片 2.3 冲一杯咖啡 2.3.1 烧些开水放入杯中 2.3.2 在水杯中加入一些咖啡和糖 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 th
5、at the procedure terminate at some point,5.2算法的概念及其描述方法,算法的特性 有穷性 在合理的时间内完成 确定性,无歧义 如果x0,则输出Yes;如果x0,则输出No 有效性 能有效执行 负数开平方 没有输入或有多个输入 有一个或多个输出,5.2算法的概念及其描述方法,算法的描述方法 自然语言描述 传统流程图(Flowchart) 在1966年,Bohra 与 Jacopini 提出 N-S结构化流程图 1973年,美国学者I.Nassi 和 B.Shneiderman 提出 伪码(Pseudocode)表示,流程图(Flowchart),Flow
6、chart represents algorithm graphically.,计算机中的问题求解过程,Example :买苹果,计算价钱 Calculate 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 proble
7、m.,顺序结构( Sequence Structure),给变量赋值 赋值表达式语句 赋值表达式 ; price = quantity*pricePerkg; 输入输出数据 标准库函数调用语句 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
8、,true,false,Pseudocode Structure step a if start step m step n end_if step b,if Statement,The structure is similar to single selection (flowchart),Syntax: if (expression) statement; or if (expression) statement1; statement2; ,if Statement,The structure is similar to single selection (flowchart),Synt
9、ax: if (expression) statement; or if (expression) statement1; statement2; ,#include main() int a, b, max; printf(“Input a,b:“); scanf(“%d,%d“, ,Input a,b: _,Input a,b: 20 15 _,Input a,b: 20 15 max = 20 _,【例5.1】计算两整数的最大值,Pseudocode Structure Step a if start Step m Step n end_if else start Step x Step
10、 y end_else Step 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 similar to double selection (flowchart),Syntax: if (expression) statement1; else statement2;,or if (expression) statement1; statement3; else state
11、ment2; 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 Flowchart to C Program,【例5.2】计算两整数的最大值,printf(“max = %dn“, max);,#include main() int a, b, max; printf(“Input a, b:“); scanf(“
12、%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 if Step a if (expression1) Step m if (expression2) Step n Step z,Step a,express
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 选择 控制 结构
链接地址:https://www.31doc.com/p-2606911.html