Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.

Similar presentations


Presentation on theme: "Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis."— Presentation transcript:

1 Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis Chapter 4: Decision Structures and Boolean Logic

2 Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-2 Chapter Topics 4.1 Introduction to Decision Structures 4.2 Dual Alternative Decision Structures 4.3 Comparing Strings 4.4 Nested Decision Structures 4.5 The Case Structure 4.6 Logical Operators 4.7 Boolean Variables

3 Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-3 4.1 Introduction to Decision Structures A decision structure allows a program to perform actions only under certain conditions Different types of decisions include –If, also called single alternative –If then else, also called dual alternative –Case structure for multiple alternative decisions

4 Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-4 4.1 Introduction to Decision Structures The if statement –An action only occurs if the decision is true If condition Then Statement End If –A diamond symbol is used in flowcharts Figure 4-1 A simple decision structure

5 Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-5 4.1 Introduction to Decision Structures Relational Operators –Determines whether a specific relationship exists between two values –Used within the condition, a Boolean expression x > y x =y x<=y x==y x!=y Table 4-1 Relational operators

6 Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-6 4.2 Dual Alternative Decision Structures If then else statement –Executes one group of statements if it’s Boolean expression is true, or another group if its Boolean expression is false Figure 4-8 A dual alternative decision structure

7 Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-7 4.2 Dual Alternative Decision Structures If condition Then statement Else statement End if If temperature < 40 Then Display “A little cold” Display “Get a coat!” Else Display “Nice weather” Display “And sunny!” End if

8 Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-8 4.3 Comparing Strings Most languages allow you to compare strings

9 Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-9 4.3 Comparing Strings Other String Concerns –String and strings can be compared name1 == name 2 –String and string literals can be compared Month != “October” –String comparisons are generally case sensitive –You can also determine whether one string is greater than or less than another string (allows for sorting strings)

10 Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-10 4.4 Nested Decision Structures Decisions are nested in order to test more than one condition If then if else example Figure 4-15 A nested decision structure

11 Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-11 4.4 Nested Decision Structures The if then else if statement can make nested logic simpler to write If score < 60 Then Display “Grade is F.” Else If score < 70 Then Display “Grade is D.” Else If score < 80 Then Display “Grade is C.” Else If score < 90 Then Display “Grade is B.” Else Display “Grade is A.” End If

12 Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-12 4.5 The Case Structure The case structure lets the value of a variable or an expression determine which path of execution the program will take –Can be used as an alternative to nested decisions Figure 4-18 A case structure

13 Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-13 4.6 Logical Operators Logical Operators are used between conditions to create complex Boolean expressions AND – Both conditions must be true OR – Either condition must be true NOT – Reverses the truth of an expression

14 Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-14 4.6 Logical Operators AND example If temperature 12 Then Display “The temperature is in the danger zone.” End If OR example If temperature 100 Then Display “The temperature is in the danger zone.” End If NOT example If NOT (temperature > 100) Then Display “This is below the maximum temperature.” End If

15 Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-15 4.6 Logical Operators Range Checking Often used for range checking –When checking for a number inside a range, use AND If x >=20 AND x <=40 Then Display “The value is in the acceptable range.” End If –When checking for a number outside a range, use OR If x 40 Then Display “The value is outside the acceptable range.” End If

16 Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-16 4.7 Boolean Variables A variable of the Boolean data type can hold one or two values: true or false Declare Boolean isLunchTime If time >=12 then Set isLunchTime = True Else Set isLunchTime = False End If

17 Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley BREAK 10 minutes

18 Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley END OF BREAK

19 Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Pseudocode from page 122, Figure 4-6: If-Then Structure Declare Integer sales Declare Real bonus Declare Real commissionRate If sales > 50000 Then Set bonus = 500.0 Set commissionRate = 0.12 Display "You’ve met your sales quota!" End If 1-19

20 Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Convert to Visual Basic Console If-Then Structure Sub Main() Dim sales As Integer Dim bonus As Double Dim commissionRate As Double If sales > 50000 Then bonus = 500.0 commissionRate = 0.12 Console.WriteLine("You’ve met your sales quota!") End If End Sub 1-20

21 Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Pseudocode from page 126, Figure 4-9 If-Then-Else structure Declare Integer temperature If temperature < 40 Then Display "A little cold, isn’t it?" Else Display "Nice weather we’re having." End If 1-21

22 Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Convert to Visual Basic Console: If-Then-Else structure Sub Main() Dim temperature As Integer If temperature < 40 Then Console.WriteLine("A little cold, isn’t it?") Else Console.WriteLine("Nice weather we’re having.") End If End Sub 1-22

