next up previous
Next: 4. Saving your VisualBasic Up: VisualBasic Previous: 2. Your first VisualBasic


3. Some mathematics (and Comment lines)

We are now ready to get our VisualBasic project to do something useful.

Make sure you are in edit mode with the form highlighted (or "in focus"). (If you are having trouble with this, make sure that you are not in run mode by clicking on the stop button. Then click on the bar which is called Form1(Form).)

Double click on the Command1 button. This should now display the three lines of code:

Private Sub Command1_Click()
Print "Hello Swansea"
End Sub

Edit these lines of code so that it now says:

Private Sub Command1_Click()
' This programme is going to add two numbers
a = 2
b = 4
c = a + b
Print "a+b = "; c
End Sub

Make sure that there is NO Option Explicitline appearing at the start of the programme. (i.e. remove this line if it appears.)

Note that there is a "comment" line in this programme:

' This programme is going to add two numbers

Comment lines are included in programmes to give some guidance to you (and anyone else who reads your programme) about what the programme is doing. It is totally ignored by the computer when it comes to run the programme.

Run this project (i.e. click on the play button), and click on the Command1 button. You should see

a+b = 6

appear at the top of the form.

Lets explore the Basic component a little. Each line in any computer program represents an instruction to the computer. The computer reads one line at a time and performs whatever task is stated on that line. It is analogous to the set of instruction to bake a cake. You start by breaking eggs, then you add flour, then ... etc.

What we have done in the above Basic program is to tell the computer to perform four tasks, one after the other. The first is to assign the number "2" to the variable called a. The second is to assign the number "4" to the variable called b. The third is to calculate what a plus b is, and to assign the answer to the variable called c. The final task is to print the value of c to the screen.

This sounds incredibly straightforward doesn't it?

Computer programs are often used to calculate the values of mathematical expressions. These expressions involve the familiar operations such as addition, multiplication etc. The above example was simply to calculate c=a+b.

Other mathematical operations are listed below:

a - b subtraction
a * b multiplication
a / b division
an raising a to the power n i.e. $a^n$
a Mod b gives the remainder e.g. 5 MOD 2 is 1

You can also use many mathematical functions in the usual way:

Cos(a), Sin(a), Tan(a), Exp(a), Log(a)

Note that the angle a has to be in RADIANS!

Other useful commands are:

Abs(a) takes the absolute value, |a|
Atn(a) returns the arctangent of a, i.e. $tan^{-1}(a)$ in RADIANS!
Sqr(a) takes the square root, $\sqrt{a}$
Sgn(a) returns either +1, -1 or 0
  depending on whether a is positive, negative, or zero
Int(a) returns the integer part of a, i.e. Int(4.8) = 4
  Note though that Int(-4.8) = 5
Rnd returns a "random" number between 0 and 1

CHECKPOINT: Write a program which calculates x in the following expression, and prints its value to the screen. (Note that if it crashes, try increasing the value of $b$.)


\begin{displaymath}
x = \frac{ -b + \sqrt{b^2 - 4 a c} } {2a}
\end{displaymath}

A program is just a list of instructions for the computer to perform. Often these instructions involve numerical manipulation of variables!

You can include comment lines (i.e. information for others to read which explain what the programme is trying to do) by using the single quotation character '


next up previous
Next: 4. Saving your VisualBasic Up: VisualBasic Previous: 2. Your first VisualBasic
Chris Allton 2006-10-27