Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 4 Making Decisions in a Program

Preview:

Citation preview

Microsoft Visual Basic 2005: Reloaded

Second Edition

Chapter 4Making Decisions in a Program

Microsoft Visual Basic 2005: Reloaded, Second Edition 2

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 3

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 4

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 5

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 6

The Selection Structure (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition 7

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 8

Writing Pseudocode for the If and If/Else Selection Structures

(continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition 9

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 10

Flowcharting the If and If/Else Selection Structures (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition 11

Coding the If and If/Else Selection Structures

Microsoft Visual Basic 2005: Reloaded, Second Edition 12

Coding the If and If/Else Selection Structures (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition 13

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 14

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 15

Comparison Operators (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition 16

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 17

Comparison Operators (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition 18

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 19

Using Comparison Operators – Swapping Numeric Values (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition 20

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 21

Using Comparison Operators – Swapping Numeric Values (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition 22

Using Comparison Operators – Swapping Numeric Values (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition 23

Using Comparison Operators – Swapping Numeric Values (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition 24

Using Comparison Operators – Swapping Numeric Values (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition 25

Using Comparison Operators – Swapping Numeric Values (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition 26

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 27

Using Comparison Operators – Example 2 (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition 28

Using Comparison Operators – Example 2 (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition 29

Using Comparison Operators – Example 2 (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition 30

Using Comparison Operators – Example 2 (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition 31

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 32

Using the ToUpper and ToLower Methods (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition 33

Using the ToUpper and ToLower Methods (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition 34

Using the ToUpper and ToLower Methods (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition 35

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 36

Logical Operators (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition 37

Logical Operators (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition 38

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 39

Logical Operators (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition 40

Logical Operators (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition 41

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 42

Using the Truth Tables (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition 43

Using the Truth Tables (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition 44

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 45

Using Logical Operators in an If…Then…Else Statement (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition 46

Using Logical Operators in an If…Then…Else Statement (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition 47

Using Logical Operators in an If…Then…Else Statement (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition 48

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 49

The String.IsNullOrEmpty Method (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition 50

Modifying the Skate-Away Sales Application

Microsoft Visual Basic 2005: Reloaded, Second Edition 51

Modifying the Skate-Away Sales Application (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition 52

Modifying the Skate-Away Sales Application (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition 53

Modifying the Skate-Away Sales Application (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition 54

The MessageBox.Show Method

• MessageBox.Show method: – Display message box with text, buttons and an icon

• When a message box is displayed, the program waits until the user selects a button

• MessageBox.Show returns an integer value indicating which button the user selected

Microsoft Visual Basic 2005: Reloaded, Second Edition 55

The MessageBox.Show Method (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition 56

The MessageBox.Show Method (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition 57

The MessageBox.Show Method (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition 58

The MessageBox.Show Method (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition 59

The MessageBox.Show Method (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition 60

The MessageBox.Show Method (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition 61

Nested Selection Structures

• Nested selection structure: a selection structure that is completely contained within another selection structure

• Primary decision: decision made by the outer selection structure

• Secondary decision: decision made by the inner selection structure

Microsoft Visual Basic 2005: Reloaded, Second Edition 62

Nested Selection Structures (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition 63

Nested Selection Structures (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition 64

Nested Selection Structures (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition 65

Nested Selection Structures (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition 66

Nested Selection Structures (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition 67

Nested Selection Structures (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition 68

Nested Selection Structures (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition 69

The If/ElseIf/Else Selection Structure

• Need a procedure to display a message based on a letter grade:

Letter grade Message

A Excellent

B Above Average

C Average

D Below Average

F Below Average

Microsoft Visual Basic 2005: Reloaded, Second Edition 70

The If/ElseIf/Else Selection Structure (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition 71

The If/ElseIf/Else Selection Structure (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition 72

The Case Selection Structure

• Case selection structure: – Used when there are many paths from which to

choose– Simpler and clearer than using If/ElseIf/Else

Microsoft Visual Basic 2005: Reloaded, Second Edition 73

The Case Selection Structure (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition 74

The Case Selection Structure (continued)

• Case selection structure in a flowchart:– Uses the diamond symbol– One flowline into the diamond, but many flowlines

out of the diamond

• Case selection structure evaluates an expression to determine which path to take

• Case selection structure:– Begins with Select Case– Ends with End Select– Has one Case clause for each possible value

Microsoft Visual Basic 2005: Reloaded, Second Edition 75

The Case Selection Structure (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition 76

The Case Selection Structure (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition 77

Using To and Is in an ExpressionList

• TO and IS keywords: specify a range of values in a Case clause’s expression list

• TO: – When you know both the upper and lower bounds of

the range

• IS: – When you know only one end of the range– Used with a comparison operator

Microsoft Visual Basic 2005: Reloaded, Second Edition 78

Using To and Is in an Expression List (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition 79

Generating Random Integers

• Pseudo-random number generator: produces a sequence of numbers that meets certain statistical requirements for randomness

• Random.Next method: – Generates a random integer– Can specify a minimum and maximum value

Microsoft Visual Basic 2005: Reloaded, Second Edition 80

Generating Random Integers (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition 81

Generating Random Integers (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition 82

Generating Random Integers (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition 83

Programming Tutorial

Microsoft Visual Basic 2005: Reloaded, Second Edition 84

Programming Example

Microsoft Visual Basic 2005: Reloaded, Second Edition 85

Summary

• Selection structure is used for decisions

• Four forms of selection structures: If, If/Else, If/ElseIf/Else, and Case

• Diamond symbol represents a decision in a flowchart

• Expressions with comparison operators will result in an answer of True or False

• Variables declared within a selection expression have block-level scope

Microsoft Visual Basic 2005: Reloaded, Second Edition 86

Summary (continued)

• Concatenation: linking two strings together

• Use logical operators to create compound conditions

• String.IsNullOrEmpty method will determine if a string contains data

• MessageBox.Show method returns an integer indicating which button was chosen

• Selection structures can be nested

Microsoft Visual Basic 2005: Reloaded, Second Edition 87

Summary (continued)

• Use If/ElseIf/Else or Case structures when there are several possible alternative outcomes

• Use TO keyword to specify a range of valid values when both the lower and upper bound are known

• Use IS keyword with a comparison operator to specify a lower or upper bound but not both

• Use the pseudo-random number generator to generate random numbers

Recommended