23 Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Look at pseudocode on pages139-140 Nested If-Then-Else Structures If score < 60 Then Display “Your grade is F.” Else If score < 70 Then Display “Your grade is D.” Else If score < 80 Then Display “Your grade is C.” Else If score< 90 Then Display “Your grade is B.” Else Display “Your grade is A.” End If 1-23

24 Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Pseudocode from page 141, Program 4-7: If-Then-ElseIf structure Declare Integer score If score < 60 Then Display “Your grade is F.” ElseIf score < 70 Then Display “Your grade is D.” ElseIf score < 80 Then Display “Your grade is C.” ElseIf score < 90 Then Display “Your grade is B.” Else Display “Your grade is A.” End If 1-24

25 Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Convert to Visual Basic Console: If-Then-ElseIf structure Sub Main() Dim score As Integer If score < 60 Then Console.WriteLine(“Your grade is F.”) ElseIf score < 70 Then Console.WriteLine(“Your grade is D.”) ElseIf score < 80 Then Console.WriteLine(“Your grade is C.”) ElseIf score < 90 Then Console.WriteLine( “Your grade is B.”) Else Console.WriteLine( “Your grade is A.”) End If End Sub 1-25

26 Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Multiple Choice Questions 1-10 In textbook pages 155 - 157 1-26

27 Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Question 1 A ______ structure can execute a set of statements only under certain circumstances. a.sequence b.circumstantial c.decision d.boolean 1-27

28 Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Question 1 A ______ structure can execute a set of statements only under certain circumstances. a.sequence b.circumstantial c.decision d.boolean 1-28

29 Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Question 2 A ______ structure provides one alternative path of execution. a.sequence b.single alternative decision c.one path alternative d.single execution decision 1-29

30 Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Question 2 A ______ structure provides one alternative path of execution. a.sequence b.single alternative decision c.one path alternative d.single execution decision 1-30

31 Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Question 3 In pseudocode, the If-Then statement is an example of a ______. a.sequence structure b.decision structure c.pathway structure d.class structure 1-31

32 Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Question 3 In pseudocode, the If-Then statement is an example of a ______. a.sequence structure b.decision structure c.pathway structure d.class structure 1-32

33 Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Question 4 A(n) ______ expression has a value of either true or false. a.binary b.decision c.unconditional d.Boolean 1-33

34 Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Question 4 A(n) ______ expression has a value of either true or false. a.binary b.decision c.unconditional d.Boolean 1-34

35 Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Question 5 The symbols, and == are all ______ operators. a.relational b.logical c.conditional d.ternary 1-35

36 Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Question 5 The symbols, and == are all ______ operators. a.relational b.logical c.conditional d.ternary 1-36

37 Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Question 6 A(n) ______ structure tests a condition and then takes one pat if the condition is true, or another path if the condition is false. a.If-Then statement b.single alternative decision c.dual alternative decision d.sequence 1-37

38 Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Question 6 A(n) ______ structure tests a condition and then takes one pat if the condition is true, or another path if the condition is false. a.If-Then statement b.single alternative decision c.dual alternative decision d.sequence 1-38

39 Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Question 7 You use a(n) ______ statement in pseudocode to write a single alternative decision structure. a.Test-Jump b.If-Then c.If-Then-Else d.If-Call 1-39

40 Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Question 7 You use a(n) ______ statement in pseudocode to write a single alternative decision structure. a.Test-Jump b.If-Then c.If-Then-Else d.If-Call 1-40

41 Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Question 8 You use a(n) ______ statement in pseudocode to write a dual alternative decision statement. a.Test-Jump b.If-Then c.If-Then-Else d.If-Call 1-41

42 Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Question 8 You use a(n) ______ statement in pseudocode to write a dual alternative decision statement. a.Test-Jump b.If-Then c.If-Then-Else d.If-Call 1-42

43 Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Question 9 A ______ structure allows you to test the value of a variable or an expression and then use that value to determine which statement or set of statements to execute. a.variable test decision b.single alternative decision c.dual alternative decision d.multiple alternative decision 1-43

44 Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Question 9 A ______ structure allows you to test the value of a variable or an expression and then use that value to determine which statement or set of statements to execute. a.variable test decision b.single alternative decision c.dual alternative decision d.multiple alternative decision 1-44

45 Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Question 10 skip 1-45

46 Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Question 11 AND, OR, and NOT are ______ operators. a.relational b.logical c.conditional d.ternary 1-46

47 Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Question 11 AND, OR, and NOT are ______ operators. a.relational b.logical c.conditional d.ternary 1-47


Download ppt "Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis."

Similar presentations


Ads by Google