版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、Lesson 8 Flow of control Selection2006/10/172About homework program stylen不同的逻辑意义的段落之间应该空行。比如:头文件包含部分、宏定义部分、主函数部分、变量声明、变量初始化、程序主体、函数返回等等n空格。表达式中,运算符与变量/常量之间应该空格,函数的参数之间也最好空格。如:distance = (double)Speed * 3600 * 24 * 365;比 distance=(double)Speed*3600*24*365;好些 printf(“%d”, a)比printf(“%d”,a)好些n常量也有类型。如
2、:2.13表示一个double型的常量,2.13f表示float型常量3ObjectivesnRelational ExpressionsnThe if and if-else StatementsnThe if-else ChainnThe switch StatementnCase Study: Data Validation4Introduction: Flow of control nrefers to the order in which a programs statements are executednfour kind of control structures:qseque
3、ntialqSelectionqRepetitionqInvocation5Introduction: Selectionnif structure is a single-entry/single-exit structuretruefalseexpressionprint “Passed”A decision can be made on any expression. zero - false nonzero - trueExample:3 - 4 is true6if (grade = 60) printf(“passed”); truefalsegrade = 60printf(“p
4、assed”); if()7Introduction: Selection (continue)8Relational Expressionsinteger (char)floatdoubleSame Type0 false1 true9Relational Expressions (continued)10Relational Expressions (continued)nRelational expressions are also known as conditionsnA relational expression evaluates to 1 (true) or 0 (false)
5、qThe expression 3 3.3 has a value of 0qThe value of hours 0 depends on the value of hours11Relational Expressions (continued)12Relational Expressions (continued)char key = m;int i = 5, j = 7, k = 12;double x = 22.5;13Logical Operations 逻辑运算OperatorSymbol Formlogical AND & * a & blogical OR |
6、 + a | blogical negation ! - ! a14Logical Operators (continued)nThe evaluation feature for the & and | operators that makes the evaluation of an expression stop as soon as it is determined that an expression is false is known as short-circuit evaluation(6 * 3 = 38 / 2) & (13 3 * 3 + 4)|!(6-2
7、 (18 = 19) & (13 9 + 4) | !(4 0 & x = 015Truth Table (真值表) Operand Operator Operand Result 0 1 0 1 & 0 0 1 1 0 0 0 1 0 1 0 1 | 0 0 1 1 0 1 1 1 ! 0 1 1 0 16Precedence (运算的优先级)OperatorsSymbolparentheses unarymultiplicativeadditiverelationalequalitylogical ANDlogical ORconditionalassignment
8、comma( )+ , - , + , - , !* , / , %+ , - , , = , !=&|?:= , += , -= , *= , /= , %=, 17Exp:Given the following declarations :int a = 5 , b = 2 , c = 4 , d=5;b*d+4 = c*c0!(-d+a=1|c5|c%b*d=60) printf (“pass”); else printf (“fail”);truefalsegrade = 60if()print “fail”print “pass”else23nC code:if ( grad
9、e = 60 ) printf( Passedn);else printf( Failedn); warning = 1; For very short statements, you can code a complete if statement placed on a single line24Exp: #include int main () int score; printf(Please input your score:); scanf(%d,&score); if (score=60) printf(Passed!n); else printf(Sorry you fa
10、iled!n); return 0;25The if-else ChainnNested if statement:if (expression1) statement1;else if (expression2) statement2; else statement3;nWhether the indentation exists or not, the compiler will, by default, associate an else with the closest previous unpaired if, unless braces are used to alter this
11、 default pairing26The if/else Selection StructureqIf students grade is greater than or equal to 90Print “A”else If students grade is greater than or equal to 80 Print “B”else If students grade is greater than or equal to 70 Print “C” else If students grade is greater than or equal to 60 Print “D” el
12、se Print “F”27The if-else Chain (continued)28Exp1: sgn( x ) int x,y; printf(input x:); scanf (%d,&x); if (x=0) if (x0) y = 1 ; else y = 0 ; else y = - 1 ; printf( y=%dn,y); 29Exp2: decoder ( x1,x2 ) int x1,x2; printf(input x1,x2: ); scanf (%d%d,&x1,&x2); if (x1=0) if (x2=0) printf(“selec
13、t 3”); else printf(“select 2”); else if (x2=0) printf(“select 1”); else printf(“select 0”); 30The if-else Chain (continued)31The switch StatementTerminated with a colondefault is optionalIf the break statement was omitted, the following case would be executed32The switch Statement (continued)33Exp:3
14、4Case Study: Data ValidationnDefensive programming is a technique where the program includes code to check for improper data before an attempt is made to process it furtherqChecking user input data for erroneous or unreasonable data is called input data validationnRequirements:qWrite a program to ca
15、lculate the square root and the reciprocal of a user-entered number. Validate that the number is not negative before attempting to take its square root, and that the number is not 0 before calculating the numbers reciprocal value.35Case Study: Data Validation (continued)36Common Programming ErrorsnU
16、sing the assignment operator, =, in place of the relational operator, =nNesting if statements without including braces to clearly indicate the desired structurenUsing a single & or | in place of the logical & and logical | operators, respectively37SummarynRelational expressions, which are al
17、so called simple conditions, are used to compare operandsnConditions can be constructed from relational expressions using Cs logical operators, &, |, and !nA one-way if statement has the general formif (expression) statement;nA compound statement consists of any number of individual statements enclosed within bracesnAn if-else selects between two alternative statements based on the value of an expression38Summary (continued)nAn if-else statement can contain other if-else statementsnThe if-else chain is a multiway selection statementnThe switch statement is a multiway selection statement;
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 重庆2025年重庆市属事业单第三季度招聘更正笔试历年参考题库附带答案详解
- 许昌2025年河南许昌职业技术学院招聘13人笔试历年参考题库附带答案详解
- 舟山浙江舟山东港街道招聘后勤工作人员(一)笔试历年参考题库附带答案详解
- 白银2025年甘肃白银市精神卫生中心招聘护理人员笔试历年参考题库附带答案详解
- 职业人群颈椎病的精准干预方案
- 桂林2025年广西桂林市七星区基层医疗卫生事业单位招聘专业技术人员笔试历年参考题库附带答案详解
- 无锡2025年江苏无锡宜兴市人民法院招聘编外用工人员6人笔试历年参考题库附带答案详解
- 德州2025年山东德州乐陵市审计局引进急需紧缺人才2人笔试历年参考题库附带答案详解
- 崇左2025年广西崇左市龙州县卫生健康事业单位招聘107人笔试历年参考题库附带答案详解
- 安庆2025年安徽安庆大观经济开发区招聘工作人员笔试历年参考题库附带答案详解
- 2025新疆能源(集团)有限责任公司共享中心招聘备考题库(2人)带答案详解(完整版)
- 2025至2030中国超纯水(UPW)系统行业项目调研及市场前景预测评估报告
- T∕CAMH 00002-2025 心理咨询师职业能力水平评价标准
- 2025年小学蔬菜颁奖典礼
- DB4114∕T 250-2024 农民田间学校建设管理规范
- 急诊科胸部创伤救治指南
- 二手手机计划书项目方案
- 十年(2016-2025年)高考数学真题分类汇编:专题10 数列解答题综合一(原卷版)
- 医院保洁人员安全管理与保障制度
- 工业园区规划(环境影响评价、水资源论证、安全风险评估等)方案咨询服务投标文件(技术标)
- 2024低温低浊水给水处理设计标准
评论
0/150
提交评论