640 likes | 1.06k Views
Microsoft Visual Basic 2010: Reloaded, Fourth Edition. 2. Objectives. After studying this chapter, you should be able to:Include the selection structure in pseudocode and in a flowchartExplain the difference between single-alternative and dual-alternative selection structuresCode a selection stru
E N D
1. Microsoft Visual Basic 2008: Reloaded Fourth Edition Chapter Four
Making Decisions in a Program
2. Microsoft Visual Basic 2010: Reloaded, Fourth Edition 2 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
3. 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
4. Microsoft Visual Basic 2010: Reloaded, Fourth Edition 4 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
5. Microsoft Visual Basic 2010: Reloaded, Fourth Edition 5 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
6. Microsoft Visual Basic 2010: Reloaded, Fourth Edition 6 The Selection Structure (cont'd.)
7. Microsoft Visual Basic 2010: Reloaded, Fourth Edition 7 The Selection Structure (cont'd.)
8. Microsoft Visual Basic 2010: Reloaded, Fourth Edition 8 The Selection Structure (cont'd.)
9. Microsoft Visual Basic 2010: Reloaded, Fourth Edition 9 The Selection Structure (cont'd.)
10. Microsoft Visual Basic 2010: Reloaded, Fourth Edition 10 The Selection Structure (cont'd.)
11. Microsoft Visual Basic 2010: Reloaded, Fourth Edition 11
12. Microsoft Visual Basic 2010: Reloaded, Fourth Edition 12
13. Microsoft Visual Basic 2010: Reloaded, Fourth Edition 13
14. Microsoft Visual Basic 2010: Reloaded, Fourth Edition 14 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
15. Microsoft Visual Basic 2010: Reloaded, Fourth Edition 15
16. Microsoft Visual Basic 2010: Reloaded, Fourth Edition 16
17. 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
18. Microsoft Visual Basic 2010: Reloaded, Fourth Edition 18
19. Microsoft Visual Basic 2010: Reloaded, Fourth Edition 19 Comparison Operators (cont’d.)
20. 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
21. Comparison Operators (cont'd.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition 21
22. Comparing Numeric Values Auction House application displays highest and lowest of two bids entered by the user
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 22
23. Comparing Numeric Values (cont'd.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition 23
24. Microsoft Visual Basic 2010: Reloaded, Fourth Edition 24
25. Microsoft Visual Basic 2010: Reloaded, Fourth Edition 25 Comparing Numeric Values (cont'd.)
26. Microsoft Visual Basic 2010: Reloaded, Fourth Edition 26 Comparing Numeric Values (cont'd.)
27. Microsoft Visual Basic 2010: Reloaded, Fourth Edition 27 Comparing Numeric Values (cont'd.)
28. Microsoft Visual Basic 2010: Reloaded, Fourth Edition 28 Comparing Numeric Values (cont'd.)
29. Microsoft Visual Basic 2010: Reloaded, Fourth Edition 29 Comparing Numeric Values (cont'd.)
30. Comparing Strings Addition and Subtraction Calculator application: displays the sum or difference of two numbers
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 30
31. Comparing Strings (cont'd.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition 31
32. Microsoft Visual Basic 2010: Reloaded, Fourth Edition 32
33. 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
34. Microsoft Visual Basic 2010: Reloaded, Fourth Edition 34
35. Microsoft Visual Basic 2010: Reloaded, Fourth Edition 35 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
36. Microsoft Visual Basic 2010: Reloaded, Fourth Edition 36 The ToUpper and ToLower Methods (cont’d.)
37. Microsoft Visual Basic 2010: Reloaded, Fourth Edition 37 The ToUpper and ToLower Methods (cont’d.)
38. Microsoft Visual Basic 2010: Reloaded, Fourth Edition 38 The ToUpper and ToLower Methods (cont’d.)
39. Microsoft Visual Basic 2010: Reloaded, Fourth Edition 39
40. Microsoft Visual Basic 2010: Reloaded, Fourth Edition 40
41. Comparing Boolean Values Check boxes: used to offer the user one or more independent and nonexclusive items from which to choose Microsoft Visual Basic 2010: Reloaded, Fourth Edition 41
42. Comparing Boolean Values (cont’d.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition 42
43. Comparing Boolean Values (cont’d.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition 43
44. Microsoft Visual Basic 2010: Reloaded, Fourth Edition 44 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)
45. Microsoft Visual Basic 2010: Reloaded, Fourth Edition 45
46. Microsoft Visual Basic 2010: Reloaded, Fourth Edition 46
47. Microsoft Visual Basic 2010: Reloaded, Fourth Edition 47
48. Microsoft Visual Basic 2010: Reloaded, Fourth Edition 48 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
49. Microsoft Visual Basic 2010: Reloaded, Fourth Edition 49
50. Microsoft Visual Basic 2010: Reloaded, Fourth Edition 50 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
51. Microsoft Visual Basic 2010: Reloaded, Fourth Edition 51 The Carroll Company Application Data validation:
Process of verifying that the input data is within the expected range
52. Microsoft Visual Basic 2010: Reloaded, Fourth Edition 52
53. The Carroll Company Application (cont'd.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition 53
54. Summary of Operators Microsoft Visual Basic 2010: Reloaded, Fourth Edition 54
55. Summary of Operators (cont’d.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition 55
56. Microsoft Visual Basic 2010: Reloaded, Fourth Edition 56 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
57. Microsoft Visual Basic 2010: Reloaded, Fourth Edition 57
58. Generating Random Integers (cont'd.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition 58
59. Generating Random Integers (cont'd.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition 59
60. Programming Tutorial 1 Microsoft Visual Basic 2010: Reloaded, Fourth Edition 60
61. Programming Tutorial 2 Microsoft Visual Basic 2010: Reloaded, Fourth Edition 61
62. Programming Example Microsoft Visual Basic 2010: Reloaded, Fourth Edition 62
63. Microsoft Visual Basic 2010: Reloaded, Fourth Edition 63 Summary Selection structure allows a procedure to make a decision and then take the appropriate action
Three types of selection structures: single-alternative, dual-alternative, and multiple-alternative
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
64. Microsoft Visual Basic 2010: Reloaded, Fourth Edition 64 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
65. Microsoft Visual Basic 2010: Reloaded, Fourth Edition 65 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