Title: Decision Making
1Decision Making
2If Statements
3Conditional Operators
4Using If Statements (One-Way Selection)
- If (condition) Then
- Statement(s)
- End If
5Using If Statements
- Example
- If (decBalance lt 0.0) Then
- MsgBox(Account Overdrawn!)
- End If
6Flowchart Symbols
Input/ Output
7Using IfElse Statements(Two-Way Selection)
- If (condition) Then
- True-Statement(s)
- Else
- False-Statement(s)
- End If
8Using ifElse Statements
- If (intNumber gt 0) Then
- MsgBox(Non-Negative)
- Else
- MsgBox(Negative)
- End If
9Overtime Pay Problem
- Create a program to compute the weekly gross pay
for a companys employees assuming
time-and-a-half is to be paid for hours worked in
excess of 40. The inputs to the program are hours
worked and hourly rate of pay. The program should
then output the employees gross pay.
10Using Check Boxes
- Each check box has a Checked Property that is
set to true if the box is checked and false if
the box is not checked. The label that appears
next to the check box is part of the check box
control. The check box has a text property that
specifies its label. This property lets the user
know if the box is checked (true) or not (false).
Check box controls should have a chk prefix.
11Setting the Default Checked Property
- You use the checked property to set the default
value of the check box. - Example
- chkTall.Checked True
- If (intHeight gt 72) Then
- chkTall.Checked True
- End If
12Using Check Boxes Radio Buttons
13Logical Operators
14Career Suggestion Problem
- Create a program to suggest a career based on an
individuals personal traits. Call the program
Career and label the form Career Suggestion. Have
a Tall (over 72 inches) and a Heavy (over 250
pounds) check boxes.
15Use the following criteria
- Tall and Heavy Policeman
- Not Tall and Heavy Professional Wrestler
- Tall and Not Heavy Basketball Player
- Not Tall and Not Heavy Jockey
- Display a label You can be a XXXXX! when a
button labeled Career is pushed.
16Nested If Statements and Radio Buttons
17IF Structures
Two-way
One-way
18Nested If Statement
- An If statement in which one or more additional
If statements follows one of the reserved words
Then or Else of another If statement.
19Nested If Structure
20Using Nested If Statements
- If (condition-1) Then
- If (condition-2) Then
- Statement(s)
- Else
- Statement(s)
- End If
- Else
- Statement(s)
- End If
21Activity Selection Problem
- Create a program that is designed to read in a
temperature and print out the activity that is
appropriate for that temperature using the
following guidelines. -
- Activity Temperature
- Surfing 85 lt Temp
- Frisbee 70 lt Temp lt 85
- Skateboarding 32 lt Temp lt 70
- Skiing 0 lt Temp lt 32
- Interpretive Dance Temp lt 0
22Using Radio Buttons
- Radio buttons always appear in groups, and only
one button in the group can be selected at a
time. Radio buttons are also called option
buttons because they allow the user to select
from several options.
23Using Radio Buttons (Continued)
- Three steps are involved in using radio buttons
- Create a GroupBox to group the radio buttons.
- Create the radio buttons in the GroupBox.
- Write the code to use the radio buttons.
24Implementing Radio Buttons
- When you create radio buttons use the prefix opt.
- Use form-level variables as the scope to keep
track of the option that has been selected.
Declare these variables under the heading Windows
Form Designer generated code. Select the form,
open the code window, and select (Declarations)
from the Methods list.
25Car Rental Cost Problem
- Construct a program for determining the cost of
a car rental. The number of days the car was
rented and the miles that were driven are inputs
to the program. The rental charges are as
follows - Car Type Rent
- Compact 25 a day
- Midsize 30 a day 0.10 a mile
- Luxury 35 a day 0.15 a mile
- SUV 50 a day .20 a mile for all miles
over 100
26Select Case Structures
27Using Select Case
- The select case is another way to make
multi-level decisions in a program. With Select
Case you specify a variable to test and then list
a number of cases for which you want to test.
28Select Case Syntax
- Select Case variable
- Case 1 to 2
- Statement(s)
- Case 3 to 4
- Statement(s)
- .
- .
- Case Else
- Default Statement
- End Select
29Quarter of the Month Problem
-
- Write a program to read a month number 1 to 12
and determine and print the yearly quarter the
month falls into.
30Loops, Multiple Forms, Menus, and Printing
31Do Loops
32Loops (Iteration)
- Three kinds of loops
- For Next, Do While, and Do Until. In this lesson
we will examine the Do Loop. All of these loops
repeat a block of code statements a certain
number of times.
33Do While Loop
- Do While condition
- Statement(s)
- Loop
34Do Loop Example
- intSum 0
- intIndex 1
- Do While intIndex lt 10
- intSum intSum intIndex
- intIndex intIndex 1
- Loop
35Inflation Problem
- Create a program to determine the effect of
inflation on the cost of an item. The program
should input the current cost of an item, a
yearly inflation rate, and the number of years
the inflation rate is to be applied to the items
cost. The program should then display the new
cost of the item after it has inflated, at the
given rate, for the specified number of years.
36Do Until Loop
- A Do Until Loop repeats until a condition is
true. It is an exit controlled loop - Do
- Statement(s)
- Loop Until condition
37Do Until Loop Example
- intSum 0
- intIndex 1
- Do
- intSum intSum intIndex intIndex
- intIndex intIndex 1
- Loop Until intSum gt 100
38Prices to Double Problem
- Construct a program that will input a yearly
inflation rate and determine and display the
number of years for prices to double given the
inflation rate.
39Using the InputBox Function
- StrName InputBox(User Prompt, Title,
Default Text) - Where User Prompt is a question displayed to the
user, Title is the boxes title bar text, and
Default Text is the users default response.
40InputBox Example
- strName InputBox(Where do you live?, Enter
City, Fairfield)
41Threshold Problem
-
- Construct a program to input number until a
threshold value has been reached. Use 1000 as the
threshold value. Report the sum the caused the
threshold to be reached and how many readings
were entered.
42 43Demo 2 TipCalculator Problem Statement
- Write a program that determines how much someone
should tip in a restaurant. The following rules
apply - The minimum tip is 1.00
- Tip 20 if the bill is 20 or less
- Tip 15 if the bill is more than 20
- Tip an extra 1 if the service was especially
good
44Demo 2 TipCalculator IPO
45Demo 2 TipCalculator Flowchart
A
Start
Get bill
Tip lt 1
Y
Tip 1
Meal gt 20
Tip .15 Bill
Y
N
Good service
Tip Tip 1
Y
N
Tip .20 Bill
N
Tip
A
End
46Demo 2 TipCalculator User Interface
47Demo 3 GradeCalculator Problem Statement
Write a program that assigns student grades based
on four test scores. The user enters the three
test scores and the program computes the total,
average, and letter grade for that user.
48Demo 3 GradeCalculator IPO
49Demo 3 GradeCalculator Flowchart
Start
Get test scores
A
Calculate Total
Total, average, grade
Calculate Average
End
Grade?
F lt60
A gt 90
C gt70
B gt80
D gt60
A
50Demo 3 GradeCalculator User Interface
51Exponentiation, Order of Operations, and Error
Handling
52Data Types and Variables
53Strings and Decimal Types
54 55Pre-built Functions
- CDec convert to decimal
- CInt convert to integer
- CStr convert to string
- decScore1 CDec(txtScore1.text)
- lblGrade.text CStr(decTotalScore)
- NOTE The textbook uses the VAL function. We will
be using the Convert - Functions Instead of the VAL function. It is a
more current way of doing - things and will work better with OPTION STRICT.
56Importance of Data Types
Liftoff of Ariane 5 Rocket, June 4, 1996 CNN
Interactive, http//www.cnn.com/TECH/9710/30/arian
e.launch
57New Controls