Presentation is loading. Please wait.

Presentation is loading. Please wait.

Selection Logic Building decision-making into programs.

Similar presentations


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

1 Selection Logic Building decision-making into programs

2 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 3 Sample Problem (v1) Figure 4-2: Rosebud Roses application (sequence structure only)

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

5 Sample Problem (v1) Download and look at code Microsoft Visual Basic 2012: Reloaded, Fifth Edition 5

6 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 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

8 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)

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

10 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

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

12 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)

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

14 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 ≠ )

15 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

16 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

17 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

18 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


Download ppt "Selection Logic Building decision-making into programs."

Similar presentations


Ads by Google