选择结构Chapter3SelectionStatements课件培训课件

上传人:yuzo****123 文档编号:137610508 上传时间:2020-07-10 格式:PPT 页数:50 大小:527.50KB
返回 下载 相关 举报
选择结构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 Statements,3.1 The Boolean Type and Operators 3.2 The if-else Statements 3.3 Case Studies 3.4 Logical Operators 3.5 Switch Statements 3.6 Operator Precedence and Associativity,Boolean (布尔)type The data type with two possible values “true” or “false” bool lighton = true; Internal

2、representation “1” - “true”; “0” - “false” coutlighton; Any non-zero value is treated as true bool lighton = -1; coutlighton;,3.1 The Boolean Type and Operators,2,Relational Operators(关系运算符),Also named “Comparison” (比较)operators To compare two values The result is a Boolean value,3,bool lighton = (1

3、2);,“?”: the result value depends on the truth value of a Boolean expression. Boolean expression: expression with a Boolean value (booleanExp) ? exp1 : exp2 y = (x 0) ? 1 : -1; If x0, y is assigned to be 1; Otherwise, y is assigned to be -1;,Conditional (条件) Operator,4,Sequence:顺序 Selection (branchi

4、ng):选择 Loop (Iteration):循环,Three control structures,6,if if-else switch,Selection Statements,7,if (BooleanExpression) statement(s); ,Simple if Statements,8,if (radius = 0) area = radius * radius * PI; cout The area for the circle of radius radius is area; ,Note,9,Example: Even or Odd?,10,Listing 3.1

5、 - a program that checks whether a number is even or odd. - prompts the user to enter an integer (line 9) - displays “number is even” if it is even (lines 11-12) and “number is odd” if it is odd (lines 14-15).,TestBoolean,A common mistake: Adding a semicolon at the end of an if clause.,Caution,11,Th

6、is mistake is hard to find: Not a compilation error; Not a runtime error; A logic error!,if (booleanExpression) statement(s)-for-the-true-case; else statement(s)-for-the-false-case; ,The if.else Statement,12,if (i k) if (j k) cout i and j are greater than k; else cout i is less than or equal to k;,N

7、ested(嵌套) if Statements,13,Multiple Alternative if Statements,14,Trace if-else statement,15,if (score = 90.0) grade = A; else if (score = 80.0) grade = B; else if (score = 70.0) grade = C; else if (score = 60.0) grade = D; else grade = F;,Suppose score is 70.0,The else clause matches the most recent

8、 if clause in the same block.,Dangling (垂悬) “else”,16,Use braces to make the matching clear!,17,Dangling else,Nothing is printed from the preceding statement. To force the else clause to match the first if clause, you must add a pair of braces: int i = 1; int j = 2; int k = 3; if (i j) if (i k) cout

9、 A; else cout B; This statement prints B.,18,Tips,Error 1: Forgetting Necessary Braces Error 2: Wrong Semicolon at the if Line,Common Errors in Selection,19,Error 3: Mistakenly Using = for =,Common Errors in Selection,20,if (count = 1) cout count is 1 endl; else cout count is not 1 endl;,The Problem

10、 of Body Mass Index (BMI) BMI is a measure of health on weight. The interpretation of BMI for people 16 years or older is as follows:,3.3 Case Studies,21,ComputeBMI,The US federal personal income tax is calculated based on the filing status and taxable income. The tax rates for 2002 are shown in Tab

11、le 3.6.,Example: Computing Taxes,22,if (status = 0) / Compute tax for single filers else if (status = 1) / Compute tax for married file jointly else if (status = 2) / Compute tax for married file separately else if (status = 3) / Compute tax for head of household else / Display wrong status ,Example

12、: Computing Taxes, cont.,23,ComputeTax,A program for a first grader to practice subtractions. Randomly generates two single-digit integers number1 and number2 with number1 = number2, displays a question such as “What is 9 2?” The student types the answer, The program displays a message to indicate w

13、hether the answer is correct.,Example: A Simple Math Learning Tool,24,SubtractionQuiz,Show the output of the following code, with the variable values on the right, respectively.,Review Questions,25,if(x2) if(y2) int z = x+ y; cout z is z endl; else cout x is x endl;,x = 2, y = 3;,x = 3, y = 2;,Also

14、known as “Boolean” operators To operate on Boolean values to get a new one,3.4 Logical (逻辑) Operators,26,Operator Name !not cout number; cout (number % 2 = 0 ,TestBooleanOperators, if p1 is true, it does not evaluate p2.,Short-Circuit (短路)Operator,29,A program to justify whether a given year it is a

15、 leap year. The number is input by user,Example: Leap year,30,LeapYear,(year % 4 = 0 break; 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 S

16、witch Statements,32,switch Statement Flow Chart,33,switch Statement Rules,34,switch (switch-expression) case value1: statement(s)1; break; case value2: statement(s)2; break; case valueN: statement(s)N; break; default: statement(s)-for-default; ,a value of integer enclosed in parentheses.,The case branch is executed when the value in the cas

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 中学教育 > 教学课件 > 高中课件

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