1.            Decisions

Calculating Different Tax Regimes Practice
Exercise Assignment

Learning Outcomes

On completion of this section you shall know:

Download as pdf

Introduction

In all aspects of life we make decisions. Examples are if the PMV’s are running we shall go into town, otherwise we shall study. In programming we have to make decisions as well or to be more precise we shall tell the computer how to make decisions. To demonstrate this we shall modify the way we calculate the tax. In Listing 1‑1 we had a simple flat tax rate of 25%. In real life tax rates are never as simple as this. If the salary is low then the tax rate will also be low but a higher salary will incur higher tax rate. In our case we shall modify the simple payroll programme we met in Chapter 3 so that it will calculate tax in two different ways depending on the value of the gross pay.

Calculating different Tax Regimes

Go to top

In Listing 31 tax was calculated as a flat 25% of the gross. As stated above tax calculation is not quite this simple. Most governments have different levels of taxation depending on a person’s income level. In our case we shall assume that any gross which is K500 or less will be taxed at 25% whereas any gross that exceeds K500 will be tax at 25% of the first K500 and 33% of the remainder. Let us look below at our modified code to see how this is done.

Listing 41

1

Sub Pay()

2

    Dim sngHours As Single

3

    Dim curRate As Currency

4

    Dim curGross As Currency

5

    Dim curTax As Currency

6

    Dim curNett As Currency

7

    sngHours = InputBox("Enter Hours worked")

8

    curRate = InputBox("Enter Rate")

9

    curGross = sngHours * curRate

10

    If curGross <= 500 Then

11

        curTax = curGross * 0.25

12

    Else

13

        curTax = 125 + (curGross - 500) * 0.33

14

    End If

15

    curNett = curGross - curTax

16

    Debug.Print "Gross ", curGross

17

    Debug.Print curTax

18

    Debug.Print curNett

19

End Sub

The code in Listing 41 is the same as that in Listing 31 until we get to line 10. Here we have an if construct which extends as far as line 14. After this the code is, once again, the same as that in Listing 31.

At line 10 the If construct begins with the keyword If. This is followed by the condition that is to be tested – in this case curGross<500.

If we entered 40 for the hours worked and 10 for the hourly rate then curGross would have a value of 400. This would make the condition true since 400 is less than 500, and therefore the body of the If would be executed. This body is simply line 11 and contains only the code for calculating 25% of the gross pay.

On the other hand if the value hours was 40 and of rate was 20 then gross would have a value of 800. In this case the condition at line 10 would be false. The body of the If would therefore be skipped and programme control would jump to line 12 for the Else part of the construct. The body of the Else is simply line 13. Earlier we stated that if the value of the gross was greater than 500 the tax would be 25% of 500 – which is 125 - plus 33% of the remainder. The remainder is calculated as curGross – 500. 33% of this is calculated as (curGross – 500) * .33. Thus the entire calculation is 125 + (curGross – 500) * .33

The bodies of the If and Else can have as many lines of code as are required. They can even contain their own If..Else constructs. In our case the bodies only contain one line of code each.

Notice that lines 11 and 13 are further indented than the rest of the code. This is because line 11 is the body of the If construct and line 13 is the body of the Else construct.

Practice

Go to top

Copy the code in Listing 41into the programme area and run it a number of times.  Use different combinations of hours and rate so that some give a gross less than 500 and others give a gross greater than 500.  Before running the programme each time first check using pen and paper what the values for the gross, tax and net should be, then check that the programme outputs the same results.

Exercise 4

Go to top

 

1.      Explain the different components of the IF..Else..End If construct.

2.      What code gets executed in the body of the If?

3.      What code gets executed in the body of the Else?

4.      In Listing 41 determine which lines form the Input, Process and Output sections

Assignment Part 3

Go to top

Modify the application you have created for Assignment Part 2 so that it can process customer discounts.  If the total is more than K100 then the customer receives a 5% discount otherwise no discount is given.  The discount must be subtracted from the subtotal before the GST is calculated.

 You must add an extra variable to your programme in order to hold the value of the discount.   The value of the discount must be printed along with the other values.