c语言 选择控制结构课件

上传人:我*** 文档编号:144916150 上传时间:2020-09-14 格式:PPT 页数:59 大小:864KB
返回 下载 相关 举报
c语言 选择控制结构课件_第1页
第1页 / 共59页
c语言 选择控制结构课件_第2页
第2页 / 共59页
c语言 选择控制结构课件_第3页
第3页 / 共59页
c语言 选择控制结构课件_第4页
第4页 / 共59页
c语言 选择控制结构课件_第5页
第5页 / 共59页
点击查看更多>>
资源描述

《c语言 选择控制结构课件》由会员分享,可在线阅读,更多相关《c语言 选择控制结构课件(59页珍藏版)》请在金锄头文库上搜索。

1、第5章 选择控制结构,本章学习内容, 算法的描述方法 用于单分支控制的if语句 用于双分支控制的if-else语句 用于多路选择的switch语句 break语句在switch语句中的作用 关系运算符 条件运算符 逻辑运算符 程序测试,5.1算法的概念及其描述方法,一个程序应包括两个方面的内容: 对数据的描述:数据结构(data structure) 对操作的描述:算法(algorithm) 著名计算机科学家沃思提出一个公式: 数据结构 + 算法 = 程序 计算机中的算法( Algorithm ) 为解决一个具体问题而采取的确定的有限的操作步骤,仅指计算机能执行的算法,5.1算法的概念及其描述

2、方法,算法的特性 有穷性 在合理的时间内完成 确定性,无歧义 如果x0,则输出Yes;如果x0,则输出No 有效性 能有效执行 负数开平方 没有输入或有多个输入 有一个或多个输出,5.1算法的概念及其描述方法,算法的描述方法 自然语言描述 传统流程图(Flowchart) 在1966年,Bohra 与 Jacopini 提出 N-S结构化流程图 1973年,美国学者I.Nassi 和 B.Shneiderman 提出 伪码(Pseudocode)表示,流程图(Flowchart),Flowchart represents algorithm graphically.,N-S流程图,去掉流程线,

3、使图紧凑易画,计算机中的问题求解过程,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 problem.,顺序结构( Sequence Structure),给变量赋

4、值 赋值表达式语句 赋值表达式 ; price = quantity*pricePerkg; 输入输出数据 标准库函数调用语句 scanf(%d, ,【例5.1】计算两整数的最大值,num1 num2,max,?,Input,Process,Output,选择结构(分支结构) (Selection Structure),Multiple Selection,5.2关系运算符与关系表达式,5.3用于单分支控制的条件语句(Single Selection),step a,condition,step m,step n,step b,true,false,Pseudocode Structure s

5、tep 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),Syntax: if (expression) statement; or

6、if (expression) statement1; statement2; ,#include void 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 y end_else Step z,Step a,co

7、ndition,Step m,Step n,Step z,true,false,Step x,Step y,5.4用于双分支控制的条件语句( 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 statement2; statement4; ,Flowchar

8、t: 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 void main() int a, b, max; printf(Input a, b:); scanf(%d,%d, ,if (a b) max = a; if (

9、a = b) max = b;,【例5.2】计算两整数的最大值,#include void main() int a, b, max; printf(Input a, b:); scanf(%d,%d, ,max = a b ? a : b;,5.5条件运算符与条件表达式,【例5.3】,5.6用于多分支控制的条件语句(Multiple Selection),Multi-way if Step a if (expression1) Step m if (expression2) Step n Step z,Step a,expression1,Step m,Step n,Step z,true,

10、false,expression2,true,false,5.6用于多分支控制的条件语句(Multiple Selection),Cascaded if Step a if (expression1) Step m else if (expression2) Step n else Step x Step z,Step a,expression1,Step m,Step n,Step z,true,false,expression2,true,false,Step x,if语句的嵌套 在if语句中又包含一个或多个if语句称为if语句的嵌套。 形式: if() if() 语句1 else 语句2

11、 else if() 语句3 else 语句4,内嵌if,匹配规则 else总是与它上面的,最近的,同一复合语句中的,未配对的if语句配对。,例: if() if() 语句1 else if() 语句2 else 语句3,例: if() if() 语句1 else if() 语句2 else 语句3,当if和else数目不同时,可以加花括号来确定配对关系。,-1 (x0) 算法1: 算法2: 输入x 输入x 若x0,则y=1 若x=0,则y=0 否则: 输出y 若x0,则y=1 输出y,#include void main() int x,y; scanf(“%d”, ,上例中的程序段有四个,

12、请判断哪个是正确的? 程序1: 程序2: if (x=0) y=-1; if (x0) y=1; else else y=0; if (x=0) y=0; else y=-1; else y=1; 程序3: 程序4: y=-1; y=0; if (x!=0) if (x=0) if (x0) y=1; if (x0) y=1; else y=0; else y=-1;,正 确,正 确,错 误,错 误,想一个1100之间的数,猜对: right 猜错:wrong并提示大小,【例5.4】 猜数字游戏,#include #include main() int magic; /*计算机想的数*/ in

13、t guess; /*人猜的数*/ magic = rand()%100 + 1; /*“想”一个1,100之间的数magic*/ printf(Please guess a magic number:); scanf(%d, ,5.7用于多路选择的switch语句,The structure is similar to multiple selection (flowchart),switch (expression) case value1 : statement1; break; case value2 : statement2; break; default : statementX;

14、 break; ,Important Rule !,switch (expression) case value1 : statement1; break; case value2 : statement2; break; default : statementX; break; ,5.7用于多路选择的switch语句,注意!,Example: switch (month) case 1: printf(“Januaryn”); break; case 2: printf(“Februaryn”); break; case 3: printf(“Marchn”); break; default

15、: printf(“Othersn”); break; printf(“End”);,January _,January End _,5.7用于多路选择的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”);,March _,March End _,5.7用于多路选择的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.7用于多路选择的switch语句,Exa

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 办公文档 > PPT模板库 > PPT素材/模板

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号