On completion of this chapter you will know:
· How to enter simple programme code
· How to run this programme and view the output
· How to interact with the user of the programme
· The meaning and use of computer variables
· The use of the Immediate Window
When dealing with computers, programming means giving the computer instructions on how to perform a particular task. All applications that you work with on a computer such as Word, Excel, Gimp, Inkscape etc are all computer programmes. Of course those programmes are a lot more complex than the simple programmes that you will start with, but by the end of the course you may have an idea on how to tackle one of them. Also programmes such as those mentioned above were not written by one person but by a team of perhaps one hundred programmers. In fact big applications such as those mentioned are strictly not programmes but a group of hundreds of small programmes that interact with each other.
In this course you will start by building very small and simple programmes that will get bigger and more sophisticated as you learn more techniques, until eventually you will have a small business application that will be similar to an accounting application.
The manipulation of variables is at the heart of computer programming. To the non-programmer variables are associated with algebra. A typical algebraic problem would be as follows:
If x = 5, y = 9 and z=3,
what is the value of 2x+3y-5z?
The solution to this of course is that if x is 5 then 2x is 10, if y is 9 then 3y is 27 and if z is 3 then 5z is 15. Thus the expression 2x+3y-5z equates to 10+27-15 which is 22.
We shall look at one more example of algebraic expression. In calculating a person’s pay we multiply the number of hours the person has worked with his hourly rate. Thus if a person has worked x hours and if his hourly rate is y, then his pay is xy. On a particular week if the person has worked 30 hours and if his rate is K8 per hour then we can state the problem as follows:
If x = 30 and y = 8, what is the value of xy?
In this case the solution is the same as above, i.e. if x is 30 and y is 8 then xy is 30 multiplied by 8, which is 240.
As stated earlier, programming a computer involves the manipulation of variables and they are manipulated in exactly the same way as they are manipulated in algebra. This means that we assign a value to each variable in the expression and then perform the calculation specified in that expression.
There are, however, a few differences between how we use variables in algebra and how we use them in programming. Some of the differences are as follows:
1.
In algebra we use a single letter to denote a
variable. The most common letters are x, y, z followed by a, b, c. In programming the
names of the variables can be as long as we want them to be. Thus in our payroll example above, instead of
If x = 30 and y = 8, what is the value of
xy we would have the following: if hours = 30 and rate = 8, what is the
value of hours * rate?
A computer programme would solve the
problem as follows:
|
1 |
Hours = 30 |
|
2 |
Rate = 8 |
|
3 |
Gross = Hours * Rate |
|
4 |
Print Gross |
2. In algebra we assign only numeric values to variables and we don’t care whether they are integers or real numbers. In programming we must be more precise. We must specify whether a variable is to hold an integer or a real number. In fact, order to make better use of the computer’s memory, we can specify whether the integers are long or short and whether the real numbers are double or single precision, or currency. This will be dealt with in more detail in the next chapter.
3. In algebra, variables represent only numeric values. In programming, variables can certainly represent numeric values, but they can also represent text and a variety of other objects such as data stored in databases etc. We need not concern ourselves with this use of variables until later in the course.

Figure 2‑1
In order to start writing a programme you must create a module into which you enter your code. To do this click on Insert in the main menu and from the drop down list click on Module. Your display will now alter to that shown in Figure 2‑1 above. You are now ready to enter your code.
For our first programme we shall use the code shown in Listing 2‑1 below. Type in this code apart from the numbers on the left. Those numbers are not part of the code, they are there as reference for the lines of the code so that when we are explaining the code the lines will be easy to refer to.
Listing 2‑1
|
1 |
Sub
payroll() |
|
2 |
Dim sngHours As Single |
|
3 |
Dim curRate As Currency |
|
4 |
Dim curGross As Currency |
|
5 |
Dim curTax As Currency |
|
6 |
Dim curNet As Currency |
|
7 |
sngHours = 30 |
|
8 |
curRate = 8 |
|
9 |
curGross = sngHours * curRate |
|
10 |
curTax = curGross * 0.25 |
|
11 |
curNet = curGross - curTax |
|
12 |
Debug.Print curGross |
|
13 |
Debug.Print curTax |
|
14 |
Debug.Print curNet |
|
15 |
End
Sub |
When you have finished entering the code, save your work. The next step is to run the programme. To do this simply click anywhere inside the code area and press F5. If you have entered the code correctly then your screen will look as in Figure 2‑2 below.

