Title: Making Decisions in VB
1Making Decisions in VB
2The if-then-else Structure in VB
- if ltcondition 1gt then
- statements to execute if condition 1 is true
- elseif ltcondition 2gt then
- statements to execute if condition 2 is true
- else
- statements to execute if earlier conditions not
true - endif
3Types of Conditions
- equality if ThisValue NewValue then
- inequality if ThisValue ltgt OldValue then
- greater than if ThisValue gt MyValue then
- less than if ThisValue lt YourValue then
- greater than or equal to if Big gt Small then
- less than or equal to if Big lt Huge then
4if-then-else Example
- if BankBalance gt 500 then
- ServiceCharge 0
- else
- ServiceCharge 5
- endif
5if-then-elseif-else Example
if BankBalance gt 500 then ServiceCharge
0 elseif BankBalance gt 250 then ServiceCharge
3 else ServiceCharge 5 endif
6if-then with No else Example
- if NumCuts gt 5 then
- stGrade F
- endif
7Nested If Statements
- If stSex M Then
- If iAge gt 18 Then
- stCategory Adult Male
- Elseif iAge gt 12 Then
- stCategory Teen Male
- Elseif iAge gt 2 then
- stCategory PreTeen Male
- Else stCategory Infant Male
- Endif
- Else
- if iAge gt 18 Then
- stCategory Adult Female
- Else stCategory Non Adult Female
- Endif
- Endif
8If2ElseIf Example2 Water Bill
- if WaterUse lt 1000 then
- Bill WaterUse.005
- elseif WaterUse lt 2000 then
- Bill 5 (WaterUse - 2000).010
- elseif WaterUse lt 5000 then
- Bill 15 (WaterUse - 2000).015
- else
- Bill 60 (WaterUse - 5000) .025
- endif
9Income Taxes for Single Filers Standard Deduction
-- 4000 Dependents -- 2550 each Tax Calculation
on resulting taxable income Taxable Income Amount
Percent Of Amt Over 0-24,000 0 15 0 24,000-
58,150 3,600 28 24,000 58,150-
121,300 13,162 31 58,150 121,300-
262,750 32,738.50 36 121,300 263,750
up 84,020.50 39.6 263.750
10The Select Case Decision Structure Syntax
Select Case Grade Case Is gt 90
LetterGrade "A" Case Is gt 80
LetterGrade "B" Case Is gt 70
LetterGrade "C" Case Is gt 60
LetterGrade "D" Case Else
LetterGrade "F" End Select