processing decisions chapter 3. if…then…else… statements if decsalesamount > 1000 then...

49
Processing Decisions Chapter 3

Post on 22-Dec-2015

221 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Processing Decisions Chapter 3. If…Then…Else… Statements If decSalesAmount > 1000 Then sngTaxRate =.05 Else sngTaxRate =.07 End If

Processing Decisions

Chapter 3

Page 2: Processing Decisions Chapter 3. If…Then…Else… Statements If decSalesAmount > 1000 Then sngTaxRate =.05 Else sngTaxRate =.07 End If

If…Then…Else… Statements

• If decSalesAmount > 1000 ThensngTaxRate = .05

• ElsesngTaxRate = .07

• End If

Page 3: Processing Decisions Chapter 3. If…Then…Else… Statements If decSalesAmount > 1000 Then sngTaxRate =.05 Else sngTaxRate =.07 End If

Nested If Statements

• Test for multiple conditions with multiple results.

• Job level is based on salary.

• < 25000 – Administrative

• > 75000 – Senior Managers

• All Others - Managers

Page 4: Processing Decisions Chapter 3. If…Then…Else… Statements If decSalesAmount > 1000 Then sngTaxRate =.05 Else sngTaxRate =.07 End If

Nested If Statements• If decSalary > 25000 Then

– If decSalry > 75000 Then

lblLevel.Text = “Senior Manager”

– Else

lblLevel.Text = “Manager”– End If

• Else– lblLevel.Text = “Administrative”

• End If

Page 5: Processing Decisions Chapter 3. If…Then…Else… Statements If decSalesAmount > 1000 Then sngTaxRate =.05 Else sngTaxRate =.07 End If

Nested If Statements

• If decSalary <= 25000 ThenlblLevel.Text = “Administrative”

• Else– If decSalry <= 75000 Then

lblLevel.Text = “Manager”– Else

lblLevel.Text = “Senior Manager”

– End If

• End If

Page 6: Processing Decisions Chapter 3. If…Then…Else… Statements If decSalesAmount > 1000 Then sngTaxRate =.05 Else sngTaxRate =.07 End If

If ElseIf Else Statements

• If condition Thenstatements

• ElseIf elseifcondition Thenelseifstatements

• Elseelsestatements

• End If

Page 7: Processing Decisions Chapter 3. If…Then…Else… Statements If decSalesAmount > 1000 Then sngTaxRate =.05 Else sngTaxRate =.07 End If

If ElseIf Else Statements

• If decSalary < 25000 ThenlblLevel.Text = “Administrative”

• ElseIf decSalry <= 75000 ThenlblLevel.Text = “Manager”

• ElselblLevel.Text = “Senior Manager”

• End If

Page 8: Processing Decisions Chapter 3. If…Then…Else… Statements If decSalesAmount > 1000 Then sngTaxRate =.05 Else sngTaxRate =.07 End If

If ElseIf Else Example

• Bonus is a percentage of salary depending on their performance rating.

Rating Bonus

3 10%

2 7.5%

1 5%

Page 9: Processing Decisions Chapter 3. If…Then…Else… Statements If decSalesAmount > 1000 Then sngTaxRate =.05 Else sngTaxRate =.07 End If

If ElseIf Else Example

• If intRating = 3 ThensngBonus = .1

• ElseIf intRating = 2 ThensngBonus = .075

• ElseIf intRating = 1 ThensngBonus = .05

• ElsesngBonus = 0

• End If

Page 10: Processing Decisions Chapter 3. If…Then…Else… Statements If decSalesAmount > 1000 Then sngTaxRate =.05 Else sngTaxRate =.07 End If

Message Box Function

MsgBox “Prompt”,[Buttons], [“Title”]

• Prompt is the message displayed.

• Buttons are the choice of buttons and/or icon that appears in the message box.

• Title is the text on the title bar of the message box.

Page 11: Processing Decisions Chapter 3. If…Then…Else… Statements If decSalesAmount > 1000 Then sngTaxRate =.05 Else sngTaxRate =.07 End If

MessageBoxStyle Values

Enumeration Value Description

OKOnly 0 (Default) 1 Button

OKCancel 1 2 Buttons

AbortRetryIgnore 2 3 Buttons

YesNoCancel 3 3 Buttons

YesNo 4 2 Buttons

Retry Cancel 5 2 Buttons

Page 12: Processing Decisions Chapter 3. If…Then…Else… Statements If decSalesAmount > 1000 Then sngTaxRate =.05 Else sngTaxRate =.07 End If

MessageBoxStyle Icon Values

Enumeration Value Description

Critical 16 Critical Icon

Question 32 Question Mark

Exclamation 48 Exclamation Point

Information 64 Information Icon

Page 13: Processing Decisions Chapter 3. If…Then…Else… Statements If decSalesAmount > 1000 Then sngTaxRate =.05 Else sngTaxRate =.07 End If

