




已阅读5页,还剩36页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
A First Book of ANSI C Fourth Edition Chapter 4 Selection A First Book of ANSI C, Fourth Edition2 Objectives Relational Expressions The if and if-else Statements The if-else Chain The switch Statement Case Study: Data Validation Common Programming and Compiler Errors A First Book of ANSI C, Fourth Edition3 Introduction Flow of control refers to the order in which a programs statements are executed Any algorithm can be built using combinations of four standardized flow of control structures: Normal flow of control for all programs is sequential Selection is used to select which statements are performed next based on a condition Repetition is used to repeat a set of statements Invocation is used to invoke a sequence of instructions using a single statement, as in calling a function A First Book of ANSI C, Fourth Edition4 Relational Expressions Simplest decision structure: if (condition) statement executed if condition is true The condition is evaluated to determine its numerical value, which is interpreted as either true (non-zero) or false (0) If condition is “true” the statement following the if is executed; otherwise, statement is not executed The condition used in all of Cs if statements can be any valid C expression Most commonly, a relational expression (can yield only 0 or 1) A First Book of ANSI C, Fourth Edition5 Relational Expressions (continued) A First Book of ANSI C, Fourth Edition6 Relational Expressions (continued) A First Book of ANSI C, Fourth Edition7 Relational Expressions (continued) Relational expressions are also known as conditions A relational expression evaluates to 1 (true) or 0 (false) The expression 3 3.3 has a value of 0 The value of hours 0 depends on the value of hours Character data can also be compared using relational operators A First Book of ANSI C, Fourth Edition8 Relational Expressions (continued) A First Book of ANSI C, Fourth Edition9 Logical Operators More complex conditions can be created using the logical operations AND ( double a = 12.0, b = 2.0, complete = 0.0; A First Book of ANSI C, Fourth Edition14 Logical Operators (continued) The evaluation feature for the A First Book of ANSI C, Fourth Edition22 The if-else Statement The most commonly used if-else statement is if (expression) statement1; else statement2; If the value of expression is 0 statement2, the statement after the reserved word else, is executed A First Book of ANSI C, Fourth Edition23 The if-else Statement (continued) A First Book of ANSI C, Fourth Edition24 The if-else Statement (continued) A First Book of ANSI C, Fourth Edition25 The if-else Statement (continued) A First Book of ANSI C, Fourth Edition26 The if-else Chain Nested if statement: if (expression1) statement1; else if (expression2) statement2; else statement3; Whether 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 default pairing A First Book of ANSI C, Fourth Edition27 The if-else Chain (continued) if-else chain: if (expression1) statement1; else if (expression2) statement2; else statement3; A First Book of ANSI C, Fourth Edition28 The if-else Chain (continued) A First Book of ANSI C, Fourth Edition29 The if-else Chain (continued) A First Book of ANSI C, Fourth Edition30 The if-else Chain (continued) A First Book of ANSI C, Fourth Edition31 The if-else Chain (continued) A First Book of ANSI C, Fourth Edition32 The switch Statement Terminated with a colon default is optional If the break statement was omitted, the following case would be executed A First Book of ANSI C, Fourth Edition33 The switch Statement (continued) A First Book of ANSI C, Fourth Edition34 The switch Statement (continued) A First Book of ANSI C, Fourth Edition35 Case Study: Data Validation Defensive programming is a technique where the program includes code to check for improper data before an attempt is made to process it further Checking user input data for erroneous or unreasonable data is called input data validation Requirements: Write a program to calculate 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. A First Book of ANSI C, Fourth Edition36 Case Study: Data Validation (continued) A First Book of ANSI C, Fourth Edition37 Common Programming Errors Using the assignment operator, =, in place of the relational operator, = Letting the if-else statement appear to select an incorrect choice Nesting if statements without including braces to clearly indicate the desired structure Using a single A compound statement consists of any number of individual statements enclosed within braces An if-else selects between two alternative statements based on the value of an expression A First Book of ANSI C, Fourth Edition40 Summary (continued) An if-else statement can contain other if-else statements The if-else chain is a multiway selection state
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年旅游地产项目可持续发展与旅游市场拓展策略报告
- 2025年绿色建材研发中心项目环保合规性分析报告
- 2025年纺织服装智能化生产环境监测与安全报告
- 现场交叉作业安全管理
- 现场临电知识培训内容课件
- 2026届黑龙江省七台河市化学高二第一学期期中监测试题含解析
- 2025年公务员考试行测数量关系高频考点专项训练及解析
- 2025年Python二级考试押题试卷 深度剖析版
- 星巴克广告策划案(定稿)
- 民法典护理课件
- 2025重庆对外建设(集团)有限公司招聘41人笔试模拟试题及答案解析
- 2025年四川省成都市中考数学真题(含答案卷)
- 2025至2030年中国泥炭行业市场深度分析及投资战略咨询报告
- 工会帮扶救助课件
- 2025年新高考全国一卷地理试题及答案解析
- 热压罐安全操作规程
- 入职合同里的保密协议竞业协议
- 2025年长沙市中考数学真题(含答案)
- 2025年9月新版劳务用工合同范本(可规避风险)
- 浙江宁波江北区重点达标名校2026届中考三模语文试题含解析
- 2025年高考英语全国一卷真题(含答案)
评论
0/150
提交评论