Microsoft Visual Basic 2008: Reloaded Third Edition - PowerPoint PPT Presentation

Loading...

PPT – Microsoft Visual Basic 2008: Reloaded Third Edition PowerPoint presentation | free to view - id: 250340-ZDc1Z



Loading


The Adobe Flash plugin is needed to view this content

Get the plugin now

View by Category
About This Presentation
Title:

Microsoft Visual Basic 2008: Reloaded Third Edition

Description:

A block-scope variable can only be used within the statement block in which it was declared ... Used to combine two or more conditions into one compound condition ... – PowerPoint PPT presentation

Number of Views:36
Avg rating:3.0/5.0
Slides: 54
Provided by: barabo
Category:

less

Write a Comment
User Comments (0)
Transcript and Presenter's Notes

Title: Microsoft Visual Basic 2008: Reloaded Third Edition


1
Microsoft Visual Basic 2008 Reloaded Third
Edition
  • Chapter Four
  • Making Decisions in a Program

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

3
Objectives (continued)
  • Use the ControlChars.NewLine constant
  • Change the case of a string
  • Generate random numbers

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
The Selection Structure (continued)
Figure 4-1 Selection structures you might use
today
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
Figure 4-2 Examples of the If and If/Else
selection structures written in pseudocode
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
Figure 4-3 Examples of the If and If/Else
selection structures in flowchart form
10
Coding the If and If/Else Selection Structures
  • If…Then…Else 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
  • If…Then…Else statement can contain variables,
    literal constants, named constants, properties,
    methods, arithmetic operators, comparison
    operators, and logical operators

11
Coding the If and If/Else Selection Structures
(continued)
Figure 4-4 How to use the If/Then/Else statement
to code the If/Then/Else selection structure
12
Figure 4-4 How to use the If/Then/Else statement
to code the If/Then/Else selection structure
(continued)
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 gt
  • Greater than or equal to gt
  • Less than lt
  • Less than or equal to lt
  • Not equal to ltgt

14
Figure 4-5 How to use the most commonly used
comparison operators
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

16
Comparison Operators (continued)
Figure 4-6 Evaluation steps for an expression
containing arithmetic and comparison operators
17
Using Comparison Operators Swapping Numeric
Values
  • Pseudocode for a procedure that displays highest
    and lowest of two numbers

Figure 4-7 Sample run of the Number Swap
application
18
Using Comparison Operators Swapping Numeric
Values (continued)
Figure 4-8 Pseudocode showing the If selection
structure
19
Figure 4-9 Flowchart showing the If selection
structure
20
Using Comparison Operators Swapping Numeric
Values (continued)
Figure 4-10 The If selection structure shown in
the displayButtons Click event procedure
21
Using Comparison Operators Swapping Numeric
Values (continued)
  • Block scope the scope of a variable created
    within a statement block
  • A block-scope variable can only be used within
    the statement block in which it was declared
  • Concatenation operator () connects or links two
    strings together
  • ControlChars.NewLine constant
  • Advances the insertion point to the next line

22
Using Comparison Operators Swapping Numeric
Values (continued)
Figure 4-11 Illustration of the swapping concept
23
Using Comparison Operators Swapping Numeric
Values (continued)
Figure 4-12 How to concatenate strings
24
Using Comparison Operators Example 2
  • Pseudocode for a procedure to allow the user to
    display the sum or difference of two numbers

Figure 4-13 Sample run of the Addition and
Subtraction Calculator application
25
Using Comparison Operators Example 2 (continued)
Figure 4-14 Pseudocode showing the If/Else
selection structure
26
Figure 4-15 Flowchart showing the If/Else
selection structure
27
Using Comparison Operators Example 2 (continued)
Figure 4-16 The If/Else selection structure
shown in the calcButtons Click event procedure
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

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
Figure 4-17a How to use the ToUpper and ToLower
methods
31
Using the ToUpper and ToLower Methods (continued)
Figure 4-17b How to use the ToUpper and ToLower
methods (continued)
32
Using the ToUpper and ToLower Methods (continued)
Figure 4-18 Code showing the ToUpper method in
the assignment statement
33
Using the ToUpper and ToLower Methods (continued)
Figure 4-19 Code showing the ToUpper method in
the If…Then…Else statements condition
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
Logical Operators (continued)
Figure 4-20 How to use the logical operators
36
Figure 4-20 How to use the logical operators
(continued)
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
Logical Operators (continued)
Figure 4-21 Truth tables
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
Using the Truth Tables (continued)
Figure 4-22 Order of precedence for arithmetic,
comparison, and logical operators
41
Using the Truth Tables (continued)
Figure 4-23 Evaluation steps for an expression
containing arithmetic, comparison, and logical
operators
42
Using Logical Operators in an If…Then…Else
Statement
  • Data validation
  • Process of verifying that the input data is
    within the expected range
  • Use an If…Then…Else statement to validate input
    data

Figure 4-24 AndAlso and OrElse logical operators
in the If…Then…Else statement
43
Figure 4-24 AndAlso and OrElse logical operators
in the If…Then…Else statement (continued)
44
Using Logical Operators in an If…Then…Else
Statement (continued)
Figure 4-25 Sample run of the application that
contains the calcButtons Click event procedure
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
Generating Random Integers (continued)
Figure 4-26 How to generate random numbers
47
Generating Random Integers (continued)
Figure 4-27 Sample run of the Random Integers
application
48
Generating Random Integers (continued)
Figure 4-28 The generateButtons Click event
procedure
49
Programming Tutorial
Figure 4-30 User interface
50
Programming Example
Figure 4-38 User interface
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
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
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
About PowerShow.com