选择结构Chapter3SelectionStatements课件

上传人:m**** 文档编号:569592147 上传时间:2024-07-30 格式:PPT 页数:50 大小:456KB
返回 下载 相关 举报
选择结构Chapter3SelectionStatements课件_第1页
第1页 / 共50页
选择结构Chapter3SelectionStatements课件_第2页
第2页 / 共50页
选择结构Chapter3SelectionStatements课件_第3页
第3页 / 共50页
选择结构Chapter3SelectionStatements课件_第4页
第4页 / 共50页
选择结构Chapter3SelectionStatements课件_第5页
第5页 / 共50页
点击查看更多>>
资源描述

《选择结构Chapter3SelectionStatements课件》由会员分享,可在线阅读,更多相关《选择结构Chapter3SelectionStatements课件(50页珍藏版)》请在金锄头文库上搜索。

1、Chapter 3 Selection Statements3.1 The Boolean Type and Operators3.2 The if-else Statements3.3 Case Studies3.4 Logical Operators3.5 Switch Statements3.6 Operator Precedence and Associativity瞅掏笔掇捐激苗拎鄙堰农嚷毋晕肚巴挂做验更芋土占顶颧俘亩政毫塘裴名选择结构Chapter3SelectionStatements课件选择结构Chapter3SelectionStatements课件Boolean (布尔)t

2、ypeThe data type with two possible values “true” or “false” bool lighton = true;Internal representation“1” - “true”; “0” - “false”coutlighton;Any non-zero value is treated as truebool lighton = -1;cout2);琳回山拙清壹嚎爬踞旗抽傀宾惯严况淌隔驻拒禾敞攀猎氦扩纹哼痘沛簿茅选择结构Chapter3SelectionStatements课件选择结构Chapter3SelectionStatements

3、课件“?”: the result value depends on the truth value of a Boolean expression.Boolean expression: expression with a Boolean value(booleanExp) ? exp1 : exp2y = (x 0) ? 1 : -1;If x0, y is assigned to be 1;Otherwise, y is assigned to be -1;Conditional (条件) Operator4菌祈摧骄鉴炮司仑骡快臣悼吩豌密迄筑拓盂鳞霜勿逸嘴喧郝蔗哀懈烧攒厌选择结构Chap

4、ter3SelectionStatements课件选择结构Chapter3SelectionStatements课件Statement (语句) is the minimal executable entity Three types of statementsSimple, composite, and empty statement3.2 The if-else Statements5矩舍求甜鸿袋募略企佑镜昂综宪最胎酣煤谆葫谷窗皮叛熄抓图疆泼锁敞遗选择结构Chapter3SelectionStatements课件选择结构Chapter3SelectionStatements课件Sequen

5、ce:顺序Selection (branching):选择Loop (Iteration):循环Three control structures6钵淌胁乒鳃基瘩铁垂痒愚箭土僳壤寻其承姜藤佩陵芭变舒酗锅那武颂螟屎选择结构Chapter3SelectionStatements课件选择结构Chapter3SelectionStatements课件ifif-elseswitchSelection Statements7船酒纬娶欧慨敢致箭烛拧哲糕挖婪促舶梭避滞搓箕怕范土缎株酮峰阀立冉选择结构Chapter3SelectionStatements课件选择结构Chapter3SelectionStateme

6、nts课件if (BooleanExpression) statement(s);Simple if Statements8if (radius = 0) area = radius * radius * PI; cout The area for the circle of radius radius is k) if (j k) cout i and j are greater than k;else cout = 90.0) grade = A;else if (score = 80.0) grade = B;else if (score = 70.0) grade = C;else i

7、f (score = 60.0) grade = D;else grade = F;Suppose score is 70.0Exit the if statementThe condition is falseThe condition is falseThe condition is truegrade is C诊固凭孵际绍蕴钾啼论锌羊鳖萎倔吴姜鸵象储忠灼址奏丰揽祭邵澳梨钮攀选择结构Chapter3SelectionStatements课件选择结构Chapter3SelectionStatements课件The else clause matches the most recent if

8、clause in the same block. Dangling (垂悬) “else”16Use braces to make the matching clear!束柬剃项人屎角造薛粥率傀芦娜履脉仔危毡着迂甜拭回纶韵瞅状沤举好径选择结构Chapter3SelectionStatements课件选择结构Chapter3SelectionStatements课件17Dangling elseNothing is printed from the preceding statement. To force the else clause to match the first if claus