MessageBoxStyle Default Button Values

Enumeration Value Description

Default Button 1 0 (default)

First Button is Default

Default Button 2 256 Second Button

Default Button 3 512 Third Button

Page 14: Processing Decisions Chapter 3. If…Then…Else… Statements If decSalesAmount > 1000 Then sngTaxRate =.05 Else sngTaxRate =.07 End If

MessageBoxStyle Modality Values

Enumeration Value Description

ApplicationModal 0 (default) User must respond to the message box before continuing with this application.

SystemModal 4096 All applications are suspended until user responds to message box.

Page 15: Processing Decisions Chapter 3. If…Then…Else… Statements If decSalesAmount > 1000 Then sngTaxRate =.05 Else sngTaxRate =.07 End If

MessageBoxStyle Window Settings Values

Enumeration Value Description

MsgBoxSetForeground

65536 Window in front.

MsgBoxRight 524288 Text is Right Aligned.

MsgBoxRtlReading 1048576 Text appears right to left (other language).

Page 16: Processing Decisions Chapter 3. If…Then…Else… Statements If decSalesAmount > 1000 Then sngTaxRate =.05 Else sngTaxRate =.07 End If

Setting Enumerations

• YesNo 4• Question 32• MsgBoxRight 524288• Sum 524324

• intButtons = 524324• MsgBox( “Yes or No?” , intButtons, “Clarification”)

Page 17: Processing Decisions Chapter 3. If…Then…Else… Statements If decSalesAmount > 1000 Then sngTaxRate =.05 Else sngTaxRate =.07 End If

• YesNo 4• Critical 16• DefaultButton2 256• Sum 276

• intButtons = 276• MsgBox( “Do you want to exit?” , intButtons, “Exit?”)

Setting Enumerations

Page 18: Processing Decisions Chapter 3. If…Then…Else… Statements If decSalesAmount > 1000 Then sngTaxRate =.05 Else sngTaxRate =.07 End If

MessageBox Return Values

Button Pushed Value Constant

OK 1 vbOK

Cancel 2 vbCancel

Abort 3 vbAbort

Retry 4 vbRetry

Ignore 5 vbIgnore

Yes 6 vbYes

No 7 vbNo

Page 19: Processing Decisions Chapter 3. If…Then…Else… Statements If decSalesAmount > 1000 Then sngTaxRate =.05 Else sngTaxRate =.07 End If

btnExit_Click

Dim intResponse as IntegerintResponse = MsgBox(“Do you

want to exit?”, MsgBoxStyle.Exclamation + MsgBoxStyle.DefaultButton2 + MsgBoxStyle.YesNo, “Exit?)

If intResponse = vbYes ThenEnd

End IF

Page 20: Processing Decisions Chapter 3. If…Then…Else… Statements If decSalesAmount > 1000 Then sngTaxRate =.05 Else sngTaxRate =.07 End If

Radio Buttons

• A Radio button is a control for selecting one option from a list of possible options.

• A Group box assigns a set of radio buttons to a group.

• A group of radio buttons can only have one button selected.

• The selected button’s Checked property is set to true.

Page 21: Processing Decisions Chapter 3. If…Then…Else… Statements If decSalesAmount > 1000 Then sngTaxRate =.05 Else sngTaxRate =.07 End If

Grouped Radio Buttons

• Create the group box first.

• Create the radio buttons inside the group box.

• This assures only one radio button in the group can be selected.

Page 22: Processing Decisions Chapter 3. If…Then…Else… Statements If decSalesAmount > 1000 Then sngTaxRate =.05 Else sngTaxRate =.07 End If

Check Box Control

• A Check box is a control that indicates whether a specific option is selected or not.

• Any number of check boxes in a group can be selected. Group boxes may or may not be used.

• A check mark appears in the box.• Its Checked property is set to True.

Page 23: Processing Decisions Chapter 3. If…Then…Else… Statements If decSalesAmount > 1000 Then sngTaxRate =.05 Else sngTaxRate =.07 End If
Page 24: Processing Decisions Chapter 3. If…Then…Else… Statements If decSalesAmount > 1000 Then sngTaxRate =.05 Else sngTaxRate =.07 End If

Radio Buttons

• If rdbAdmin.Checked Then– strLevel = “Administrative” &

vbCrLf

• ElseIf rdbMgr.Checked Then– strLevel = “Manager” & vbCrLf

• Else– strLevel = “Senior Manager” &

vbCrLf

• End If

Page 25: Processing Decisions Chapter 3. If…Then…Else… Statements If decSalesAmount > 1000 Then sngTaxRate =.05 Else sngTaxRate =.07 End If

Check Boxes

• If chkMedical.Checked Then– strBenefits = vbCrLf & “Medical

Insurance”

