870 likes | 885 Vues
Microsoft Visual Basic 2005: Reloaded Second Edition. Chapter 4 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 Write an If…Then…Else statement
E N D
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 4 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 • Write an If…Then…Else statement • Write code that uses comparison operators and logical operators • Create a variable having block-scope • Concatenate strings Microsoft Visual Basic 2005: Reloaded, Second Edition
Objectives (continued) • Use the ControlChars.NewLine constant • Change the case of a string • Determine whether a string contains data • Display a message in a message box • Include a nested selection structure in pseudocode, a flowchart, and code Microsoft Visual Basic 2005: Reloaded, Second Edition
Objectives (continued) • Code an If/ElseIf/Else selection structure • Include a Case selection structure in pseudocode, a flowchart, and code • Generate random numbers Microsoft Visual Basic 2005: Reloaded, Second 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 • Four forms of selection structure: • If • If/Else • If/ElseIf/Else • Case Microsoft Visual Basic 2005: Reloaded, Second Edition
The Selection Structure (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition
Writing Pseudocode for the If and If/Else Selection Structures • If selection structure: contains one set of instructions to process when the condition is true • If/Else selection structure: • Contains two sets of instructions • One set is processed when the condition is true • The other set is processed when the condition is false • True path: path to follow when condition is true • False path: path to follow when condition is false Microsoft Visual Basic 2005: Reloaded, Second Edition
Writing Pseudocode for the If and If/Else Selection Structures (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition
Flowcharting the If and If/Else Selection Structures • Selection/repetition symbol: • Diamond shape • Represents both selection and repetition structures • One flowline entering and two flowlines leaving Microsoft Visual Basic 2005: Reloaded, Second Edition
Flowcharting the If and If/Else Selection Structures (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition
Coding the If and If/Else Selection Structures Microsoft Visual Basic 2005: Reloaded, Second Edition
Coding the If and If/Else Selection Structures (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition
Coding the If and If/Else Selection Structures (continued) • Else clause: an optional part of the If statement • Statement block: set of statements terminated by an Else or End If Microsoft Visual Basic 2005: Reloaded, Second Edition
Comparison Operators • Comparison operators (or relational operators): • Used as part of the condition in an If statement • 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 2005: Reloaded, Second Edition
Comparison Operators (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition
Comparison Operators (continued) • Comparison operators: • Have no order of precedence • Are evaluated from left to right in an expression Microsoft Visual Basic 2005: Reloaded, Second Edition
Comparison Operators (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition
Using Comparison Operators – Swapping Numeric Values • Pseudocode for a procedure that displays highest and lowest of two numbers: Microsoft Visual Basic 2005: Reloaded, Second Edition
Using Comparison Operators – Swapping Numeric Values (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition
Using Comparison Operators – Swapping Numeric Values (continued) • Block scope: the scope of a variable created within a block • Block-scope variable: can only be used within the statement block in which it was declared • Concatenation operator (&): links two strings • ControlChars.NewLine constant: • Advances the insertion point to the next line Microsoft Visual Basic 2005: Reloaded, Second Edition
Using Comparison Operators – Swapping Numeric Values (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition
Using Comparison Operators – Swapping Numeric Values (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition
Using Comparison Operators – Swapping Numeric Values (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition
Using Comparison Operators – Swapping Numeric Values (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition
Using Comparison Operators – Swapping Numeric Values (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition
Using Comparison Operators – Example 2 • Pseudocode for a procedure to allow the user to display the sum or difference of two numbers: Microsoft Visual Basic 2005: Reloaded, Second Edition
Using Comparison Operators – Example 2 (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition
Using Comparison Operators – Example 2 (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition
Using Comparison Operators – Example 2 (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition
Using Comparison Operators – Example 2 (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition
Using 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 2005: Reloaded, Second Edition
Using the ToUpper and ToLower Methods (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition
Using the ToUpper and ToLower Methods (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition
Using the ToUpper and ToLower Methods (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition
Logical Operators • Logical operators (or Boolean operators): • Used to combine one or more conditions • Compound condition: a combination of conditions using logical operator(s) Microsoft Visual Basic 2005: Reloaded, Second Edition
Logical Operators (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition
Logical Operators (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition
Logical Operators (continued) • Truth tables: used to evaluate logical operators in an expression • Short-circuit evaluation: an evaluation in which the second condition may not be evaluated • And and Or operations always evaluate both conditions • AndAlso and OrElse operations do not evaluate second condition if the first condition is false Microsoft Visual Basic 2005: Reloaded, Second Edition
Logical Operators (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition
Logical Operators (continued) Microsoft Visual Basic 2005: Reloaded, Second 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 • Use XOr when exactly one condition must be true to give a true result • Logical operators are evaluated after arithmetic or comparison operators in an expression Microsoft Visual Basic 2005: Reloaded, Second Edition
Using the Truth Tables (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition
Using the Truth Tables (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition
Using Logical Operators in an If…Then…Else Statement • Data validation: • Verifying that the input data is within the expected range • Use an If…Then…Else statement to validate input data Microsoft Visual Basic 2005: Reloaded, Second Edition
Using Logical Operators in an If…Then…Else Statement (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition
Using Logical Operators in an If…Then…Else Statement (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition
Using Logical Operators in an If…Then…Else Statement (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition
The String.IsNullOrEmpty Method • String.IsNullOrEmpty method: determine if a control’s Text property or String variable contains data Microsoft Visual Basic 2005: Reloaded, Second Edition
The String.IsNullOrEmpty Method (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition
Modifying the Skate-Away Sales Application Microsoft Visual Basic 2005: Reloaded, Second Edition