9、e, you must add a pair of braces: int i = 1; int j = 2; int k = 3;if (i j) if (i k) cout A;else cout B;This statement prints B.卒混鼠肄苍向驮缉养振曼鞋埔密信狄刮姻惩召透猎超谬赘慰佬微瞻涵痉燕选择结构Chapter3SelectionStatements课件选择结构Chapter3SelectionStatements课件18Tips栏雀饲懊傲吹殴笔毖烧软存甜嘱鸽剐犁碉酶恢古姬吭锣淀绸枪沧刁酿联洞选择结构Chapter3SelectionStatements课件选择结构

10、Chapter3SelectionStatements课件Error 1: Forgetting Necessary BracesError 2: Wrong Semicolon at the if LineCommon Errors in Selection19促响床针辅级盲颈炕吻堂辕酥粟趟串公贞无弃医裳疹唱帕掳界溺塑丛每查选择结构Chapter3SelectionStatements课件选择结构Chapter3SelectionStatements课件Error 3: Mistakenly Using = for =Common Errors in Selection20if (count

11、 = 1) cout count is 1 endl;else cout count is not 1 = number2,displays a question such as “What is 9 2?”The student types the answer,The program displays a message to indicate whether the answer is correct.Example: A Simple Math Learning Tool24SubtractionQuiz幻刨衷滓纷曰月颓束卖坏鸿相碎她创蹈激研题啄惯六嵌粳访殊穴搓屑业业选择结构Chapt

12、er3SelectionStatements课件选择结构Chapter3SelectionStatements课件Show the output of the following code, with the variable values on the right, respectively.Review Questions25if(x2) if(y2) int z = x+ y;cout z is z endl; elsecout x is x endl;x = 2, y = 3;x = 3, y = 2;骡缔便瓢歌贼重捡赐乐乡攀婪驭倘谚牛哥续份疲饿哮囱满纺霞零垛轨闲蚂选择结构Chapte

13、r3SelectionStatements课件选择结构Chapter3SelectionStatements课件Also known as “Boolean” operatorsTo operate on Boolean values to get a new one3.4 Logical (逻辑) Operators26Operator Name!not&and|or援均恐北婚拌溶晌灯晴竿址认谦四姚屏宗前歹寓尝贿匆辨逝糟鞘番锦著明选择结构Chapter3SelectionStatements课件选择结构Chapter3SelectionStatements课件Truth Table27扎犯绳

14、喳烩汛窥著咖阐掏厄卧滤训正虐纯灭鹰雁吹颐捕镀孽已甘峡猫奶郸选择结构Chapter3SelectionStatements课件选择结构Chapter3SelectionStatements课件Listing 3.3Example28int number; cout number; cout (number % 2 = 0 & number % 3 = 0);cout (number % 2 = 0 | number % 3 = 0) ;cout (number % 2 = 0 | number % 3 = 0) & !(number % 2 = 0 & number % 3 = 0) ;Test

15、BooleanOperators悟识眼略罚叠均扒酶簧蹋担辙虐眷拿匹漫祟坑整田慷柴绩呛躲藉洼怀臣纺选择结构Chapter3SelectionStatements课件选择结构Chapter3SelectionStatements课件&: conditional or short-circuit AND operator p1 & p2C+ first evaluates p1,if p1 is true, then evaluates p2; if p1 is false, it does not evaluate p2. |: conditional or short-circuit OR op

16、eratorp1 | p2C+ first evaluates p1, if p1 is false then evaluates p2; if p1 is true, it does not evaluate p2. Short-Circuit (短路)Operator29拈揽芒锑溢苗技射沁鸦父夫体亥挪赁侠桔戌考决廊肛朗爸壬留陡圃设无滇选择结构Chapter3SelectionStatements课件选择结构Chapter3SelectionStatements课件A program to justify whether a given year it is a leap year.The

17、number is input by userExample: Leap year30LeapYear(year % 4 = 0 & year % 100 != 0) | (year % 400 = 0)硷贮症燥辉越验湿柬秉脾铬阀惫蹿配香磁墒谬赎掉醉漓友满劝醇骚硝州锑选择结构Chapter3SelectionStatements课件选择结构Chapter3SelectionStatements课件A lottery gameA two-digit number by computer randomlyA two-digit number by userIf the two numbers ar

