Chapter 4: The Selection Structure - PowerPoint PPT Presentation

1 / 50
About This Presentation
Title:

Chapter 4: The Selection Structure

Description:

... an application that he can use to calculate the monthly payment on a car loan ... Figure 4-31: Sketch of the Monthly Payment Calculator user interface ... – PowerPoint PPT presentation

Number of Views:25
Avg rating:3.0/5.0
Slides: 51
Provided by: david2323
Category:

less

Transcript and Presenter's Notes

Title: Chapter 4: The Selection Structure


1
Chapter 4 The Selection Structure
Programming with Microsoft Visual Basic .NET,
Second Edition
2
The IfThenElse Statement Lesson A Objectives
  • Write pseudocode for the selection structure
  • Create a flowchart to help you plan an
    applications code
  • Write an If...Then...Else statement

3
The IfThenElse Statement Lesson A Objectives
(continued)
  • Write code that uses comparison operators and
    logical operators
  • Format numbers using the ToString method
  • Change the case of a string

4
The Selection Structure
  • Use the selection structure to make a decision or
    comparison and select a particular set of tasks
    to perform
  • The selection structure is also called the
    decision structure
  • The condition must result in either a true (yes)
    or false (no) answer

5
The Selection Structure (continued)
  • If the condition is true, the program performs
    one set of tasks
  • If the condition is false, there may or may not
    be a different set of tasks to perform
  • Visual Basic .NET provides four forms of the
    selection structure If, If/Else, If/ElseIf/Else,
    and Case

6
Writing Pseudocode for If and If/Else Selection
Structures
  • An If selection structure contains only one set
    of instructions, which are processed when the
    condition is true
  • An 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

7
Flowcharting the If and If/Else Selection
Structures
8
Flowcharting the If and If/Else Selection
Structures (continued)
T
F
T
F
9
Coding the If and If/Else Selection Structures
  • If condition Then
  • statement block containing one or more
    statements to be processed when the condition is
    true
  • Else
  • statement block containing one or more
    statements to be processed when the condition is
    false
  • End If

10
Coding the If and If/Else Selection Structures
(continued)
  • The items in square brackets ( ) in the syntax
    are optional
  • You do not need to include the Else portion
  • Words in bold are essential components of the
    statement

11
Coding the If and If/Else Selection Structures
(continued)
  • Items in italic indicate where the programmer
    must supply information pertaining to the current
    application
  • The set of statements contained in the true path,
    as well as the statements in the false path, are
    referred to as a statement block

12
Comparison Operators
13
Comparison Operators (continued)
  • Comparison operators are also referred to as
    relational operators
  • All expressions containing a relational operator
    will result in either a true or false answer only
  • Comparison operators are evaluated from left to
    right, and are evaluated after any mathematical
    operators

14
Comparison Operators (continued)
  • 10 3 lt 5 2
  • 5 2 is evaluated first, giving 10
  • 10 3 is evaluated second, giving 13
  • 13 lt 10 is evaluated last, giving false
  • 7 gt 3 4 / 2
  • 3 4 is evaluated first, giving 12
  • 12 / 2 is evaluated second, giving 6
  • 7 gt 6 is evaluated last, giving true

15
Comparison Operators (continued)
16
Logical Operators
17
Logical Operators (continued)
  • Truth table for Not operator

18
Logical Operators (continued)
  • Truth table for And operator

19
Logical Operators (continued)
  • Truth table for AndAlso operator

20
Logical Operators (continued)
  • Truth table for Or operator

21
Logical Operators (continued)
  • Truth table for OrElse operator

22
Logical Operators (continued)
  • Truth table for Xor operator

23
Logical Operators (continued)
Figure 4-19 Order of precedence for arithmetic,
comparison, and logical operators
24
Using the ToString Method to Format Numbers
  • Use the ToString method to format a number
  • Syntax variablename.ToString(formatString)
  • variablename is the name of a numeric variable

25
Using the ToString Method to Format Numbers
(continued)
  • formatString is a string that specifies the
    format
  • Must be enclosed in double quotation marks
  • Takes the form Axx
  • A is an alphabetic character called the format
    specifier
  • xx is a sequence of digits called the precision
    specifier

26
Comparing Strings
  • Example 1 Using the OrElse operator
  • Dim letter As String
  • letter Me.uiLetterTextBox.Text
  • If letter P OrElse letter p Then
  • Me.uiResultLabel.Text Pass
  • Else
  • Me.uiResultLabel.Text Fail
  • End if

27
Comparing Strings (continued)
  • Example 2 Using the AndAlso operator
  • Dim letter As String
  • letter Me.uiLetterTextBox.Text
  • If letter ltgt P AndAlso letter ltgt p Then
  • Me.uiResultLabel.Text Fail
  • Else
  • Me.uiResultLabel.Text Pass
  • End if

28
Comparing Strings (continued)
  • Example 3 Correct, but less efficient, solution
  • Dim letter As String
  • letter Me.uiLetterTextBox.Text
  • If letter P OrElse letter p Then
  • Me.uiResultLabel.Text Pass
  • End If
  • If letter ltgt P AndAlso letter ltgt p Then
  • Me.uiResultLabel.Text Fail
  • End if

29
Comparing Strings (continued)
  • Example 4 Using the ToUpper method
  • Dim letter As String
  • letter Me.uiLetterTextBox.Text
  • If letter.ToUpper() P Then
  • Me.uiResultLabel.Text Pass
  • Else
  • Me.uiResultLabel.Text Fail
  • End if

