Class Notes: 06/21/06

Assignment: Case Study -Payroll System Phase 4: Decision-Making

4B) Determine an additional tax rate based on marital status:

1) Declare marital status as a number (integer).

    1=Single     2=Married     3=Head of Household

    An additional 5% will be added to the tax rate of s single person subtract 5% if head of household.

2) Declare marital status as character
(working with either letters M, S, H, or character digits 1, 2, 3)

3) Program should accept either upper case or lower case letters for marital status (e.g. accept M or m).

What do we need in order to calculate tax rate?
a) Gross pay
b) Marital Status

(Take a look at page 115 for a similar problem)
Instead of choice we’ll call it marital status by either textbox, inputbox or radio button.

Dim status as integer

status = textbox1.text
if (status = 1) then
msgbox(“YOUR STATUS IS SINGLE”)
else if (status = 2) then
msgbox(“YOUR STATUS IS MARRIED”)
else if (status = 3) then
msgbox(“YOUR STATUS IS HEAD OF HOUSEHOLD”)
else
msgbox(“ERROR IN ENTRY”)

end if


Using variable as “char”

 

Now, if we want to enter uppercase letters for status we will need to add another piece of code.
    Dim status As Char

        status = TextBox1.Text

        If (status = "1") Then

            MsgBox("YOUR STATUS IS SINGLE")

        ElseIf (status = "2") Then

            MsgBox("YOUR STATUS IS MARRIED")

        ElseIf (status = "3") Then

            MsgBox("YOUR STATUS IS HEAD OF HOUSEHOLD")

        ElseIf (status = "s") Then

            MsgBox("YOUR STATUS IS SINGLE")

        ElseIf (status = "m") Then

            MsgBox("YOUR STATUS IS MARRIED")

        ElseIf (status = "h") Then

            MsgBox("YOUR STATUS IS HEAD OF HOUSEHOLD")

 

        ElseIf (status = "S") Then

            MsgBox("YOUR STATUS IS SINGLE")

        ElseIf (status = "M") Then

            MsgBox("YOUR STATUS IS MARRIED")

        ElseIf (status = "H") Then

            MsgBox("YOUR STATUS IS HEAD OF HOUSEHOLD")

        Else

            MsgBox("ERROR IN ENTRY")

        End If

 

Second part of Question # 1
   Dim status As Char

        Dim taxrate As Double

        status = TextBox1.Text

        taxrate = TextBox2.Text

        If (status = "1") Then

            MsgBox("YOUR STATUS IS SINGLE")

            taxrate = taxrate + 0.05

            TextBox2.Text = taxrate

        ElseIf (status = "2") Then

            MsgBox("YOUR STATUS IS MARRIED")

        ElseIf (status = "3") Then

            MsgBox("YOUR STATUS IS HEAD OF HOUSEHOLD")

            taxrate = taxrate - 0.05

            TextBox2.Text = taxrate

        ElseIf (status = "s") Then

            MsgBox("YOUR STATUS IS SINGLE")

        ElseIf (status = "m") Then

            MsgBox("YOUR STATUS IS MARRIED")

        ElseIf (status = "h") Then

            MsgBox("YOUR STATUS IS HEAD OF HOUSEHOLD")

        ElseIf (status = "S") Then[k;

            MsgBox("YOUR STATUS IS SINGLE")

            taxrate = taxrate + 0.05

            TextBox2.Text = taxrate

        ElseIf (status = "M") Then

            MsgBox("YOUR STATUS IS MARRIED")

        ElseIf (status = "H") Then

            MsgBox("YOUR STATUS IS HEAD OF HOUSEHOLD")

            taxrate = taxrate - 0.05

            TextBox2.Text = taxrate

        Else

            MsgBox("ERROR IN ENTRY")

        End If


Example of Select Statement.

Code Taken from: http://www.w3schools.com/vbscript/vbscript_conditionals.asp
Select Case

You can also use the SELECT statement if you want to select one of many blocks of code to execute:

select case payment

 case "Cash"

   msgbox "You are going to pay cash"

 case "Visa"

   msgbox "You are going to pay with visa"

 case "AmEx"

   msgbox "You are going to pay with American Express"

 case Else

   msgbox "Unknown method of payment"

end select

This is how it works: First we have a single expression (most often a variable), that is evaluated once. The value of the expression is then compared with the values for each Case in the structure. If there is a match, the block of code associated with that Case is executed.


 

Another example of Select Case taken from: http://theopensourcery.com/vb05tut.htm

One of the statements that Visual Basic lacks is the Continue statement. There are times in the middle of a loop when you want to stop testing and just go to the next iteration in the loop. One can approximate this with nested Ifs but the logic of the nesting can quickly become quite complicated and obscure to follow. Visual Basic does not have a Continue statement, but its very powerful Select Case conditional statement will fill in quite well in many cases. The best way to show how a Select Case works is to give an example.

Select Case iswitch
Case 1
'Print a short report
itype = "short"
call PrintReport(itype)
Case 2,3,4
'Print a long, detailed report
itype="long"
call LongReport(itype,isw)
Case 5
'Graph the output
call GraphReport()
Else Case
'All other cases or values of iswitch are an error

MsgBox iswitch + " is not expected, Error"
End Select

Array:

Array comes from the word arrangement.