selection logic building decision-making into programs

18
Selection Logic Building decision-making into programs

Upload: conrad-norris

Post on 15-Jan-2016

224 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Selection Logic Building decision-making into programs

Selection LogicBuilding decision-making into programs

Page 2: Selection Logic Building decision-making into programs

Sample Problem (v1)• Create an application to allow a store clerk to enter:

• the price of a dozen roses• the number of dozens desired

• Calculate and display the total cost of the order

Microsoft Visual Basic 2012: Reloaded, Fifth Edition

2

Page 3: Selection Logic Building decision-making into programs

Microsoft Visual Basic 2012: Reloaded, Fifth Edition

3

Sample Problem (v1)

Figure 4-2: Rosebud Roses application (sequence structure only)

Page 4: Selection Logic Building decision-making into programs

Sample Problem (v1)

Microsoft Visual Basic 2012: Reloaded, Fifth Edition

4

Page 5: Selection Logic Building decision-making into programs

Sample Problem (v1)• Download and look at code

Microsoft Visual Basic 2012: Reloaded, Fifth Edition

5

Page 6: Selection Logic Building decision-making into programs

Sample Problem modified (v2)• Create an application to allow a store clerk to enter:

• the price of a dozen roses• the number of dozens desired

• Customers who order more than 2 dozen roses get a 10% discount

• Calculate and display the total cost of the order

Microsoft Visual Basic 2012: Reloaded, Fifth Edition

6

Page 7: Selection Logic Building decision-making into programs

Microsoft Visual Basic 2012: Reloaded, Fifth Edition

7

Modified Problem (v2)

Figure 4-3: Modified Rosebud Roses application (single-alternative selection structure)

Figure 4-4: Single-alternative selection structure shown in a flowchart

Page 8: Selection Logic Building decision-making into programs

Microsoft Visual Basic 2012: Reloaded, Fifth Edition

8

Coding Single-Alternative and Dual-Alternative Selection Structures

• The If…Then…Else statement is used for coding single-alternative and dual-alternative selection structures

• The set of statements contained in each path is referred to as a statement block

Figure 4-6: How to use the If…Then…Else statement (continues)

Page 9: Selection Logic Building decision-making into programs

Modified Problem (v2)• Modify our code as seen in previous slide

Microsoft Visual Basic 2012: Reloaded, Fifth Edition

9

Page 10: Selection Logic Building decision-making into programs

Sample Problem modified (v3)• Create an application to allow a store clerk to enter:

• the price of a dozen roses• the number of dozens desired

• All customers get a discount• Customers who order more than 2 dozen roses get a 10%

discount• All other orders get a 5% discount

• Calculate and display the total cost of the order

Microsoft Visual Basic 2012: Reloaded, Fifth Edition

10

Page 11: Selection Logic Building decision-making into programs

Microsoft Visual Basic 2012: Reloaded, Fifth Edition

11

Sample Problem Modified (v3)

Figure 4-5: Modified Rosebud Roses application (dual-alternative selection structure)

Page 12: Selection Logic Building decision-making into programs

Microsoft Visual Basic 2012: Reloaded, Fifth Edition

12

Coding Single-Alternative and Dual-Alternative Selection Structures (cont’d.)

Figure 4-6: How to use the If…Then…Else statement (continues)

Page 13: Selection Logic Building decision-making into programs

Modified Problem (v3)• Modify our code as seen in previous slide

Microsoft Visual Basic 2012: Reloaded, Fifth Edition

13

Page 14: Selection Logic Building decision-making into programs

Microsoft Visual Basic 2012: Reloaded, Fifth Edition

14

Comparison (Relational) Operators• Each comparison operator (also referred to as relational

operators) can be used to compare two values, and the comparison always results in a Boolean value: either True or False. • When comparing values, keep in mind that equal to (=) is the opposite

of not equal to (<>), greater than (>) is the opposite of less than or equal to (<=), and less than (<) is the opposite of greater than or equal to (>=)

Figure 4-7 How to use comparison operators in a condition (continues)

( in math ≥ )

( in math ≤ )( in math ≠ )

Page 15: Selection Logic Building decision-making into programs

Modified Problem (v4)• Instead of 2 discount rate levels, have 3 discount rate levels:

• 1 or 2 dozen: 5%• 3 to 6 dozen: 10%• More than 6 dozen: 20%

Microsoft Visual Basic 2012: Reloaded, Fifth Edition

15

Page 16: Selection Logic Building decision-making into programs

Modified Problem (v4) flowchart a

Microsoft Visual Basic 2012: Reloaded, Fifth Edition

16

Order <= 2

Order <= 6

discountRate = .10discountRate = .20

discountRate = .05

T

T

F

F

Page 17: Selection Logic Building decision-making into programs

Modified Problem (v4) flowchart b

Microsoft Visual Basic 2012: Reloaded, Fifth Edition

17

Order > 6

Order > 2

discountRate = .10discountRate = .05

discountRate = .20

T

T

F

F

Page 18: Selection Logic Building decision-making into programs

Modified Problem (v4) code b

Microsoft Visual Basic 2012: Reloaded, Fifth Edition

18

If ordered > 6 Then discountRate = 0.2Else If ordered > 2 Then discountRate = 0.1 Else discountRate = 0.05 End IfEnd If

If ordered > 6 Then discountRate = 0.2Elseif ordered > 2 Then discountRate = 0.1Else discountRate = 0.05End If