# Selection Logic Building decision-making into programs.

## Presentation on theme: "Selection Logic Building decision-making into programs."— Presentation transcript:

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

3 Sample Problem (v1) Figure 4-2: Rosebud Roses application (sequence structure only)

Sample Problem (v1) Microsoft Visual Basic 2012: Reloaded, Fifth Edition 4

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

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

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)

Modified Problem (v2) Modify our code as seen in previous slide Microsoft Visual Basic 2012: Reloaded, Fifth Edition 9

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

Microsoft Visual Basic 2012: Reloaded, Fifth Edition 11 Sample Problem Modified (v3) Figure 4-5: Modified Rosebud Roses application (dual-alternative selection structure)

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)

Modified Problem (v3) Modify our code as seen in previous slide Microsoft Visual Basic 2012: Reloaded, Fifth Edition 13

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 ( =) Figure 4-7 How to use comparison operators in a condition (continues) ( in math ≥ ) ( in math ≤ ) ( in math ≠ )

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

Modified Problem (v4) flowchart a Microsoft Visual Basic 2012: Reloaded, Fifth Edition 16 Order <= 2 Order <= 6 discountRate =.10 discountRate =.20 discountRate =.05 T T F F

Modified Problem (v4) flowchart b Microsoft Visual Basic 2012: Reloaded, Fifth Edition 17 Order > 6 Order > 2 discountRate =.10 discountRate =.05 discountRate =.20 T T F F

Modified Problem (v4) code b Microsoft Visual Basic 2012: Reloaded, Fifth Edition 18 If ordered > 6 Then discountRate = 0.2 Else If ordered > 2 Then discountRate = 0.1 Else discountRate = 0.05 End If If ordered > 6 Then discountRate = 0.2 Elseif ordered > 2 Then discountRate = 0.1 Else discountRate = 0.05 End If

Similar presentations