Download presentation
Presentation is loading. Please wait.
Published byTrevin Decker Modified over 9 years ago
1
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 4 Making Decisions in a Program
2
Microsoft Visual Basic 2005: Reloaded, Second Edition2 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
3
Microsoft Visual Basic 2005: Reloaded, Second Edition3 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
4
Microsoft Visual Basic 2005: Reloaded, Second Edition4 Objectives (continued) Code an If/ElseIf/Else selection structure Include a Case selection structure in pseudocode, a flowchart, and code Generate random numbers
5
Microsoft Visual Basic 2005: Reloaded, Second Edition5 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
6
Microsoft Visual Basic 2005: Reloaded, Second Edition6 The Selection Structure (continued)
7
Microsoft Visual Basic 2005: Reloaded, Second Edition7 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
8
Microsoft Visual Basic 2005: Reloaded, Second Edition8 Writing Pseudocode for the If and If/Else Selection Structures (continued)
9
Microsoft Visual Basic 2005: Reloaded, Second Edition9 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
10
Microsoft Visual Basic 2005: Reloaded, Second Edition10 Flowcharting the If and If/Else Selection Structures (continued)
11
Microsoft Visual Basic 2005: Reloaded, Second Edition11 Coding the If and If/Else Selection Structures
12
Microsoft Visual Basic 2005: Reloaded, Second Edition12 Coding the If and If/Else Selection Structures (continued)
13
Microsoft Visual Basic 2005: Reloaded, Second Edition13 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
14
Microsoft Visual Basic 2005: Reloaded, Second Edition14 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: <>
15
Microsoft Visual Basic 2005: Reloaded, Second Edition15 Comparison Operators (continued)
16
Microsoft Visual Basic 2005: Reloaded, Second Edition16 Comparison Operators (continued) Comparison operators: –Have no order of precedence –Are evaluated from left to right in an expression
17
Microsoft Visual Basic 2005: Reloaded, Second Edition17 Comparison Operators (continued)
18
Microsoft Visual Basic 2005: Reloaded, Second Edition18 Using Comparison Operators – Swapping Numeric Values Pseudocode for a procedure that displays highest and lowest of two numbers:
19
Microsoft Visual Basic 2005: Reloaded, Second Edition19 Using Comparison Operators – Swapping Numeric Values (continued)
20
Microsoft Visual Basic 2005: Reloaded, Second Edition20 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
21
Microsoft Visual Basic 2005: Reloaded, Second Edition21 Using Comparison Operators – Swapping Numeric Values (continued)
22
Microsoft Visual Basic 2005: Reloaded, Second Edition22 Using Comparison Operators – Swapping Numeric Values (continued)
23
Microsoft Visual Basic 2005: Reloaded, Second Edition23 Using Comparison Operators – Swapping Numeric Values (continued)
24
Microsoft Visual Basic 2005: Reloaded, Second Edition24 Using Comparison Operators – Swapping Numeric Values (continued)
25
Microsoft Visual Basic 2005: Reloaded, Second Edition25 Using Comparison Operators – Swapping Numeric Values (continued)
26
Microsoft Visual Basic 2005: Reloaded, Second Edition26 Using Comparison Operators – Example 2 Pseudocode for a procedure to allow the user to display the sum or difference of two numbers:
27
Microsoft Visual Basic 2005: Reloaded, Second Edition27 Using Comparison Operators – Example 2 (continued)
28
Microsoft Visual Basic 2005: Reloaded, Second Edition28 Using Comparison Operators – Example 2 (continued)
29
Microsoft Visual Basic 2005: Reloaded, Second Edition29 Using Comparison Operators – Example 2 (continued)
30
Microsoft Visual Basic 2005: Reloaded, Second Edition30 Using Comparison Operators – Example 2 (continued)
31
Microsoft Visual Basic 2005: Reloaded, Second Edition31 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
32
Microsoft Visual Basic 2005: Reloaded, Second Edition32 Using the ToUpper and ToLower Methods (continued)
33
Microsoft Visual Basic 2005: Reloaded, Second Edition33 Using the ToUpper and ToLower Methods (continued)
34
Microsoft Visual Basic 2005: Reloaded, Second Edition34 Using the ToUpper and ToLower Methods (continued)
35
Microsoft Visual Basic 2005: Reloaded, Second Edition35 Logical Operators Logical operators (or Boolean operators): –Used to combine one or more conditions Compound condition: a combination of conditions using logical operator(s)
36
Microsoft Visual Basic 2005: Reloaded, Second Edition36 Logical Operators (continued)
37
Microsoft Visual Basic 2005: Reloaded, Second Edition37 Logical Operators (continued)
38
Microsoft Visual Basic 2005: Reloaded, Second Edition38 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
39
Microsoft Visual Basic 2005: Reloaded, Second Edition39 Logical Operators (continued)
40
Microsoft Visual Basic 2005: Reloaded, Second Edition40 Logical Operators (continued)
41
Microsoft Visual Basic 2005: Reloaded, Second Edition41 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
42
Microsoft Visual Basic 2005: Reloaded, Second Edition42 Using the Truth Tables (continued)
43
Microsoft Visual Basic 2005: Reloaded, Second Edition43 Using the Truth Tables (continued)
44
Microsoft Visual Basic 2005: Reloaded, Second Edition44 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
45
Microsoft Visual Basic 2005: Reloaded, Second Edition45 Using Logical Operators in an If…Then…Else Statement (continued)
46
Microsoft Visual Basic 2005: Reloaded, Second Edition46 Using Logical Operators in an If…Then…Else Statement (continued)
47
Microsoft Visual Basic 2005: Reloaded, Second Edition47 Using Logical Operators in an If…Then…Else Statement (continued)
48
Microsoft Visual Basic 2005: Reloaded, Second Edition48 The String.IsNullOrEmpty Method String.IsNullOrEmpty method: determine if a control’s Text property or String variable contains data
49
Microsoft Visual Basic 2005: Reloaded, Second Edition49 The String.IsNullOrEmpty Method (continued)
50
Microsoft Visual Basic 2005: Reloaded, Second Edition50 Modifying the Skate-Away Sales Application
51
Microsoft Visual Basic 2005: Reloaded, Second Edition51 Modifying the Skate-Away Sales Application (continued)
52
Microsoft Visual Basic 2005: Reloaded, Second Edition52 Modifying the Skate-Away Sales Application (continued)
53
Microsoft Visual Basic 2005: Reloaded, Second Edition53 Modifying the Skate-Away Sales Application (continued)
54
Microsoft Visual Basic 2005: Reloaded, Second Edition54 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
55
Microsoft Visual Basic 2005: Reloaded, Second Edition55 The MessageBox.Show Method (continued)
56
Microsoft Visual Basic 2005: Reloaded, Second Edition56 The MessageBox.Show Method (continued)
57
Microsoft Visual Basic 2005: Reloaded, Second Edition57 The MessageBox.Show Method (continued)
58
Microsoft Visual Basic 2005: Reloaded, Second Edition58 The MessageBox.Show Method (continued)
59
Microsoft Visual Basic 2005: Reloaded, Second Edition59 The MessageBox.Show Method (continued)
60
Microsoft Visual Basic 2005: Reloaded, Second Edition60 The MessageBox.Show Method (continued)
61
Microsoft Visual Basic 2005: Reloaded, Second Edition61 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
62
Microsoft Visual Basic 2005: Reloaded, Second Edition62 Nested Selection Structures (continued)
63
Microsoft Visual Basic 2005: Reloaded, Second Edition63 Nested Selection Structures (continued)
64
Microsoft Visual Basic 2005: Reloaded, Second Edition64 Nested Selection Structures (continued)
65
Microsoft Visual Basic 2005: Reloaded, Second Edition65 Nested Selection Structures (continued)
66
Microsoft Visual Basic 2005: Reloaded, Second Edition66 Nested Selection Structures (continued)
67
Microsoft Visual Basic 2005: Reloaded, Second Edition67 Nested Selection Structures (continued)
68
Microsoft Visual Basic 2005: Reloaded, Second Edition68 Nested Selection Structures (continued)
69
Microsoft Visual Basic 2005: Reloaded, Second Edition69 The If/ElseIf/Else Selection Structure Need a procedure to display a message based on a letter grade: Letter gradeMessage AExcellent BAbove Average CAverage DBelow Average F
70
Microsoft Visual Basic 2005: Reloaded, Second Edition70 The If/ElseIf/Else Selection Structure (continued)
71
Microsoft Visual Basic 2005: Reloaded, Second Edition71 The If/ElseIf/Else Selection Structure (continued)
72
Microsoft Visual Basic 2005: Reloaded, Second Edition72 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
73
Microsoft Visual Basic 2005: Reloaded, Second Edition73 The Case Selection Structure (continued)
74
Microsoft Visual Basic 2005: Reloaded, Second Edition74 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
75
Microsoft Visual Basic 2005: Reloaded, Second Edition75 The Case Selection Structure (continued)
76
Microsoft Visual Basic 2005: Reloaded, Second Edition76 The Case Selection Structure (continued)
77
Microsoft Visual Basic 2005: Reloaded, Second Edition77 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
78
Microsoft Visual Basic 2005: Reloaded, Second Edition78 Using To and Is in an Expression List (continued)
79
Microsoft Visual Basic 2005: Reloaded, Second Edition79 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
80
Microsoft Visual Basic 2005: Reloaded, Second Edition80 Generating Random Integers (continued)
81
Microsoft Visual Basic 2005: Reloaded, Second Edition81 Generating Random Integers (continued)
82
Microsoft Visual Basic 2005: Reloaded, Second Edition82 Generating Random Integers (continued)
83
Microsoft Visual Basic 2005: Reloaded, Second Edition83 Programming Tutorial
84
Microsoft Visual Basic 2005: Reloaded, Second Edition84 Programming Example
85
Microsoft Visual Basic 2005: Reloaded, Second Edition85 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
86
Microsoft Visual Basic 2005: Reloaded, Second Edition86 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
87
Microsoft Visual Basic 2005: Reloaded, Second Edition87 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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.