30
The Monthly Payment Calculator ApplicationLesson
B Objectives
  • Group objects using a GroupBox control
  • Calculate a periodic payment using the
    Financial.Pmt method
  • Create a message box using the MessageBox.Show
    method
  • Determine the value returned by a message box

31
Completing the User Interface
  • Herman Juarez has asked you to create an
    application that he can use to calculate the
    monthly payment on a car loan
  • To make this calculation, the application needs
  • The loan amount (principal)
  • The annual percentage rate (APR) of interest
  • The life of the loan (term) in years

32
Completing the User Interface (continued)
Figure 4-31 Sketch of the Monthly Payment
Calculator user interface
33
Adding a Group Box Control to the Form
  • Use the GroupBox tool in the Toolbox window to
    add a group box control to the interface
  • A group box control serves as a container for
    other controls
  • Use a group box control to visually separate
    related controls from other controls on the form

34
Coding the uiCalcPayButton Click Event Procedure
  • The uiCalcPayButtons Click event procedure is
    responsible for
  • Calculating the monthly payment amount
  • Displaying the result in the uiPaymentLabel
    control
  • Figure 4-37 shows the pseudocode for the
    uiCalcPayButtons Click event procedure

35
Coding the uiCalcPayButton Click Event Procedure
(continued)
Figure 4-37 Pseudocode for the uiCalcPayButton
Click event procedure
36
Using the Financial.Pmt Method
  • Use the Visual Basic .NET Financial.Pmt method to
    calculate a periodic payment on either a loan or
    an investment
  • Syntax Financial.Pmt(Rate, NPer, PV, FV, Due)
  • Rate interest rate per period
  • NPer total number of payment periods (the term)

37
Using the Financial.Pmt Method (continued)
  • PV present value of the loan or investment the
    present value of a loan is the loan amount,
    whereas the present value of an investment is
    zero
  • FV future value of the loan or investment the
    future value of a loan is zero, whereas the
    future value of an investment is the amount you
    want to accumulate if omitted, the number 0 is
    assumed

38
Using the Financial.Pmt Method (continued)
  • Due due date of payments can be either the
    constant DueDate.EndOfPeriod or the constant
    DueDate.BegOfPeriod if omitted,
    DueDate.EndOfPeriod is assumed

39
The MessageBox.Show Method
  • Use the MessageBox.Show method to display a
    message box that contains text, one or more
    buttons, and an icon
  • Syntax MessageBox.Show(text, caption, buttons,
    icon, defaultButton)
  • text text to display in the message box
  • caption text to display in the title bar of the
    message box

40
The MessageBox.Show Method (continued)
  • buttons buttons to display in the message box
  • icon icon to display in the message box
  • defaultButton button automatically selected when
    the user presses Enter

41
Coding the TextChanged Event
  • A controls TextChanged event occurs when the
    contents of a controls Text property have
    changed as a result of
  • The user entering data into the control, or
  • The applications code assigning data to the
    controls Text property

42
Coding the TextChanged Event (continued)
  • When the user makes a change to the information
    entered in the three text box controls, the
    Monthly Payment Calculator application should
    delete the monthly payment displayed in the
    uiPaymentLabel control

43
Completing the Monthly Payment Calculator
ApplicationLesson C Objectives
  • Specify the keys that a text box will accept
  • Align the text in a label control
  • Handle exceptions using a Try/Catch block

44
Coding the KeyPress Event
  • Template
  • Private Sub uiPrincipalTextBox_KeyPress( _
  • ByVal sender As Object, _
  • ByVal e As System.Windows.Forms.KeyPressEve
    ntArgs) _
  • Handles uiPrincipalTextBox.KeyPress
  • Setting e.Handled True will cancel the key

45
Aligning the Text in a Label Control
  • The TextAlign property controls the placement of
    the text in a label control
  • The TextAlign property can be set to TopLeft (the
    default), TopCenter, TopRight, MiddleLeft,
    MiddleCenter, MiddleRight, BottomLeft,
    BottomCenter, or BottomRight

46
Using a Try/Catch Block
  • An exception is an error that occurs while a
    program is running
  • Use the Try statement to catch (or trap) an
    exception when it occurs in a program
  • Use a Catch statement to take the appropriate
    action to resolve the problem
  • A block of code that uses both the Try and Catch
    statements is referred to as a Try/Catch block

47
Using a Try/Catch Block (continued)
  • Try
  • one or more statements that might generate an
    exception
  • Catch variablename As exceptionType
  • one or more statements that will execute when
    an exceptionType exception occurs
  • Catch variablename As exceptionType
  • one or more statements that will execute when
    an exceptionType exception occurs
  • End Try

48
Summary
  • To evaluate an expression containing arithmetic,
    comparison, and logical operators, evaluate
    arithmetic operators first, then comparison
    operators, and then logical operators
  • To code a selection structure, use the
    If...Then...Else statement
  • To create a compound condition, use the logical
    operators

49
Summary (continued)
  • Use the GroupBox tool to add a group box control
    to the form drag controls from the form or the
    Toolbox window into the group box control
  • To calculate a periodic payment on either a loan
    or an investment, use the Financial.Pmt method
  • To display a message box that contains text, one
    or more buttons, and an icon, use the
    MessageBox.Show method

50
Summary (continued)
  • To allow a text box to accept only certain keys,
    code the text boxs KeyPress event
  • To align the text in a control, set the controls
    TextAlign property
  • To catch an exception, and then have the computer
    take the appropriate action, use a Try/Catch block
Write a Comment
User Comments (0)
About PowerShow.com