Figure 2‑2
Let us spend some time examining what we have here. Firstly, in the Code window we have the code almost as we entered it – with one exception. Some words are in blue while the rest are in black. What’s the difference between them?
The words in blue are Sub, Dim, As, Single, Currency, End and Debug.Print. Those are referred to as Reserved Words. They mean something special to Visual Basic and we are allowed to use them only in certain circumstances. We shall discuss this in more detail shortly when we will examine the code itself.
The Project window has altered from what it looked like in Figure 2‑1. It now has a Module folder with one module whose name is Module 1.
The window we most need to look at is the Immediate Window at the bottom right of the main window. (If this window is not visible then click on View/Immediate Window.)
If you have entered the code exactly as it is shown in Listing 2‑1 then this window should show the values 240, 60, 180.
Now let us examine the code itself as shown in Listing 2‑1
The first lines we need to look at are lines 1 and 15. Line 1 has Sub payroll in it. As stated above the word Sub is a reserved word in Visual Basic. It is a short form of the word subroutine, which in programming terms means a group of lines of programme code that are executed together. Every subroutine must have a name, which the user chooses. Since, in our case, we are dealing with a very simple pay application we have named the subroutine payroll. Line 15 has the words End Sub. Clearly this means the end of the subroutine. Thus our entire application is contained within the keywords Sub and End Sub. This means that when we run a subroutine it starts at the line following Sub and it stops at the line immediately before End Sub. Thus the lines in our programme that run at the lines 2 – 14 inclusive.
Lines 2 – 6 all begin with the keyword Dim. This is an abbreviation of the word dimension. These are the lines where we declare our variables. The word Dim indicates to Visual Basic that we are creating a variable. This is followed directly by the name off the variable itself. In line 2 the name is sngHours. The name of the variable is followed by the keyword As, which is itself followed by Single in line 2. Recall that in Chapter 1 we stated that computer variables, as well as having long names, can also have different data types assigned to them. The data type assigned to sngHours is Single. This again is an abbreviation of single precision floating point number. In simple terms it is a real number or a number with decimal places.
Line 3 is similar to line 2 except for the word Currency. For a change it is quite clear what this variable will hold – monetary values. Lines 4, 5 and 6 follow the same logic as lines 2 and 3.
Here we need to say a few words about the convention of
naming variables. Firstly we can give a
variable any name we wish as long as it is not one of the reserved words of
Visual
Basic. However, in order to make the
programme easier to understand we should give the variables meaningful
names. In our case we are dealing with a
payroll application. The data items we
need to know are the hours worked and the hourly rate. Once we know those we can work out the gross
by multiplying the hours by the rate.
Once we know the gross we can work out the tax. Finally once we know the gross and tax we can
work out the net pay. Our variables then
could have been named Hours, Rate, Gross, Tax and Net. There is nothing wrong with this and the
programme would run perfectly well with those names. However to be more precise the variable name
must also specify the type of data that is to be stored in that variable. In the case of Hours the data stored there is
of type Single. For this reason we put the abbreviation sng in front of the
name, thus ending up with the full name of sngHours. The variable to hold the
rate is of type Currency and thus the abbreviation cur must appear at the
front, thus giving the full name of cuRate.
One further point regarding naming of variables. A variable name always starts with a lowercase letter. However, if the name contains more than one word then the first letter of each subsequent word must be capitalised. Hence we have sngHours instead of snghours. Of course we can have as many words in our variable name as we wish. Thus in our case we could have the first two variables named sngHoursWorkedByEmpoyee and curHourlyRateOfEmployee.
Now let us look at the rest of the code. As stated above we need to know the value of the hours and rate if we are to work out the values of the gross, tax and net. Thus in line 7 we give the value of 30 to sngHours while in line 8 we give the value of 8 to curRate. These two lines have exactly the same logic as the algebraic statements:
x = 30
y = 8
Line 9 multiplies the values stored in the variables sngHours and curRate and stores the result in curGross. Again this line has the same logic as z = xy. Based on the values given here z would have the value of 240.
Line 10 multiplies the value stored in curGross by 0.25 and stores the result in curTax. In the case of this example the value stored in curTax should be 60.
Line 11 calculates the net by subtracting the values stored in curTax from the value stored in curGross.
By the time we have reached line 11 the programme has performed all of our calculations for us and has stored those calculations in the appropriate variables. Those variables, however, exist in the computer memory and thus cannot be seen by human eyes. For this reason, before ending the programme, we must show them to the user. Lines 12, 13 and 14 perform this task for us. In line 12 Debug.Print means write something in the Immediate Window. What will be written into this window will be the values after the keywords – curGross in our case.
The same logic applies to lines 13 and 14.
Before leaving this explanation of the code it is important to point out that programme lines are executed in a strict top-to-bottom order. Thus it is important to ensure that the actions we want the programme to do are specified in the correct order. As an example look at the code in Listing 2‑2 below. At first glance it looks very much like Listing 2‑1 but if you compare lines 7, 8 and 9 in both of them you will see the difference. In our version below the value of the gross is calculated before any values have been given to either hours or rate. As we need to know the value of hours and rate before we can calculate the value of the gross lines 8 and 9 must come before line 7.
Listing 2‑2
|
1 |
Sub
payroll() |
|
2 |
Dim sngHours As Single |
|
3 |
Dim curRate As Currency |
|
4 |
Dim curGross As Currency |
|
5 |
Dim curTax As Currency |
|
6 |
Dim curNet As Currency |
|
7 |
curGross = sngHours * curRate |
|
8 |
sngHours = 30 |
|
9 |
curRate = 8 |
|
10 |
curTax = curGross * 0.25 |
|
11 |
curNet = curGross - curTax |
|
12 |
Debug.Print curGross |
|
13 |
Debug.Print curTax |
|
14 |
Debug.Print curNet |
|
15 |
End
Sub |
Copy the code in Listing 2‑1into your code area and run it. Check that it runs correctly and that the output appears in the Immediate Window.
Next change the values 30 and 8 for hours and rate in lines 7 and 8 to other values of your choice. Run the programme again and check the output in the Immediate Window. Repeat this action a number of times, each time with a different set of values for hours and rate. After each running check the Immediate Window.
2. Extend the expression created in 1 above when we learn that the user bought 12 items at K6 each.
4. Two numbers are to be processed as follows: the first is multiplied by 3 and the second multiplied by 4. The two results are then added. Write an algebraic expression for this problem. Extend the expression if we know that the first number is 12 and the second number is 7.
The following exercises are based on problems 1 to 5 above.
6.
Write a programme based on Exercises 1and 2 above. You must declare
three variables, two to hold the values of the amount purchased and the unit
price of each item and a third variable to hold the total charged to the
customer. The programme will,
then, calculate the product of those two values and store the result in the
third variable. Finally the programme
will display the result of the calculation in the Immediate Window using the
Debug.Print command.
Ensure that you use the required conventions for naming the variables.
7. Write a programme based on Exercises 3 above. You must declare four variables, two to hold the values of the three variables and also to hold the result. The programme will, then, calculate the sum of those three variables and store the result in the fourth variable. Finally the programme will display the result of the calculation in the Immediate Window using the Debug.Print command.
8. Write a programme based on exercise 4 above. Follow the same guidelines as for exercises 6 and 7 above.
9. Write a programme based on exercise 5 above. Again follow the same guidelines as for exercises 6 and 7 above
Create a small application for calculating a customer’s total in a store. Assume that the customer buys only one item type but that he may buy as many of that item as he wishes. As an example the customer buys only tins o Diana Tuna, but he may buy as many tins as he wishes.
The programme will ask the user for the amount of items that the customer has purchased and the unit price for those items. The programme will calculate the subtotal by multiplying the amount sold by the unit price. GST is then calculated by multiplying the subtotal by 0.1 – or by dividing the subtotal by 10. The total charged to the customer is then calculated by adding the subtotal and the GST. The values of the subtotal, GST and Total are then printed out in the immediate window.