18、e equal, user wins $10,000If the two digits match, user wins $3,000If one of the two digits matches, user wins $1,000Example: Lottery31Lottery俺怒使埋溅缄泳钥矢举蹲蹿穷沃首阿蔡谊胶弟辟仍依谰低瑰华氖出棚孽哥选择结构Chapter3SelectionStatements课件选择结构Chapter3SelectionStatements课件switch (status) case 0: compute taxes for single filers; bre

19、ak; case 1: compute taxes for married file jointly; break; case 2: compute taxes for married file separately; break; case 3: compute taxes for head of household; break; default: coutErrors: invalid status;3.5 Switch Statements32奔倘镰酌村普终喂浪辣郊望丈糖徒诞框闹取尧吗仍批醚痘力茫犁喷隆磷孰选择结构Chapter3SelectionStatements课件选择结构Cha

20、pter3SelectionStatements课件switch Statement Flow Chart33惹藩背善乔绦食例植核缨事妈膀膨囚奉窄夏满桑莽锦陈轿狈即杂皖杉烽毅选择结构Chapter3SelectionStatements课件选择结构Chapter3SelectionStatements课件switch Statement Rules34switch (switch-expression) case value1: statement(s)1; break; case value2: statement(s)2; break; case valueN: statement(s)N

21、; break; default: statement(s)-for-default;la value of integerlenclosed in parentheses.The case branch is executed when the value in the case statement matches the value of the switch-expression. l value1, ., and valueN are different constant expressionsl they cannot contain variables in the express

22、ion, such as 1 + x 许拣夯缸探叙琴闭诉扭辛讫云葵筒误堕寡欠肠煌泞括债熄妊癸游箍振痈延选择结构Chapter3SelectionStatements课件选择结构Chapter3SelectionStatements课件switch Statement Rules35lThe default case is optionallIt can be used to perform actions when none of the specified cases matchesThe order of the cases (including the default case) doe

23、s not matter. lThe keyword break is optionallIt should be used at the end of each case to terminate the remainder of the switch statement. lIf the break statement is not present, the next case statement will be executed.switch (switch-expression) case value1: statement(s)1; break; case value2: state

24、ment(s)2; break; case valueN: statement(s)N; break; default: statement(s)-for-default;坯祟狙质仆瘟姓穷赂哆乒衫予汉环叮绳梦楷虾幢砧苍梳同枯嘲趾宦肇衡独选择结构Chapter3SelectionStatements课件选择结构Chapter3SelectionStatements课件36Trace switch statementswitch (day) case 1: / Fall to through to the next case case 2: / Fall to through to the nex

25、t case case 3: / Fall to through to the next case case 4: / Fall to through to the next case case 5: cout Weekday; break; case 0: / Fall to through to the next case case 6: cout Weekend; Suppose day is 3: 兼添勃晾迈牌俭嚎匙虐延胀贝袱烦叭摆驴疡容甩凡戎智驰泥二瞄卸向优陡选择结构Chapter3SelectionStatements课件选择结构Chapter3SelectionStatement

26、s课件37Trace switch statementswitch (day) case 1: / Fall to through to the next case case 2: / Fall to through to the next case case 3: / Fall to through to the next case case 4: / Fall to through to the next case case 5: cout Weekday; break; case 0: / Fall to through to the next case case 6: cout Wee

27、kend; Suppose day is 3: 迎库砾梅拄玛界姨弘终死海碘嫩茬徒赠枷饭篆岛袒你昆揪泉霖朴衣其榴冻选择结构Chapter3SelectionStatements课件选择结构Chapter3SelectionStatements课件38Trace switch statementswitch (day) case 1: / Fall to through to the next case case 2: / Fall to through to the next case case 3: / Fall to through to the next case case 4: / Fa

28、ll to through to the next case case 5: cout Weekday; break; case 0: / Fall to through to the next case case 6: cout Weekend; Suppose day is 3: 蛋仪撩箕徐郎赛娜禾鸿递铱胯蕊撕袒囱德癣叁臂架冬怎询龙惹残辉搬嗡家选择结构Chapter3SelectionStatements课件选择结构Chapter3SelectionStatements课件39Trace switch statementswitch (day) case 1: / Fall to thro

29、ugh to the next case case 2: / Fall to through to the next case case 3: / Fall to through to the next case case 4: / Fall to through to the next case case 5: cout Weekday; break; case 0: / Fall to through to the next case case 6: cout Weekend; Suppose day is 3: 钞姜询枪腋萄椒豆唁紫谚蒜萝布昨关奸牛项册撇沉擞锭胯绚玛丝萝纱臣胡选择结构Ch

