Activity : Programming Methods | ||
Below is an example of a section of program code (using
Visual Basic) that calculates the total amount of an invoice with VAT
added.
Prices and Quantities are input repeatedly until a rogue value of 0 is input. The first version is an example of bad programming... |
||
Dim
P As Single Dim Q As Integer Dim T, F As Single Total = 0 Do Price = InputBox("Please enter the Price") If P <> 0 Then Q = InputBox("Please enter the Quantity") T = T + (P * Q) End If Loop Until (P = 0) F = T + T * 17.5 / 100
|
This is difficult to read, and difficult to make changes if needed. | |
The 'better' version is shown below... | ||
'Declare
Local variables Dim Price As Single Dim Quantity As Integer Dim Total, FinalTotal, VATAmount As Single 'Declare Constant Const VATRate = 17.5 'Initialise Total to 0 Price = InputBox("Please enter the Price") 'Check for rogue value If Price <> 0 Then Quantity = InputBox("Please enter the Quantity") Total = Total + (Price * Quantity) End If Loop Until (Price = 0) 'Calculate VAT VATAmount = Total * VATRate / 100 'Add VAT to total FinalTotal = Total + VATAmount
|
Longer code, so more typing...but much easier to understand! | |
How many improvements can you spot?
Click on the space below to see if you were right... |
||
|
||
Back |