版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、第四章,选择结构程序设计,教学目的: 掌握选择结构的形式,掌握典型算法。 教学要求: 掌握关系表达式和逻辑表达式以及在选择结构中的使用。学会把现实问题通过表达式的形式表示。,关系运算符和关系表达式 Relational Operators and Expressions 逻辑运算符和逻辑表达式 Logical Operators and Expressions 条件表达式 Conditional Expressions if 语句 switch 语句,关系运算符和以关系运算符组成的关系表达式 关系运算符 种类:= = != 结合方向:自左向右 优先级别:,关系表达式:以关系符连起来的式子 关系
2、表达式的值:是逻辑值“真”或“假”, 用1和0表示; 1表示逻辑真,0表示逻辑假(非零为真),例 int a=3,b=2,c=1,d,f; ab (ab)=c b+cb f=abc,/表达式值1,/表达式值1,/表达式值0,/d=1,/f=0,因为 a=x值为1,所以 x=b的值为0,关系运算表达式使用中要注意各步骤的逻辑值:,例 若a=0; x=0.3;b=0.5; 则 a78在C中是允许的, 值为,0,例 int i=1, j=7,a; a=i+(j%4!=0); 则a=,2,例 a0 结果为 A100 结果为,1,0,因为上式运算步骤是左结合 ( (52) 7 ) 8 所以 5278的值
3、为0,关系运算注意:,2. 注意区分 = 与 = main() int a=0,b=1; printf(%dn,a=b); printf(%dn,a=b); getch(); ,1. 避免对实数作相等或不等的判断 如 1.0/0.00001*0.00001=1.0;结果为 一般而言,对于实型量 a,b作相等或不等的判断应当用: fabs(a-b)1e-6,0,0 1,关系运算注意:,2. 注意区分 = 与 = main() int a=0,b=1; printf(%dn,a=b); printf(%dn,a=b); getch(); ,1. 避免对实数作相等或不等的判断 如 1.0/0.000
4、01*0.00001=1.0;结果为 一般而言,对于实型量 a,b作相等或不等的判断应当用: fabs(a-b)1e-6,0,0 1,关系运算注意:,2. 注意区分 = 与 = main() int a=0,b=1; printf(%dn,a=b); printf(%dn,a=b); getch(); ,1. 避免对实数作相等或不等的判断 如 1.0/0.00001*0.00001=1.0;结果为 一般而言,对于实型量 a,b作相等或不等的判断应当用: fabs(a-b)1e-6,0,0 1,补充知识:,例如:对于 float a,b; 不要用 if(a=b) 进行比较, 而要用 if(fab
5、s(a-b)=epsilon*fabs(a) 进行比较, (其中要确保a不会为0)epsilon是一个控制接近度的值。 用绝对阈值的方法是许多书上推荐的: if(fabs(a-b)0,它们的逻辑结果都是true。但从计算机而言,当x与y的绝对值相差很大时,这个值就有可能是false,#include main() double x=1e20,y=10; printf(%d, x+yx); getch(); /* 输出是0 */,逻辑运算符和以逻辑运算符组成的逻辑表达式 逻辑运算符 种类: b=5; !a a x0?a+b:a-b a+|b|,例 (a=b)?Y:N a等于b为Y,否则为N (x
6、%2=1)?1:0 10进制整数转为2进制 (x=0)?x:-x x的绝对值 (c=a else max=y;,形式二: 格式:if (expression) statement1 else statement2 执行过程:,例:if (xy) printf(“%d”,x);,形式三: 格式:,if ( expr1 ) statement1 else if (expr2 ) statement2 else if (expr3 ) statement3 . else statementn ,执行过程:,if(salary10000) index=0.4; else if(salary8000)
7、index=0.3; else if (salary4000) index=0.2; else if (salary2000) index=0.1; else index=0;,如:if(a=b,statement可以是一条语句或复合语句 if(x) if(x!=0) 例: if(3) if(3!=0) 恒成立 if(0) if(0!=0) 恒不成立 if(!x) if(x= =0) 例: if(!3) if(3= =0) 恒不成立 if(!0) if(0= =0)恒成立,说明: if后面的表达式类型任意 if 后面的表达式的值按逻辑值处理(0或非0)。,例 下面程序正确吗? #include
8、 main() int x,y; printf(x=); scanf(%d, ,illegal else without matching if,main() int x,y; printf(输入一个整数:); scanf(%d, ,例:求一个数的绝对值,运行:输入一个整数:-12 整数-12的绝对值是12。,main() int a,b; printf(a=); scanf(%d, ,例 输入两个数并判断两数相等否,运行:a=12 b=12 a=b,运行:a=12 b=9 ab,例如:已知三角形的三条边求面积,程序,#include #include main ( ) float a,b,c
9、,s,area; printf(Side a=); scanf(%f, ,运行时输入: 3 4 5 输出:area=6.00 运行时输入: 1 3 3 输出:Sorry,reentry please,if语句嵌套 一般形式,例: 输入两数并判断其大小关系,main() int x,y; printf(x=); scanf(%d, ,运行:x=12 y=15 xy x=6 y=6 x y,if else 配对原则: 缺省 时,else总是和它上面离它最近的未配对的if 配对,if (a=b) if(b=c) printf(“a=b=c”); else printf(“a!=b”);,修改:if
10、(a=b) if(b=c) printf(“a=b=c”); else printf(“a!=b”);,实现if else 正确配对方法:加 ,题目: 从a是否等于b开始,判断a、b、c三值是否相等,A code fragment is to be used to separate integer values into five ranges. For example: 0 . 29, 30 . 49, 50 . 59, 60 . 69, 70 . 100. What is the fewest number of if . else selection statements needed
11、to do this? Note, all data presented will already have been validated to ensure they are in the range 0 .100.,break语句(中断语句),语句形式:break; 作用:中止switch结构或循环结构的执行, 转去执行相应结构的下一条语句。 说明:break语句只能用于switch结构或者循 环结构之中。,switch statements,使用if语句嵌套层次太多,容易发生错误,因此在多分支结构中,通常使用switch语句 。 Suppose we had an integer var
12、iable j, and wanted to execute different statements depending on the value of j. Lets suppose we wanted to print j is one if j had value 1, j is two if j has the value 2, otherwise we want to print j is neither 1 nor 2. We could write the following code: if (j = 1) printf(“j is one”); else if (j = 2
13、) printf(“ j is two”); else printf(“j is neither 1 nor 2”); OR we could use a switch statement.,switch statement example,switch (j) case 1: printf(“ j is one”); break; case 2: printf(“ j is two”); break; default: printf(“j is neither 1 nor 2”); When used in a switch statement , the break statement c
14、auses control to pass to the statement following the closing brace.,switch statement switch语句的一般形式,switch (expr) case constant1: statement1; break; case constant2: statement2; break; : :,default: default statement; expr must be of type int. Each case label must be followed by a constant. First expr
15、is evaluated, control then passes to the case label having matching constant value, otherwise, control passes to the default label. You usually want to use break statements inside switches. Remember: more all statements following the matching case label will be executed. The default statement need n
16、ot be the last in the switch.,当表达式的值与某一个case后面的常量相等时,执行该case后面的语句。然后执行break语句switch结构。 若所有的case后面的常量都不和表达式的值相匹配,就执行default后面的语句。 每一个case的常量表达式必须互不相同。 常量可以是整数或字符,switch语句(开关分支语句) 使用if语句嵌套层次太多,容易发生错误,因此在多分支结构中,通常使用switch语句 。 执行过程:,switch( 表达式) case E1: 语句组 1; break; case E2: 语句组 2; break; . case En: 语
17、句组 n; break; default: 语句组n+1 ; ,一般形式:,说明: E1,E2,En是常量表达式,且值必须互不相同 Switch(表达式)中的值自动取整 case En:起语句标号的作用,如果不用break跳出,程序就顺序执行下去。 case后可包含多个可执行语句,且不必加 switch可嵌套 多个case可共用一组执行语句,如: case A: case B: case C: printf(“score60n”); break; .,例:当score为5时,以下程序段的输出是什么? switch(score) case 5: printf(“Very good!”); case 4: printf(“Good!”); case 3: printf(“Pass!”); case 2: printf(“Fail!”); default : printf(“data error!”); ,运行结果:score为5时,输出: Very good! Good! Pass! Fail! data error!,例 (switch的嵌套使用) :写出程序的出结果 main() int x=1,y=0,a=
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026 低压三相异步电机检修电工理论考试参考题库-含答案
- 糖尿病酮症酸中毒(DKA)实验室检查诊断与抢救监测指标
- 有关毒物排泄的试题及答案梳理
- 常见导管相关试题与答案
- 水厂机房设备操作试题及答案
- 1.1我们身边的地理课件(共60张) 湘教版(2024)七年级地理上册
- 2026天津市北辰区中医医院第四批合同制人员招聘2人笔试备考题库及答案详解
- 2026年安徽省交通运输厅所属事业单位公开招聘工作人员11人考试参考题库及答案详解
- 2026年石棉县网格员招聘考试模拟试题及答案解析
- 2026年镇江句容市下蜀镇临时性公益性岗位招聘1人笔试参考题库及答案详解
- 2025年四川省机关事业单位工人技术等级考试(计算机系统操作工·中级)历年参考题库含答案详解(5卷)
- 2025成人高考专升本《日语》试题及答案
- 新初一年级第一次家长会校长发言:家校携手共启新程
- 电子焊接培训课件
- 《人工智能数据服务》高职全套教学课件
- TCAGHP025-2018场地地质灾害危险性评估技术要求(试行)
- 2024年版《煤矿安全生产标准化管理体系基本要求及评分方法》解读
- 糖尿病口服降糖药的分类
- 统计学:假设检验基础
- 肛肠科科普知识宣讲主题讲座培训课件
- 个人简历模板 求职简历模板(10套完整版)
评论
0/150
提交评论