Decision Making - PowerPoint PPT Presentation

1 / 41
About This Presentation
Title:

Decision Making

Description:

Modifying the Automobile Loan Calculator Application. 4. Chapter 5: Decision Making ... Open the Automobile Loan Calculator program ... for Auto Loan Calculator ... – PowerPoint PPT presentation

Number of Views:39
Avg rating:3.0/5.0
Slides: 42
Provided by: steve1703
Category:

less

Transcript and Presenter's Notes

Title: Decision Making


1
Chapter 5
  • Decision Making

2
Objectives
  • Use the ComboBox control, MessageBox class
  • Use the IfThenElse structure
  • Use a nested IfThenElse structure
  • Use the Select Case structure
  • Validate user input
  • Use the string concatenation in code
  • Use relational and logical operators in code

3
Modifying the Automobile Loan Calculator
Application
4
Problem Analysis
  • The primary problem is to determine the interest
    rate based on the customers credit rating
  • If the CreditRating is A Excellent then
  • AdjustedInterestRate InterestRate
  • If the CreditRating is B Good then
  • AdjustedInterestRate InterestRate 1.1
  • If the CreditRating is C Average then
  • AdjustedInterestRate InterestRate 1.15

5
Interface Design
  • The interface allows user to select one credit
    rating
  • A ComboBox control presents a list of choices
  • Set the properties of ComboBox to a drop-down
    list box
  • Two messages to inform errors in the input data
  • One for forgetting to select credit rating
  • The other for exceeding 5000 for Low credit
    rating

6
Program Design
  • A flowchart represents the logic for the Compute
    Payment Click event
  • A nested ifthenelse is a structure which
    contains another ifthenelse

7
Modifying the User Interface
  • Open the Automobile Loan Calculator program
  • Right-click the Form1 form in the main work area,
    and then click Lock Controls on the shortcut menu
    to unlock the form
  • Increase the form height to 248 pixels
  • Select the txtnMonthlyPament, btnComputePayment,
    Label3 and btnReset controls. Drag the controls
    down four marks on the positioning grid
  • Add a Label and ComboBox controls, and position
    as shown on the next slide

8
Change Control Properties
  • Change the properties of Label 4, and ComboBox1

9
Setting the Items Property
  • Select the Items property in the Properties
    window
  • Click the Collection build button
  • Type the five items into the String Collection
    Editor, Upon completion, lock the controls

10
Resetting the ComboBox Value
  • The SelectedIndex property of the ComboBox
    control is assigned the value of the items
    index, 0, 1, 2, etc.
  • When no item is selected, the SelectedIndex
    property is set to -1
  • Add the following line of code to the end of the
    btnReset_Click Event Procedure

11
Decision-Making Control Structures
  • Decision making is the process of determining
    which of one or more paths to take
  • IfThenElse structure
  • Select Case structure

12
IfThenElse Structure for Auto Loan Calculator
  • If the Credit rating I blank, then an error
    message displays and the focus is set to the
    cmbCreditRating control
  • If the Credit rating is not blank, then the input
    is valid and the procedure continues to execue

13
The MessageBox Class
  • The MessageBox class provides the ability to
    display a message to the user in a pop-up window
  • The Show() method must be used to display a
    message with a title bar
  • MessageBox.Show(Please enter the customers
    credit rating in the Credit rating list box.,No
    Credit Rating)

14
The Focus() Method
  • When a user selects a control on a form, the
    control is said to have focus
  • It is often necessary to set the focus to a
    control during run time
  • cmbCreditRating.Focus()

15
IfThenElse Statement
  • Relational expression
  • , , ,

16
IfThenElse Statement
  • Block-level scoping if you declare a variable in
    a block statement, such as IfThenElse
    statement, the variable is valid only inside that
    block.
  • Exit Sub Statement
  • Terminates execution of a procedure immediately

17
Considering Selection Structures
  • To count number of registered and unregistered
    voters

