Microsoft Visual Basic 2008: Reloaded Fourth Edition Chapter Four Making Decisions in a Program Objectives After studying this chapter, you should be able to: • Include the selection structure in pseudocode and in a flowchart • Explain the difference between single-alternative and dual-alternative selection structures • Code a selection structure using the If…Then…Else statement • Include comparison operators and logical operators in a selection structure’s condition • Create a block-level variable Microsoft Visual Basic 2010: Reloaded, Fourth Edition 2 Objectives (cont'd.) • Concatenate strings • Use the ControlChars.NewLine constant • Change the case of a string • Include a check box in an interface • Generate random numbers Microsoft Visual Basic 2010: Reloaded, Fourth Edition 3 The Selection Structure • Selection structure (or decision structure): – Used to select a path to take based on the outcome of a decision or comparison • Condition: – The decision to be made – Results in a Boolean (True or False) answer • Single-alternative selection structure: performs a set of tasks only when the condition is true • True path: the tasks to perform when the condition is true Microsoft Visual Basic 2010: Reloaded, Fourth Edition 4 The Selection Structure (cont’d.) • Dual-alternative selection structure: contains one set of tasks to perform when the condition is true and a different set of tasks to perform when the condition is false • False path: the tasks to perform when the condition is false • Pseudocode uses if…end if to denote a selection structure and else to denote the false path – Indent instructions within the selection structure Microsoft Visual Basic 2010: Reloaded, Fourth Edition 5 The Selection Structure (cont'd.) Figure 4-1: Selection structures you might use today Microsoft Visual Basic 2010: Reloaded, Fourth Edition 6 The Selection Structure (cont'd.) Figure 4-1: Selection structures you might use today (cont’d.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition 7 The Selection Structure (cont'd.) Figure 4-2: Problem specification for Mountain Biking Figure 4-3: Interface for the Mountain Biking application Microsoft Visual Basic 2010: Reloaded, Fourth Edition 8 The Selection Structure (cont'd.) Figure 4-4: Pseudocode containing only the sequence structure Microsoft Visual Basic 2010: Reloaded, Fourth Edition 9 The Selection Structure (cont'd.) Figure 4-5: Modified problem specification and pseudocode containing a single-alternative selection structure Microsoft Visual Basic 2010: Reloaded, Fourth Edition 10 Figure 4-6: Single-alternative selection structure shown in a flowchart Microsoft Visual Basic 2010: Reloaded, Fourth Edition 11 Figure 4-7: Modified problem specification and pseudocode containing a dual-alternative selection structure Microsoft Visual Basic 2010: Reloaded, Fourth Edition 12 Figure 4-8: Dual-alternative selection structure shown in a flowchart Microsoft Visual Basic 2010: Reloaded, Fourth Edition 13 Coding Single-Alternative and DualAlternative Selection Structures • If…Then…Else statement: used to code singlealternative and dual-alternative selection structures – Else clause: an optional part of the If statement • Only used for the dual-alternative selection structure – Condition must be a Boolean expression that evaluates to either True or False • Can contain variables, literal constants, named constants, properties, methods, arithmetic operators, comparison operators, and logical operators • Statement block: set of statements in the true path or the false path Microsoft Visual Basic 2010: Reloaded, Fourth Edition 14 Figure 4-9: How to use the If…Then…Else statement Microsoft Visual Basic 2010: Reloaded, Fourth Edition 15 Figure 4-9: How to use the If…Then…Else statement (cont’d.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition 16 Comparison Operators • Comparison operators (or relational operators): – Used as part of the condition in an If…Then…Else statement to compare two values • Most commonly used comparison operators: – – – – – – Equal to: = Greater than: > Greater than or equal to: >= Less than: < Less than or equal to: <= Not equal to: <> Microsoft Visual Basic 2010: Reloaded, Fourth Edition 17 Figure 4-10: How to use comparison operators in a condition Microsoft Visual Basic 2010: Reloaded, Fourth Edition 18 Comparison Operators (cont’d.) Figure 4-10: How to use comparison operators in a condition (cont’d.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition 19 Comparison Operators (cont'd.) • Comparison operators: – Have no order of precedence – Are evaluated from left to right in an expression – Are evaluated after any arithmetic operators in the expression • All expressions containing comparison operators evaluate to True or False only Microsoft Visual Basic 2010: Reloaded, Fourth Edition 20 Comparison Operators (cont'd.) Figure 4-11: Evaluation steps for an expression containing arithmetic and comparison operators Microsoft Visual Basic 2010: Reloaded, Fourth Edition 21 Comparing Numeric Values • Auction House application displays highest and lowest of two bids entered by the user Figure 4-12: Sample run of the Auction House application Microsoft Visual Basic 2010: Reloaded, Fourth Edition 22 Comparing Numeric Values (cont'd.) Figure 4-13: Pseudocode containing a single-alternative selection structure Microsoft Visual Basic 2010: Reloaded, Fourth Edition 23 Figure 4-14: Flowchart containing a single-alternative selection structure Microsoft Visual Basic 2010: Reloaded, Fourth Edition 24 Comparing Numeric Values (cont'd.) Figure 4-15: Code entered in the Display button’s Click event procedure Microsoft Visual Basic 2010: Reloaded, Fourth Edition 25 Comparing Numeric Values (cont'd.) Figure 4-15: Code entered in the Display button’s Click event procedure (cont’d.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition 26 Comparing Numeric Values (cont'd.) • Block-level variables: declared within a statement block and remain in memory until the procedure ends • Block scope: A block-scope variable can only be used within the statement block in which it was declared • Concatenation operator (&): connects or links two strings together • ControlChars.NewLine constant: – Advances the insertion point to the next line Microsoft Visual Basic 2010: Reloaded, Fourth Edition 27 Comparing Numeric Values (cont'd.) Figure 4-16: Illustration of the swapping concept Microsoft Visual Basic 2010: Reloaded, Fourth Edition 28 Comparing Numeric Values (cont'd.) Figure 4-17: How to concatenate strings Microsoft Visual Basic 2010: Reloaded, Fourth Edition 29 Comparing Strings • Addition and Subtraction Calculator application: displays the sum or difference of two numbers Figure 4-18: Sample run of the Addition and Subtraction Calculator application Microsoft Visual Basic 2010: Reloaded, Fourth Edition 30 Comparing Strings (cont'd.) Figure 4-19: Pseudocode containing a dual-alternative selection structure Microsoft Visual Basic 2010: Reloaded, Fourth Edition 31 Figure 4-20: Flowchart containing a dual-alternative selection structure Microsoft Visual Basic 2010: Reloaded, Fourth Edition 32 Comparing Strings (cont'd.) • MaxLength property: text box property that specifies the maximum number of characters that can be entered • CharacterCasing property: text box property that indicates if text should remain as typed or be converted to uppercase or lowercase Microsoft Visual Basic 2010: Reloaded, Fourth Edition 33 Figure 4-21: Calculate button’s Click event procedure Microsoft Visual Basic 2010: Reloaded, Fourth Edition 34 The ToUpper and ToLower Methods • String comparisons in Visual Basic are casesensitive • ToUpper method: converts a string to uppercase • ToLower method: converts a string to lowercase • ToUpper and ToLower can be used to permanently or temporarily convert a variable’s contents Microsoft Visual Basic 2010: Reloaded, Fourth Edition 35 The ToUpper and ToLower Methods (cont’d.) Figure 4-22: How to use the ToUpper and ToLower methods Microsoft Visual Basic 2010: Reloaded, Fourth Edition 36 The ToUpper and ToLower Methods (cont’d.) Figure 4-22: How to use the ToUpper and ToLower methods (cont'd.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition 37 The ToUpper and ToLower Methods (cont’d.) Figure 4-22: How to use the ToUpper and ToLower methods (cont'd.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition 38 Figure 4-23: Examples of using the ToUpper method in the calcButton Click event procedure Microsoft Visual Basic 2010: Reloaded, Fourth Edition 39 Figure 4-23: Examples of using the ToUpper method in the calcButton Click event procedure (cont’d.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition 40 Comparing Boolean Values • Check boxes: used to offer the user one or more independent and nonexclusive items from which to choose Figure 4-24: A different interface for the Addition and Subtraction Calculator application Microsoft Visual Basic 2010: Reloaded, Fourth Edition 41 Comparing Boolean Values (cont’d.) Figure 4-25: Click event procedures for the subtractionCheckBox and calcButton Microsoft Visual Basic 2010: Reloaded, Fourth Edition 42 Comparing Boolean Values (cont’d.) Figure 4-25: Click event procedures for the subtractionCheckBox and calcButton (cont’d.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition 43 Logical Operators • Logical operators (or Boolean operators): – Used to combine two or more conditions into one compound condition • Compound condition: a combination of conditions using logical operator(s) Microsoft Visual Basic 2010: Reloaded, Fourth Edition 44 Logical Operators (cont'd.) Figure 4-26: How to use logical operators in a condition Microsoft Visual Basic 2010: Reloaded, Fourth Edition 45 Logical Operators (cont'd.) Figure 4-26: How to use logical operators in a condition (cont'd.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition 46 Figure 4-26: How to use logical operators in a condition (cont'd.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition 47 Logical Operators (cont'd.) • Truth tables: used to evaluate logical operators in an expression • Short-circuit evaluation: an evaluation in which the second condition may not be evaluated • AndAlso evaluates to True only when both subconditions are True • OrElse evaluates to False only when both subconditions are False • AndAlso and OrElse operations do not evaluate the second condition if the first condition is false Microsoft Visual Basic 2010: Reloaded, Fourth Edition 48 Logical Operators (cont'd.) Figure 4-27: Truth tables for the AndAlso and OrElse logical operators Microsoft Visual Basic 2010: Reloaded, Fourth Edition 49 Using the Truth Tables • Use And or AndAlso when both conditions must be true to give a true result • Use Or or OrElse when one or both conditions must be true to give a true result • Remember: logical operators are evaluated after arithmetic or comparison operators in an expression Microsoft Visual Basic 2010: Reloaded, Fourth Edition 50 The Carroll Company Application • Data validation: – Process of verifying that the input data is within the expected range Figure 4-28: Two ways of writing the calcButton Click event procedure Microsoft Visual Basic 2010: Reloaded, Fourth Edition 51 Figure 4-28: Two ways of writing the calcButton Click event procedure (cont’d.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition 52 The Carroll Company Application (cont'd.) Figure 4-29: Sample run of the Carroll Company application using valid data Figure 4-30: Sample run of the Carroll Company application using invalid data Microsoft Visual Basic 2010: Reloaded, Fourth Edition 53 Summary of Operators Figure 4-31: Listing of arithmetic, concatenation, comparison, and logical operators Microsoft Visual Basic 2010: Reloaded, Fourth Edition 54 Summary of Operators (cont’d.) Figure 4-31: Listing of arithmetic, concatenation, comparison, and logical operators (cont’d.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition 55 Generating Random Integers • Pseudo-random number generator: a device that produces a sequence of numbers that meets certain statistical requirements for randomness • Random object: represents a pseudo-random number generator • Random.Next method: – Generates a random integer – Can specify a minimum and maximum value Microsoft Visual Basic 2010: Reloaded, Fourth Edition 56 Figure 4-32: How to generate random integers Microsoft Visual Basic 2010: Reloaded, Fourth Edition 57 Generating Random Integers (cont'd.) Figure 4-33: Sample run of the Random Integers application Microsoft Visual Basic 2010: Reloaded, Fourth Edition 58 Generating Random Integers (cont'd.) Figure 4-34: Generate button’s Click event procedure Microsoft Visual Basic 2010: Reloaded, Fourth Edition 59 Programming Tutorial 1 Figure 4-36: MainForm for the Find the Mouse Game application Microsoft Visual Basic 2010: Reloaded, Fourth Edition 60 Programming Tutorial 2 Figure 4-44: MainForm for the Greenview Health Club application Microsoft Visual Basic 2010: Reloaded, Fourth Edition 61 Programming Example Figure 4-50: MainForm in the Fat Calculator application Microsoft Visual Basic 2010: Reloaded, Fourth Edition 62 Summary • Selection structure allows a procedure to make a decision and then take the appropriate action • Three types of selection structures: singlealternative, dual-alternative, and multiplealternative • Diamond symbol represents a decision in a flowchart • Expressions with comparison operators will result in an answer of True or False • Comparison operators are evaluated from left to right in expressions, after arithmetic operators Microsoft Visual Basic 2010: Reloaded, Fourth Edition 63 Summary (cont'd.) • Variables declared within a selection expression have block-level scope • Concatenation: connecting or linking two strings together with the concatenation operator (&) • ControlChars.Newline advances the insertion point to the next line in a control • String comparisons are case-sensitive • Use ToUpper and ToLower methods to temporarily convert the case of a string Microsoft Visual Basic 2010: Reloaded, Fourth Edition 64 Summary (cont'd.) • Use check boxes to provide the user with one or more independent and nonexclusive choices • Use logical operators to create compound conditions • An expression containing a logical operator will evaluate to either True or False • Logical operators have an order of precedence and are evaluated after arithmetic and comparison operators • Use the pseudo-random number generator to generate random numbers Microsoft Visual Basic 2010: Reloaded, Fourth Edition 65