Title: Decision Making
1Chapter 5
2Objectives
- 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
3Modifying the Automobile Loan Calculator
Application
4Problem 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
5Interface 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
6Program Design
- A flowchart represents the logic for the Compute
Payment Click event - A nested ifthenelse is a structure which
contains another ifthenelse
7Modifying 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
8Change Control Properties
- Change the properties of Label 4, and ComboBox1
9Setting 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
10Resetting 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
11Decision-Making Control Structures
- Decision making is the process of determining
which of one or more paths to take - IfThenElse structure
- Select Case structure
12IfThenElse 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
13The 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)
14The 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()
15IfThenElse Statement
- Relational expression
- , , ,
16IfThenElse 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
17Considering Selection Structures
- To count number of registered and unregistered
voters
18Considering Selection Structures
- To count number of registered voters
19Considering Selection Structures
- To count number of unregistered voters
20Considering Selection Structures
21Declaring 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
22IfThenElse Structure
23The Nested IfThenElse Structure
24- What are the intNEMale, intNEFem, intNRMale,
intNRFem, intNVote and intVote counting?
25Coding 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
26Coding a Nested IfThenElse Structure
- Delete radTwoYears_CheckedChanged(),
radFiveYears_CheckedChanged(), and
radSixYears_CheckedChanged() subroutines. - Enter the nested IfThenElse statement
27Compound 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
28And 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
29And Logical Operator
30Or 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
31Combining 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
32Parentheses 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)
33Logical 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
34Logical Operator and String Expressions
35The Select Case Structure
36The 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
37In-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.
38The Select Case Structure
- When specifying a range in a Case clause, make
sure the smaller value is listed first
39Coding a Select Case Statement
40Modifying 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
41Summary
- 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