650 likes | 668 Views
Learn how to include selection structures, code selection structures using If...Then...Else statement, concatenate strings, generate random numbers, and more in Visual Basic 2010.
E N D
Microsoft Visual Basic 2008: ReloadedFourth 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
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
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
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
The Selection Structure (cont'd.) Figure 4-1: Selection structures you might use today Microsoft Visual Basic 2010: Reloaded, Fourth Edition
The Selection Structure (cont'd.) Figure 4-1: Selection structures you might use today (cont’d.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition
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
The Selection Structure (cont'd.) Figure 4-4: Pseudocode containing only the sequence structure Microsoft Visual Basic 2010: Reloaded, Fourth Edition
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
Figure 4-6: Single-alternative selection structure shown in a flowchart Microsoft Visual Basic 2010: Reloaded, Fourth Edition
Figure 4-7: Modified problem specification and pseudocode containing a dual-alternative selection structure Microsoft Visual Basic 2010: Reloaded, Fourth Edition
Figure 4-8: Dual-alternative selection structure shown in a flowchart Microsoft Visual Basic 2010: Reloaded, Fourth Edition
Coding Single-Alternative and Dual-Alternative Selection Structures • If…Then…Else statement: used to code single-alternative 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
Figure 4-9: How to use the If…Then…Else statement Microsoft Visual Basic 2010: Reloaded, Fourth Edition
Figure 4-9: How to use the If…Then…Else statement (cont’d.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition
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
Figure 4-10: How to use comparison operators in a condition Microsoft Visual Basic 2010: Reloaded, Fourth Edition
Comparison Operators (cont’d.) Figure 4-10: How to use comparison operators in a condition (cont’d.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition
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
Comparison Operators (cont'd.) Figure 4-11: Evaluation steps for an expression containing arithmetic and comparison operators Microsoft Visual Basic 2010: Reloaded, Fourth Edition
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
Comparing Numeric Values (cont'd.) Figure 4-13: Pseudocode containing a single-alternative selection structure Microsoft Visual Basic 2010: Reloaded, Fourth Edition
Figure 4-14: Flowchart containing a single-alternative selection structure Microsoft Visual Basic 2010: Reloaded, Fourth Edition
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
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
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
Comparing Numeric Values (cont'd.) Figure 4-16: Illustration of the swapping concept Microsoft Visual Basic 2010: Reloaded, Fourth Edition
Comparing Numeric Values (cont'd.) Figure 4-17: How to concatenate strings Microsoft Visual Basic 2010: Reloaded, Fourth Edition
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
Comparing Strings (cont'd.) Figure 4-19: Pseudocode containing a dual-alternative selection structure Microsoft Visual Basic 2010: Reloaded, Fourth Edition
Figure 4-20: Flowchart containing a dual-alternative selection structure Microsoft Visual Basic 2010: Reloaded, Fourth Edition
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
Figure 4-21: Calculate button’s Click event procedure Microsoft Visual Basic 2010: Reloaded, Fourth Edition
The ToUpper and ToLower Methods • String comparisons in Visual Basic are case-sensitive • 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
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
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
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
Figure 4-23: Examples of using the ToUpper method in the calcButton Click event procedure Microsoft Visual Basic 2010: Reloaded, Fourth Edition
Figure 4-23: Examples of using the ToUpper method in the calcButton Click event procedure (cont’d.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition
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
Comparing Boolean Values (cont’d.) Figure 4-25: Click event procedures for the subtractionCheckBox and calcButton Microsoft Visual Basic 2010: Reloaded, Fourth Edition
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
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
Logical Operators (cont'd.) Figure 4-26: How to use logical operators in a condition Microsoft Visual Basic 2010: Reloaded, Fourth Edition
Logical Operators (cont'd.) Figure 4-26: How to use logical operators in a condition (cont'd.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition
Figure 4-26: How to use logical operators in a condition (cont'd.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition
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 sub-conditions are True • OrElse evaluates to False only when both sub-conditions 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
Logical Operators (cont'd.) Figure 4-27: Truth tables for the AndAlso and OrElse logical operators Microsoft Visual Basic 2010: Reloaded, Fourth Edition
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