530 likes | 797 Views
Microsoft Visual Basic 2008: Reloaded, Third Edition. 2. Objectives. After studying this chapter, you should be able to:Include the selection structure in pseudocode and in a flowchartWrite an If
E N D
1. Microsoft Visual Basic 2008: Reloaded Third Edition Chapter Four
Making Decisions in a Program
2. Microsoft Visual Basic 2008: Reloaded, Third Edition 2 Objectives After studying this chapter, you should be able to:
Include the selection structure in pseudocode and in a flowchart
Write an IfThenElse statement
Write code that uses comparison operators and logical operators
Create a variable having block scope
Concatenate strings
3. Microsoft Visual Basic 2008: Reloaded, Third Edition 3 Objectives (continued) Use the ControlChars.NewLine constant
Change the case of a string
Generate random numbers
4. Microsoft Visual Basic 2008: Reloaded, Third 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
Four forms of selection structure:
If
If/Else
If/ElseIf/Else
Case
5. Microsoft Visual Basic 2008: Reloaded, Third Edition 5 The Selection Structure (continued)
6. Microsoft Visual Basic 2008: Reloaded, Third Edition 6 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
7. Microsoft Visual Basic 2008: Reloaded, Third Edition 7
8. Microsoft Visual Basic 2008: Reloaded, Third Edition 8 Flowcharting the If and If/Else Selection Structures Flowchart uses standardized symbols to show the steps a computer must take
Selection/repetition symbol:
Diamond shape
Represents both selection and repetition structures
One flowline entering and two flowlines leaving
9. Microsoft Visual Basic 2008: Reloaded, Third Edition 9
10. Microsoft Visual Basic 2008: Reloaded, Third Edition 10 Coding the If and If/Else Selection Structures IfThenElse statement: used to code the If and If/Else selection structures
Else clause: an optional part of the If statement
Condition must be a Boolean expression
Must evaluate to either True or False
Statement block: set of statements terminated by an Else or End If
IfThenElse statement can contain variables, literal constants, named constants, properties, methods, arithmetic operators, comparison operators, and logical operators
11. Microsoft Visual Basic 2008: Reloaded, Third Edition 11 Coding the If and If/Else Selection Structures (continued)
12. Microsoft Visual Basic 2008: Reloaded, Third Edition 12
13. 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 2008: Reloaded, Third Edition 13
14. Microsoft Visual Basic 2008: Reloaded, Third Edition 14
15. Comparison Operators (continued) 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 2008: Reloaded, Third Edition 15
16. Comparison Operators (continued) Microsoft Visual Basic 2008: Reloaded, Third Edition 16
17. Using Comparison Operators Swapping Numeric Values Pseudocode for a procedure that displays highest and lowest of two numbers
Microsoft Visual Basic 2008: Reloaded, Third Edition 17
18. Using Comparison Operators Swapping Numeric Values (continued) Microsoft Visual Basic 2008: Reloaded, Third Edition 18
19. Microsoft Visual Basic 2008: Reloaded, Third Edition 19
20. Microsoft Visual Basic 2008: Reloaded, Third Edition 20 Using Comparison Operators Swapping Numeric Values (continued)
21. Microsoft Visual Basic 2008: Reloaded, Third Edition 21 Using Comparison Operators Swapping Numeric Values (continued)
22. Microsoft Visual Basic 2008: Reloaded, Third Edition 22 Using Comparison Operators Swapping Numeric Values (continued)
23. Microsoft Visual Basic 2008: Reloaded, Third Edition 23 Using Comparison Operators Swapping Numeric Values (continued)
24. 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 2008: Reloaded, Third Edition 24
25. Using Comparison Operators Example 2 (continued) Microsoft Visual Basic 2008: Reloaded, Third Edition 25
26. Microsoft Visual Basic 2008: Reloaded, Third Edition 26
27. Microsoft Visual Basic 2008: Reloaded, Third Edition 27 Using Comparison Operators Example 2 (continued)
28. Using Comparison Operators Example 2 (continued) 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 upper- or lowercase
Microsoft Visual Basic 2008: Reloaded, Third Edition 28
29. Microsoft Visual Basic 2008: Reloaded, Third Edition 29 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 variables contents
30. Microsoft Visual Basic 2008: Reloaded, Third Edition 30
31. Microsoft Visual Basic 2008: Reloaded, Third Edition 31 Using the ToUpper and ToLower Methods (continued)
32. Microsoft Visual Basic 2008: Reloaded, Third Edition 32 Using the ToUpper and ToLower Methods (continued)
33. Using the ToUpper and ToLower Methods (continued) Microsoft Visual Basic 2008: Reloaded, Third Edition 33
34. Microsoft Visual Basic 2008: Reloaded, Third Edition 34 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)
35. Microsoft Visual Basic 2008: Reloaded, Third Edition 35
36. Microsoft Visual Basic 2008: Reloaded, Third Edition 36
37. Microsoft Visual Basic 2008: Reloaded, Third Edition 37 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 the second condition if the first condition is false
38. Microsoft Visual Basic 2008: Reloaded, Third Edition 38
39. Microsoft Visual Basic 2008: Reloaded, Third Edition 39 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
40. Microsoft Visual Basic 2008: Reloaded, Third Edition 40 Using the Truth Tables (continued)
41. Using the Truth Tables (continued) Microsoft Visual Basic 2008: Reloaded, Third Edition 41
42. Microsoft Visual Basic 2008: Reloaded, Third Edition 42 Using Logical Operators in an IfThenElse Statement Data validation:
Process of verifying that the input data is within the expected range
Use an IfThenElse statement to validate input data
43. Microsoft Visual Basic 2008: Reloaded, Third Edition 43
44. Using Logical Operators in an IfThenElse Statement (continued) Microsoft Visual Basic 2008: Reloaded, Third Edition 44
45. Microsoft Visual Basic 2008: Reloaded, Third Edition 45 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
46. Microsoft Visual Basic 2008: Reloaded, Third Edition 46 Generating Random Integers (continued)
47. Generating Random Integers (continued) Microsoft Visual Basic 2008: Reloaded, Third Edition 47
48. Generating Random Integers (continued) Microsoft Visual Basic 2008: Reloaded, Third Edition 48
49. Programming Tutorial Microsoft Visual Basic 2008: Reloaded, Third Edition 49
50. Programming Example Microsoft Visual Basic 2008: Reloaded, Third Edition 50
51. Microsoft Visual Basic 2008: Reloaded, Third Edition 51 Summary Selection structure allows a program to make a decision and then select one of two paths
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
Comparison operators are evaluated from left to right in expressions, after arithmetic operators
52. Microsoft Visual Basic 2008: Reloaded, Third Edition 52 Summary (continued) 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
53. Microsoft Visual Basic 2008: Reloaded, Third Edition 53 Summary (continued) 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