• End If• If chkLife.Checked Then

– strBenefits = strBenefits & vbCrLf & “Life Insurance”

• End If• If chk401K.Checked Then

– strBenefits = strBenefits & vbCrLf & “401K”

• End If

Page 26: Processing Decisions Chapter 3. If…Then…Else… Statements If decSalesAmount > 1000 Then sngTaxRate =.05 Else sngTaxRate =.07 End If

Data Validation

• Missing Data• If txtData.Text <> “” Then

– MsgBox(“You entered: “ & txtData.Text, MsgBoxStyle.Information, “Data is Valid”)

• Else– MsgBox(“Data is required. Please try again.”,

MsgBoxStyle.Critical, “Invalid Data”)

• End If

Page 27: Processing Decisions Chapter 3. If…Then…Else… Statements If decSalesAmount > 1000 Then sngTaxRate =.05 Else sngTaxRate =.07 End If

IsNumeric Function

• If the Val function encounters empty or invalid text string it converts the string to 0.

• IsNumeric function checks a text string and determines if it can be evaluated as a number.

• Returns a value of True or False.

• IsNumeric(expression)

Page 28: Processing Decisions Chapter 3. If…Then…Else… Statements If decSalesAmount > 1000 Then sngTaxRate =.05 Else sngTaxRate =.07 End If

Data Validation

• Non-numeric Data• If IsNumeric(txtData.Text) Then

– MsgBox(“You entered: “ & txtData.Text, MsgBoxStyle.Information, “Data is Valid”)

• Else– MsgBox(“A numeric value is required. Please

try again.”, MsgBoxStyle.Critical, “Invalid Data”)

• End If

Page 29: Processing Decisions Chapter 3. If…Then…Else… Statements If decSalesAmount > 1000 Then sngTaxRate =.05 Else sngTaxRate =.07 End If

IsNumeric Examples

Expression Return Value

“123” True

“123.01” True

“$1000” False

“Zero” False

“$100,000” False

“123 Dollars” False

Page 30: Processing Decisions Chapter 3. If…Then…Else… Statements If decSalesAmount > 1000 Then sngTaxRate =.05 Else sngTaxRate =.07 End If

Data Validation

• Valid Range• If txtData.Text < 160 Then

– MsgBox(“You entered: “ & txtData.Text, MsgBoxStyle.Information, “Data is Valid”)

• Else– MsgBox(“Vacation hours may not exceed 160

per year.”, MsgBoxStyle.Critical, “Invalid Data”)

• End If

Page 31: Processing Decisions Chapter 3. If…Then…Else… Statements If decSalesAmount > 1000 Then sngTaxRate =.05 Else sngTaxRate =.07 End If

Combining Data Validation

• If IsNumeric(txtData.Text) Then– If txtData.Text < 160 Then

MsgBox(“You entered: “ & txtData.Text, MsgBoxStyle.Information, “Data is Valid”)

– Else MsgBox(“Vacation hours may not exceed 160 per year.”,

MsgBoxStyle.Critical, “Invalid Data”)

– End If

• Else MsgBox(“A numeric value is required. Please try

again.”, MsgBoxStyle.Critical, “Invalid Data”)

• End If

Page 32: Processing Decisions Chapter 3. If…Then…Else… Statements If decSalesAmount > 1000 Then sngTaxRate =.05 Else sngTaxRate =.07 End If

String Comparisons

• Database applications use string comparisons to validate input and to sort information.

• The order of string data is determined by each character’s ASCII value.

• ASCII (American Standard Code for Information Interchange) is a standard designation of characters and symbols.

Page 33: Processing Decisions Chapter 3. If…Then…Else… Statements If decSalesAmount > 1000 Then sngTaxRate =.05 Else sngTaxRate =.07 End If

String Comparisons

• String comparisons are made from left to right one character pair at a time.

• The comparison is terminated when a character is unequal.

• “Browning” compared to “Brower”

• The first four pairs are equal.

• The fifth pair is unequal (n and e).

Page 34: Processing Decisions Chapter 3. If…Then…Else… Statements If decSalesAmount > 1000 Then sngTaxRate =.05 Else sngTaxRate =.07 End If

String Comparisons

Value 1 ASCII Value

Value 2 ASCII Value

Value 1 > Value 2

Z 122 A 65 True

+1 43 -1 45 False

A 65 ? 63 True

Missouri 79 Mississippi 73 True

100 49 98 57 False

Page 35: Processing Decisions Chapter 3. If…Then…Else… Statements If decSalesAmount > 1000 Then sngTaxRate =.05 Else sngTaxRate =.07 End If

UCase and LCase Functions

• Character Case is very important in string comparisons.

• Upper case letters are < lower case letters.

• Use LCase or UCase so you do not have to validate all possible user inputs.

