Chapter 4 Decision Structures and Boolean Logic STARTING
Chapter 4 Decision Structures and Boolean Logic STARTING OUT WITH Python First Edition by Tony Gaddis Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
4. 1 The if Statement Concept: The if statement is used to create a decision structure, which allows a program to have more than one path of execution. The if statement causes one or more statements to execute only when a Boolean expression is true. 1 -2 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4 -2
4. 1 The if Statement A control structure is a logical design that controls the order in which a set of statements execute. • A sequence structure is a set of statements that execute in the order that they appear. • A decision (or selection) structure is a control structure that can execute a set of statements and perform a specific action only if a certain condition exists. 1 -3 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4 -3
4. 1 The if Statement Decision (selection) structure: Figure 4 -1 A simple decision structure Diamond symbol represents a true/false condition Single alternative decision structure provides only one alternative path of execution. 1 -4 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4 -4
4. 1 The if Statement Decision (selection) structure: In Python … if condition: statement etc. 1 -5 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4 -5
4. 1 The if Statement Boolean Expressions and Relational Operators A relational operator determines whether a specific relationship exists between two values. Table 4 -1 Relational operators 1 -6 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4 -6
4. 1 The if Statement Boolean Expressions and Relational Operators Table 4 -2 Boolean expressions using relational operators 1 -7 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4 -7
4. 1 The if Statement Nested Blocks Figure 4 -5 Nested blocks 1 -8 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4 -8
4. 2 The if-else Statement Concept: An if-else statement will execute one block of statements if its condition is true, or another block if its condition is false. 1 -9 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4 -9
4. 2 The if-else Statement A dual alternative decision structure provides two possible paths of execution – one path is taken if a condition is true, and the other path is taken if the condition is false. Figure 4 -6 A dual alternative decision structure 1 -10 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4 -10
4. 2 The if-else Statement Dual alternative In Python … if condition: statement etc. else: statement etc. 1 -11 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4 -11
4. 2 The if-else Statement Figure 4 -7 Conditional execution in an if-else statement 1 -12 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4 -12
4. 3 Comparing Strings Concept: Python allows you to compare strings. This allows you to create decision structures that test the value of a string. 1 -13 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4 -13
4. 3 Comparing Strings Program 4 -3 (password. py) 1 -14 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4 -14
4. 3 Comparing Strings Other String Comparisons Figure 4 -10 Character codes for the strings 'Mary' and 'Mark' Figure 4 -11 Comparing each character in a string 1 -15 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4 -15
4. 4 Nested Decision Structures and the if-else Statement Concept: To test more than one condition, a decision structure can be nested inside another decision structure. 1 -16 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4 -16
4. 4 Nested Decision Structures and the if-else Statement Figure 4 -14 A nested decision structure 1 -17 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4 -17
4. 4 Nested Decision Structures and the if-else Statement Figure 4 -16 Nested blocks 1 -18 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4 -18
4. 4 Nested Decision Structures and the if-else Statement The if-else Statement if condition_1: statement etc. elif condition_2: statement etc. else: statement etc. Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1 -19 4 -19
4. 4 Nested Decision Structures and the if-else Statement if score < 60: print ‘Your grade is F. ’ The if-else Statement elif score < 70: print ‘Your grade is D. ’ elif score < 80: print ‘Your grade is C. ’ elif score < 90: print ‘Your grade is B. ’ else: print ‘Your grade is A. ’ 1 -20 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4 -20
4. 5 Logical Operators Concept: The logical and operator and the logical or operator allow you to connect multiple Boolean expressions to create a compound expression. The logical not operator reverses the truth of a Boolean expression. 1 -21 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4 -21
4. 5 Logical Operators Table 4 -3 Logical operators 1 -22 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4 -22
4. 5 Logical Operators Table 4 -4 Compound Boolean expressions using logical operators 1 -23 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4 -23
4. 5 Logical Operators The and Operator Table 4 -5 Truth table for the and operator if temperature < 20 and minutes > 12: print ‘The temperature is in the danger zone. ’ Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1 -24 4 -24
4. 5 Logical Operators The or Operator Table 4 -6 Truth table for the or operator if temperature < 20 or temperature > 100: print ‘The temperature is too extreme. ’ 1 -25 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4 -25
4. 5 Logical Operators The not Operator Table 4 -7 Truth table for the not operator if not(temperature > 100): print ‘This is below the maximum temperature. ’ Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1 -26 4 -26
4. 5 Logical Operators Checking Numeric Ranges with Logical Operators if x >= 20 and x <= 40: print ‘The value is in the acceptable range. ’ if x < 20 or x > 40: print ‘The value is outside the acceptable range. ’ # This is an error! if x < 20 and x > 40: print ‘The value is outside the acceptable range. ’ Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1 -27 4 -27
4. 6 Boolean Variables Concept: A Boolean variable can reference one of two values: True or False. Boolean variables are commonly used as flags, which indicate whether a specific condition exists. 1 -28 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4 -28
4. 6 Boolean Variables if sales >= 50000: sales_quota_met = True else: sales_quota_met = False if sales_quota_met == True: print ‘You have met your sales quota!. ’ OR if sales_quota_met: print ‘You have met your sales quota!. ’ Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1 -29 4 -29
Chapter 4 Decision Structures and Boolean Logic QUESTIONS ? Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
- Slides: 30