18
Considering Selection Structures
  • To count number of registered voters

19
Considering Selection Structures
  • To count number of unregistered voters

20
Considering Selection Structures
21
Declaring Additional Variables
  • Two additional variables are needed in the
    btnComputePayment_Click event procedure
  • dblAdjustedRate holds the value of the interest
    rate after customers credit rating is used to
    adjust the interest rate dblRate
  • strErrorMessage displays when a customer with low
    credit rating tries to borrow more than allowed

22
IfThenElse Structure
23
The Nested IfThenElse Structure
24
  • What are the intNEMale, intNEFem, intNRMale,
    intNRFem, intNVote and intVote counting?

25
Coding a Nested IfThenElse Structure
  • The value of radTwoYears.Checked is of boolean
    type. It is either true or false

Without looking at your book, convert the flow
chart to the nested IfThenElse statement
26
Coding a Nested IfThenElse Structure
  • Delete radTwoYears_CheckedChanged(),
    radFiveYears_CheckedChanged(), and
    radSixYears_CheckedChanged() subroutines.
  • Enter the nested IfThenElse statement

27
Compound Condition
  • You may use Not, And, Or, Xor logical Operators
    to make compound condition
  • A relational expression that is preceded by the
    Not logical operator forms a condition that is
    false when the relational expression is true.

If Not A B Then MessageBox.Show() End
If
If A
28
And Logical Operator
  • The And logical operator requires both
    conditions to be true for the compound condition
    to be true

If strGender M And intAge 20 Then
MessageBox.Show(strEmpName) End If
If strGender M Then If intAge 20
Then MessageBox.Show(strEmpName)
End If End If
29
And Logical Operator
30
Or Logical Operator
  • The Or logical operator requires only one of two
    or more conditions to be true for the compound
    condition to be true.
  • The compound condition is false, if all
    conditions are false

Assuming C is 4, X is 4.9, Y is 4.8
31
Combining Logical Operators
  • If XY Or TD And H
  • intCount intCount 1
  • End If
  • Rules of precedence
  • Reads from left to right
  • Parentheses
  • Arithmetic operators
  • Relational operators
  • Not operators
  • And/AndAlso operators
  • Or/OrElse or Xor operators

D is 3, H is 3, R is 2, T is 5, X is 3 and Y is 2
32
Parentheses in the Evaluation of Compound
Conditions
  • Parentheses can be used to change the order of
    precedence
  • Avoid ambiguity and group conditions with a
    desired logical operator
  • (CD And S4) Or (X

Assume C is 6, D is 3 C 7 And D
0 C 7 And (D 0)
33
Logical Operator and String Expressions
  • You may use operator to concatenate string
  • strErrorMessage Customers with this credit
    rating may only borrow strMoney

Without looking at your book, convert the flow
chart to the nested IfThenElse statement
34
Logical Operator and String Expressions
35
The Select Case Structure
36
The Select Case Structure
  • The keyword Is is required when a relational
    operator, such as
  • In the second example, strCode contains no
    lowercase. Otherwise, you should include a Case
    Else in the Select Case Statement

37
In-Class Exercises
  • Write a series of code statements to perform the
    logic shown in the figure. Declare any variables
    you use and name the variables following the
    conventions.

38
The Select Case Structure
  • When specifying a range in a Case clause, make
    sure the smaller value is listed first

39
Coding a Select Case Statement
40
Modifying a Parameter in a Function
  • The function call to the Pmt( ) function
    currently accepts the dblAdjustedRate variable as
    the interest rate parameter
  • Takes the customers credit rating into account

41
Summary
  • Use the ComboBox control
  • Use the IfThenElse structure
  • Use a nested IfThenElse structure
  • Use the Select Case structure
  • Validate user input
  • Use the MessageBox class
  • Use the string concatenation in code
  • Use relational operators in code
  • Use logical operators in code
Write a Comment
User Comments (0)
About PowerShow.com