CHAPTER 5.2
IF Statement
§ EXAMPLE 1 5-2-15-2-1.vbp
- Task
Find the larger of two numbers: N1, N2
- Form Design
Logic
FalseTrue
- Code
Private Sub cmdFindLarger_Click()
Dim largerNum As Single
picResult.Cls
If Val(txtFirstNum.Text) > Val(txtSecondNum.Text) Then
largerNum = Val(txtFirstNum.Text)
Else
largerNum = Val(txtSecondNum.Text)
End If
picResult.Print "The larger number is"; largerNum
End Sub
§ EXAMPLE 2: NESTED IF (5-2-2)5-2-2.vbp
- Task
Enter Revenue and Cost, display "Break even", "Profit", or "Loss"
- Form Design
- Logic
FalseTrue
FalseTrue
- CodeIndentation of nested Loops
Private Sub cmdShow_Click()
Dim costs As Single, revenue As Single, profit As Single
costs = Val(txtCosts.Text)
revenue = Val(txtRev.Text)
picResult.Cls
If costs = revenueThen
picResult.Print "Break even"
Else
If costs < revenue Then
profit = revenue - costs
picResult.Print "Profit is "; FormatCurrency(profit, 2)
Else
loss = costs - revenue
picResult.Print "Loss is "; FormatCurrency(loss, 2)
End If
End If
End Sub
§ EXAMPLE 3: COMPOUND CONDITION (SJ5-2-3) SJ5-2-1.vbp
- Task
MSIS is hiring a web master. To qualify for the position, the person must be a MSIS major, has passed either MS309 or MS 310. Record of candidate is stored in a sequential file, WebRate.TXT, such as
"John","Accounting","Y","N"
"Judy","MSIS","N", "N"
"Michael","Finance","Y","Y"
"Jeniffer","MSIS","Y","N"
"Amy","MSIS","Y","Y"
First field:Name
Second field:Major
Third field:Passed MS309
Forth field:Passed MS310
- Form Design
- Logic
False
True
False
True
- Code
Private Sub Command1_Click()
Open App.Path & "\WebRat.txt" For Input As #1
Picture1.Print "Name", "Major", "MS309", "MS310"
Picture1.Print
Do While Not EOF(1)
Input #1, N$, M$, MS309$, MS310$
If M$ = "MSIS" And (MS309$ = "Y" Or MS310$ = "Y") Then
Picture1.Print N$, M$, MS309$, MS310$
End If
Loop
End Sub
EXAMPLE 4 (5-2-6)5-2-6.vbp
- Task
Determine the FICA Tax, which is composed of
- SST (Social Security Tax): 6.2% on the first $72,600
- Medicare Tax: 1.45% of earning
Form Design
- SST Calculation
/ Case 1
YTD / Current
72600
/ Case 2
YTD / Current
72600
/ Case 3
YTD / Current
72600
- Code
Private Sub cmdCalculate_Click()
Dim FicaTaxes As Single
FicaTaxes = FICA(Val(YTD), Val(Current))
picTax.Cls
picTax.Print "Your FICA taxes for the current"
picTax.Print "pay period are "; FormatCurrency(FicaTaxes, 2)
End Sub
Private Function FICA(ytdEarnings As Single, curEarnings As Single) As Single
Dim SST As Single, medicare As Single
SST = 0
If (ytdEarnings + curEarnings) <= 72600 Then
SST = 0.062 * curEarnings
Else
If ytdEarnings < 72600 Then
SST = 0.062 * (72600 - ytdEarnings)
Endif
End If
medicare = 0.0145 * curEarnings
FICA = SST + medicare
End Function
HOMEWORK
A.Do Practice Question 1 and 2 of Problems 5.2
B.Do the following questions of Exercise 5.2
1, 3, 7, 11, 13, 17, 19, 21, 23, 25, 27, 29, 33, 35, 37, 40, 41
C.Project 7
Design and Code a program for the 309 Dating Service Inc.HW7.vbp
Design a Main form to input the player's data, such as
DATA ENTRY
Name:Use a text box and store the entry in MyName
Sex:Use 2 option controls and store the entry in MyGender
Age:Use a Horizontal Scroll bar (Range from 10 to 99) and store the entry in MyAge
Height:Use a Horizontal Scroll bar (Range from 140 CM to 220 CM),) and store the entry in MyHeight.
In addition to the metric measure, also display the value of scrollbar in English Measure (use a function to convert height in centimeter to feet and inches)
Interest:Use a Combo box and store the entry in MyInterest
MENUHw4-Menu.vbpHw4.vbp
Instead of using command buttons, Project 7 is a menu driven program. Structure of menu is as follows:
Start:Ask if the player needs instruction. Display instruction if player so desire, otherwise set focus to Text box to start entering data
Display a brief description of this program (use your own word) if player so desire, otherwise, set focus to Text box for Name
Exit:Terminate the program
Enter:Display a message about how to enter data
Enter Data upon clicking OK
Confirm:Ask if the data entry is correct
If answer is 'Yes", display the data you enter on a separate form, otherwise clear all data field and re-enter data.
And click OK on the message box
Return to main form;
- To convert height from centimeter to feet and inches:
Public Sub Conversion(Height As Integer, EHeight As String)
Dim e As Single, f As Integer, i As Integer
e = Height / 2.54
f = e \ 12
i = e Mod 12
EHeight = Str(f) + Chr(39) + Str(i) + Chr(34)
End Sub
Double QuoteSingle Quote
VB5-21