版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2024年个人房屋漏水维修合同范本
- 苏教版六年级科学下册物质的奥秘
- 苏教版四年级上册期中测试卷全科考察
- 人教版画方案
- 七年级数学下册北师大版复习方法
- 春风十里不如归
- 教版木兰诗人的文化研究
- 人教版一年级四季课件
- 科学计数法探索宇宙的秘密
- 苏教版进学课件学习方法与实践
- 新型工业化的概念与背景
- 嗜酸性肉芽肿性血管炎新视野
- 学校 社团课 合同
- 勾股定理的逆定理勾股定理
- 2024年事业单位招聘考试题库(附答案)
- 2023年七年级地理(上册)期末调研卷及答案
- 《研学旅行课程设计》课件-学习情境二 研之有义-研学课程整体设计
- 五年级上美术教学设计-小记者-湘美版
- 五年级信息科技上全册教案 浙教版2023年
- 2024-2029年中国电子元器件行业市场发展现状及发展趋势与投资前景研究报告
- 中职学校实训工作方案
评论
0/150
提交评论