版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年肝脏损伤模拟试题(含答案)
- 2026年磨具项目安全调研评估报告
- 2026年母婴考核模拟题及答案详解
- 2026年商家运营考试模拟题及答案详解
- 2026年电工安全模拟试题模拟试卷及答案详解
- 医院感染管理知识竞赛试题及答案详解
- 2026年普通遗传学理论综合模拟题及答案详解
- 2026年反洗钱行业安全培训模拟试题(含答案)
- 2026年社工专业知识测试模拟题集锦试题及答案详解
- 2026年山西幼师考试模拟题及答案详解
- DB11∕T 2363-2024 医疗机构安宁疗护服务规范
- 纺织企业职业病防护与检测报告
- 《文学理论》第九讲:文学风格、流派与思潮
- 《现代办公事务处理》全套教学课件
- 城市道路清扫保洁与质量评价标准 CJJT126-2022
- 江西省人防工程质量监督管理指导手册(试行)
- 长沙市外墙外保温、外墙内保温、屋面标准图集
- 系列非晶合金干式电力变压器安装使用说明书
- 九年级上册语文理解性默写(含答案)
- 领导干部的法治思维-胡锦光教学课件
- 聚苯硫醚纤维演示文稿
评论
0/150
提交评论