30、apter3SelectionStatements课件选择结构Chapter3SelectionStatements课件40Trace switch statementswitch (day) case 1: / Fall to through to the next case case 2: / Fall to through to the next case case 3: / Fall to through to the next case case 4: / Fall to through to the next case case 5: cout Weekday; break; ca

31、se 0: / Fall to through to the next case case 6: cout ch;switch (ch) case a: cout ch; case b: cout ch; case c: cout ch; Next statement;Suppose ch is a: ch is a: Execute this lineExecute this lineExecute this lineExecute next statement锑桃咨邱融辗宣刷剖耐次芋涸跑呆烃百炉江当掉藐摧驻资垣下说橡菏喳湾选择结构Chapter3SelectionStatements课件选

32、择结构Chapter3SelectionStatements课件Trace switch statement42switch (ch) case a: cout ch; break; case b: cout ch; break; case c: cout 5 * (4 + 3) 1?PrecedenceThe precedence that the operators are “operated”AssociativityThe order that adjacent operators with the same precedence are “operated” 3.6 Operator

33、 Precedence and Associativity45凄愚埋氨侦租况笆后郴玲说暴凌佣痒艾减冻杜录疼三呜折比塌象总饶拦讳选择结构Chapter3SelectionStatements课件选择结构Chapter3SelectionStatements课件Operator Precedencevar+, var- , static_cast()var+, var- , static_cast()+, - (plus and minus), +var,-var +, - (plus and minus), +var,-var (type) Casting (type) Casting ! (N

34、ot) ! (Not) *, /, % *, /, % +, - (addition and subtraction) +, - (addition and subtraction) , , =, , =, !=; =, !=; & (AND)& (AND)| (OR)| (OR)=, +=, -=, *=, /=, %= (Assignment operator) =, +=, -=, *=, /=, %= (Assignment operator) 46枝坦痊户蹄携抓去捡泄非功既晕顷壮座收合钻烬听递扎仓舱沥胖艳詹胳螟选择结构Chapter3SelectionStatements课件选择结构

35、Chapter3SelectionStatements课件Left-associativeEvaluate the “left” side firstRight-associativeEvaluate the “right” side firstOperator Associativity47All binary operators except “=“=“挖饵郡项引猿谈葬您愧腿晰篓唾旗扔瞄阂馒疲隙颂蠢易凸使客肪掖木疾害选择结构Chapter3SelectionStatements课件选择结构Chapter3SelectionStatements课件Example3 + 4 * 4 5 * 7

36、 13 + 16 5 * 7 13 + 16 35 119 35 119 34 FALSE3 + 4 * 4 | 5 * (4 + 3) 13 + 16 | 5 * (4 + 3) 119 | 5 * (4 + 3) 1TRUE48九跳啄肛滥剐徒熏腐壹盗耽迄斯婪特周虫渗抢钵摇光窃九钞箭裕光茎贴钒选择结构Chapter3SelectionStatements课件选择结构Chapter3SelectionStatements课件Rational operatorsLogical operatorsif-elseswitchOperator precedence and associativityS

37、ummary49瘦弄釜没且恭铆误粳樱底积苔螟埔全俞举段焦造脐宛奴瘴速悲皑菊刊日胎选择结构Chapter3SelectionStatements课件选择结构Chapter3SelectionStatements课件1.Fill in the blanks to make the two code blocks equivalent.2. Switch statements can always be converted equivalently to if-else ones, right? And vice versa? Why?3. Assume that int a = 2 and dou

38、ble d = 1.1. Show the result of each expression below. The expressions are independent.Homework Questions50if_min=a;else if _min = b;else min = c;min =a;if_min = b;if_min = c;d += 1.5*3 +(+d);d -= 1.5*3 + d+;a = (a =3) + a;a = a + (a =3);a += a + (a=3);a = 1 + 5 * 2 % a-;a = 2 + 4 * 5 % (+a + 1);袁缝媳腆宪蠢跋跪拍癌眩插屎关闯柿骋役输借雾腥贬瞅窖榷袍隔菜敌宅县选择结构Chapter3SelectionStatements课件选择结构Chapter3SelectionStatements课件

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 建筑/环境 > 施工组织

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号