19.12.2012 Views

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

110 <strong>Computer</strong> <strong>Programming</strong> <strong>Concepts</strong> <strong>and</strong> <strong>Visual</strong> <strong>Basic</strong><br />

This block searches for the first true condition, carries out its action, <strong>and</strong> then skips to the<br />

statement following End If. If none of the conditions is true, then Else’s action is carried out.<br />

Execution then continues with the statement following the block. In general, an If block can<br />

contain any number of ElseIf clauses. As before, the Else clause is optional.<br />

EXAMPLE 5<br />

Redo Example 1 so that if the two numbers are equal, the program so reports.<br />

SOLUTION:<br />

Private Sub cmdFindLarger_Click()<br />

picResult.Cls<br />

If Val(txtFirstNum.Text) > Val(txtSecondNum.Text) Then<br />

picResult.Print “The larger number is “; txtFirstNum.Text<br />

ElseIf Val(txtSecondNum.Text) > Val(txtFirstNum.Text) Then<br />

picResult.Print “The larger number is “; txtSecondNum.Text<br />

Else<br />

picResult.Print “The two numbers are equal.”<br />

End If<br />

End Sub<br />

[Run, type 7 into both text boxes, <strong>and</strong> press the comm<strong>and</strong> button.]<br />

If blocks allow us to define functions whose values are not determined by a simple formula.<br />

The function in Example 6 uses an If block.<br />

EXAMPLE 6<br />

The Social Security or FICA tax has two components—the Social Security benefits tax, which in 1999 is<br />

6.2 percent on the first $72,600 of earnings for the year, <strong>and</strong> the Medicare tax, which is 1.45 percent of<br />

earnings. Write a program to calculate an employee’s FICA tax for the current pay period.<br />

SOLUTION:<br />

Object Property Setting<br />

frmFICA Caption FICA Taxes<br />

lblToDate Caption Total earnings for this year prior to the<br />

current pay period<br />

Alignment Right Justify<br />

txtToDate Text (blank)<br />

lblCurrent Caption Earnings for the current pay period<br />

Alignment Right Justify<br />

txtCurrent Text (blank)<br />

cmdCalculate Caption Calculate FICA Taxes<br />

picTax<br />

Private Sub cmdCalculate_Click()<br />

Dim FicaTaxes As Single<br />

FicaTaxes = FICA(Val(txtToDate.Text), Val(txtCurrent.Text))

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!