730 likes | 845 Views
Programming with Microsoft Visual Basic 2010 5 th Edition. Chapter Four The Selection Structure. Previewing the Monthly Payment Calculator Application. The Monthly Payment Calculator application uses the selection structure. Figure 4-1 Message box. Figure 4-2
E N D
Programming with Microsoft Visual Basic 2010 5th Edition Chapter Four The Selection Structure
Previewing the Monthly Payment Calculator Application • The Monthly Payment Calculator application uses the selection structure
Figure 4-1 Message box Figure 4-2 Monthly payment amount shown in the interface
Lesson A Objectives After studying Lesson A, you should be able to: • Write pseudocode for the selection structure • Create a flowchart to help you plan an application’s code • Write an If...Then...Else statement • Include comparison operators and logical operators in a selection structure’s condition • Change the case of a string • Determine the success of the TryParse method
Making Decisions in a Program • Three basic control structures • Sequence • Selection • Repetition • All procedures in an application are written using one of more of these structures • Procedures in previous chapters used sequence structure only
Making Decisions in a Program (cont’d.) • Selection structure • Chooses one of two paths based on condition • Also called a decision structure • Example: • If employee works over 40 hours, add overtime pay • Condition • Decision expression evaluating to true or false
Making Decisions in a Program (cont’d.) • Single-alternative selection structure • Tasks performed only when condition is true • Dual-alternative selection structure • One set of tasks performed if condition is true • Called true path • Different set of tasks performed if condition is false • Called false path • If and end if • Denotes selection structure’s beginning and end • Else denotes beginning of false path
Making Decisions in a Program (cont’d.) Figure 4-3 Selection structures you might use today
Making Decisions in a Program (cont’d.) • Example: Kanton Boutique Figure 4-4 Problem specification for Kanton Boutique
Figure 4-5 Interface for the Kanton Boutique application Figure 4-6 Pseudocode containing only the sequence structure
Figure 4-7 Modified problem specification and pseudocode containing a single-alternative selection structure
Making Decisions in a Program (cont’d.) • Decision symbol • Diamond shape in a flowchart • Represents the selection structure’s condition • Other symbols • Oval: Start/stop symbol • Rectangle: Process symbol • Parallelogram: Input/output symbol
Figure 4-8 Single-alternative selection structure shown in a flowchart
Figure 4-9 Modified problem specification and pseudocode containing a dual-alternative selection structure
Figure 4-10 Dual-alternative selection structure shown in a flowchart
Coding Single-Alternative and Dual-Alternative Selection Structures • If…Then…Else statement • Used to code single and dual-alternative selection structures • Statement block • Set of statements in each path • Syntax and examples shown in Figure 4-11 on next slide
Figure 4-11 Syntax and examples of the If…Then…Else statement (continues)
Figure 4-11 Syntax and examples of the If…Then…Else statement (cont’d.)
Comparison Operators • Comparison operators • Used to compare two values • Always result in a True or False value • Rules for comparison operators • They do not have an order of precedence • They are evaluated from left to right • They are evaluated after any arithmetic operators in the expression
Figure 4-12 Listing and examples of commonly used comparison operators
Comparison Operators (cont’d.) • Using comparison operators: Swapping numeric values • Sample application displays the lowest and highest of two numbers entered by the user Figure 4-14 Sample run of the Lowest and Highest application
Comparison Operators (cont’d.) Figure 4-15 Pseudocode containing a single-alternative selection structure
Figure 4-16 Flowchart containing a single-alternative selection structure
Comparison Operators (cont’d.) • Values input by the user are stored in variables with procedure scope • A temporary variable is used when values must be swapped • Declared within statement block • Block-level variable • Block scope • Restricts use of variable to statement block in which it is declared
Comparison Operators (cont’d.) Figure 4-18 Illustration of the swapping concept
Comparison Operators (cont’d.) • Using comparison operators: Displaying the sum or difference • Sample application displays the sum or difference of two numbers entered by the user Figure 4-19 Sample run of the Addition and Subtraction application
Comparison Operators (cont’d.) Figure 4-20 Pseudocode containing a dual-alternative selection structure
Figure 4-21 Pseudocode containing a dual-alternative selection structure
Logical Operators • Logical operators • Used to create compound conditions • Expressions evaluate to a Boolean value • True or False • Six logical operators in Visual Basic • Not, And, AndAlso, Or, OrElse, Xor
Figure 4-23 Listing and examples of logical operators (continues)
Figure 4-23 Listing and examples of logical operators (cont’d.)
Logical Operators (cont’d.) • Truth tables • Show how logical operators are evaluated • Short-circuit evaluation • Bypasses evaluation of condition when outcome can be determined without it • Operators using technique: AndAlso, OrElse • Example: • If state = "TN" AndAlso sales > $5000 Then… • If state is not TN, no need to evaluate sales > $5000
Figure 4-24 Truth tables for the logical operators
Logical Operators (cont’d.) • Using the truth tables • Scenario: Calculate a bonus for a salesperson • Bonus condition: “A” rating and sales > $9,000 • Appropriate operators: And, AndAlso (more efficient) • Both conditions must be true to receive bonus • Sample code: strRating = "A”AndAlsodblSales> 9000
Logical Operators (cont’d.) • Using logical operators: Calculating gross pay • Scenario: Calculate and display employee gross pay • Requirements for application • Verify hours are within range (>= 0.0 and <= 40.0) • If data is valid, calculate and display gross pay • If data is not valid, display error message • Can accomplish this using AndAlso or OrElse • Data validation • Verifying that input data is within expected range
Comparing Strings Containing Letters • Scenario: • Display “Pass” if ‘P’ is entered in txtLetter control • Display “Fail” if ‘F’ is entered in txtLetter control • Can use the OrElse or the AndAlso operator • Note that ‘P’ is not the same as ‘p’ • They have different Unicode values
Figure 4-28 Examples of using string comparisons in a procedure
Converting a String to Uppercase or Lowercase • String comparisons are case sensitive • CharacterCasing property: • Three case values: Normal (default), Upper, Lower • ToUpper method: Converts string to uppercase • ToLower method: Converts string to lowercase • Syntax and examples shown in Figure 4-29 on next two slides
Figure 4-29 Syntax and examples of the ToUpper and ToLower methods (continues)
Figure 4-29 Syntax and examples of the ToUpper and ToLower methods (cont’d.)
Converting a String to Uppercase or Lowercase (cont’d.) • Using the ToUpper and ToLower Methods: Displaying a Message • Procedure requirements • Display message “We have a store in this state” • Valid states: IL, IN, KY • Must handle case variations in the user’s input • Can use ToLower or ToUpper • Can assign a String variable to the input text box’s value converted to uppercase
Figure 4-29 Examples of using the ToUpper and ToLower methods in a procedure
Comparing Boolean Values • Boolean variable: Contains either True or False • Naming convention: “Is” denotes Boolean type • Example: blnIsInsured • When testing for a True value, it is not necessary to include the “= True” • Examples in Figure 4-32 on next slide
Comparing Boolean Values (cont’d.) Figure 4-32 Examples of using Boolean values in a condition
Comparing Boolean Values (cont’d.) • Comparing Boolean values: Determining whether a string can be converted to a number • TryParse method returns a numeric value after converting the string, or 0 if it cannot be converted • TryParse also returns a Boolean value indicating success or failure of the conversion attempt • Use Boolean value returned by TryParse method in an If…Then…Else statement
Figure 4-33 Syntax and example of using the Boolean value returned by the TryParse method
Summary of Operators • Precedence of logical operators • Evaluated after any arithmetic or comparison operators in the expression • Summary listing of arithmetic, concatenation, comparison, and logical operators in Figure 4-36 in text
Lesson A Summary • Single and dual-alternative selection structures • Use If...Then...Else statement • Use comparison operators to compare two values • Use a temporary variable to swap values contained in two variables • Use logical operators to create a compound condition • Use text box’s CharacterCasing property to change text to upper- or lowercase