Page 36: Processing Decisions Chapter 3. If…Then…Else… Statements If decSalesAmount > 1000 Then sngTaxRate =.05 Else sngTaxRate =.07 End If

Data Validation

• Specific Input

If UCase(txtDept.Text) = “ACCT” Then

ElseIf UCase(txtDept.Text) = “ECO” Then

Else

End If

Page 37: Processing Decisions Chapter 3. If…Then…Else… Statements If decSalesAmount > 1000 Then sngTaxRate =.05 Else sngTaxRate =.07 End If

Logical Operators

• And – If both conditions are True the result is True.

• Or – If either condition is True the result is True.

• Not – The condition is reversed. True becomes False, False becomes True.

Page 38: Processing Decisions Chapter 3. If…Then…Else… Statements If decSalesAmount > 1000 Then sngTaxRate =.05 Else sngTaxRate =.07 End If

Logical If Example

• In order to qualify for a bonus, an employee must be employed for at least 12 months and earn at least $25,000 annually.

Page 39: Processing Decisions Chapter 3. If…Then…Else… Statements If decSalesAmount > 1000 Then sngTaxRate =.05 Else sngTaxRate =.07 End If

Logical If Example

• If intMonthsEmployed >= 12 AND decSalary >= 25000 Then– Msgbox “You qualify for the employee bonus.”

• Else– Msgbox “You are not eligible for a bonus.”

• End If

Page 40: Processing Decisions Chapter 3. If…Then…Else… Statements If decSalesAmount > 1000 Then sngTaxRate =.05 Else sngTaxRate =.07 End If

Logical If Example

• An employee may qualify for a bonus, when been employed for 18 months or when he earns $25,000 annually.

Page 41: Processing Decisions Chapter 3. If…Then…Else… Statements If decSalesAmount > 1000 Then sngTaxRate =.05 Else sngTaxRate =.07 End If

Logical If Example

• If intMonthsEmployed >= 18 OR decSalary >= 25000 Then– Msgbox “You qualify for the employee bonus.”

• Else– Msgbox “You are not eligible for a bonus.”

• End If

Page 42: Processing Decisions Chapter 3. If…Then…Else… Statements If decSalesAmount > 1000 Then sngTaxRate =.05 Else sngTaxRate =.07 End If

Select Case Structure

• Used when a single expression is tested for multiple values.

• A Case is an individual condition.Select Case expression

Case expressionlist

Case expressionlist

End Select

Page 43: Processing Decisions Chapter 3. If…Then…Else… Statements If decSalesAmount > 1000 Then sngTaxRate =.05 Else sngTaxRate =.07 End If

Select Case Example

• Bonus is a percentage of salary depending on their performance rating.

Rating Bonus

8-10 10%

4-7 7.5%

1-3 5%

0 0%

Page 44: Processing Decisions Chapter 3. If…Then…Else… Statements If decSalesAmount > 1000 Then sngTaxRate =.05 Else sngTaxRate =.07 End If

Select Case Example

• Select Case intRating– Case < 1

• sngBonusRate = 0

– Case 1 To 3• sngBonusRate = .05

– Case 4 To 7• sngBonusRate = .075

– Case 8 To 10• sngBonusRate = .1

• End Select

Page 45: Processing Decisions Chapter 3. If…Then…Else… Statements If decSalesAmount > 1000 Then sngTaxRate =.05 Else sngTaxRate =.07 End If

Select Case Example

Page 46: Processing Decisions Chapter 3. If…Then…Else… Statements If decSalesAmount > 1000 Then sngTaxRate =.05 Else sngTaxRate =.07 End If

Radio Button CheckChanged Event

• Private mintRdbIndex as Integer• Private Sub rdbLowSalary_CheckChanged ()

– mintRdbIndex = 0

• End Sub• Private Sub rdbHighSalary_CheckChanged ()

– mintRdbIndex = 3

• End Sub

Page 47: Processing Decisions Chapter 3. If…Then…Else… Statements If decSalesAmount > 1000 Then sngTaxRate =.05 Else sngTaxRate =.07 End If

Select Case Example

• Select Case mintRdbIndex– Case 0

• lblResult.Text = “Your salary is less than $20,000.”

– Case 1 • lblResult.Text = “Your salary is between $20,000 and

$40,000.”

– Case 2• lblResult.Text = “Your salary is between $41,000 and

$60,000.”

– Case 3• lblResult.Text = “Your salary is greater than $60,000.”

• End Select

Page 48: Processing Decisions Chapter 3. If…Then…Else… Statements If decSalesAmount > 1000 Then sngTaxRate =.05 Else sngTaxRate =.07 End If

Loan Payment Application

Page 49: Processing Decisions Chapter 3. If…Then…Else… Statements If decSalesAmount > 1000 Then sngTaxRate =.05 Else sngTaxRate =.07 End If

Mail Order Shipping

Costs