版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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年人工智能训练师(三级)职业技能鉴定理论考试题库(含答案)
- 企业管理-村里消防管理制度
- UIBE数字经济实验室-北京市货物贸易月度监测报告(2026年1-8月)
- 2026年校招:中成控股集团试题及答案
- 名人传中考试题及答案
- 2026年校招:中国华能笔试题及答案
- 吉利汽车GEELY+品牌VI手册 Geely Auto Communication Guidelines (New Energy 2025)
- 电缆绝缘检测方法
- 2026年山东名校考试联盟5月联考(核心素养评估)地理试题(含答案)
- 离子束抛光控制算法:原理、应用与优化策略
- 化工园区多米诺效应分析
- 新课标引领下高中地理课堂教学设计的创新转型
- 35KV变电站施工方案
- 跳蚤的自我设障课件
- 2024年山东大学校长开学讲话稿8000字
- 学习《水利水电工程生产安全重大事故隐患判定导则-SLT 842》课件
- (2025)医院招聘护士考试题库(附参考答案)
评论
0/150
提交评论