Sunday, 6 October 2013

VB Lesson 2: Performing Calculations

Hello again, it has not been long since my last post. Why? Is it because I love Computing so much that I can't keep myself away (perhaps this is the case) or is it because I just condensed most of my previous lessons into one solid three hours of Computing? Whilst the former seems to be true, I must state that the reason for this blog post being so close is due to the latter. So let's get down to business.

Visual Basic Lesson 2:

So when I covered the basics of this lesson I did not have the worksheet and so most of it was me just designing different programs based on the questions Lalin set for me. So I will go through the stuff learnt with Lalin through the set questions and then I will attempt to cover what I have learned through the sheet.

Essentially there is no easier way to learn how to program than by doing! But before you do, you must understand the steps which must be undertaken when trying to write out calculations. These are the steps:

1. Declare variables. Using suitable data types.
e.g Dim total As Single = 0
Dim marks1 As Integer
Dim marks2 As Integer
2. Get input from user. Using InputBox usually.
e.g. marks1 = InputBox("How many marks were scored in first test?")
marks2 = InputBox("How many marks were scored in second test?")
3. Calculate values. 
e.g. total = marks1 + marks2
4. Display end values. Usually with a MsgBox or a Text Box.
e.g. MsgBox("Total: " & total)

If you follow the steps in green that's half the battle and you shouldn't go wrong.

Some things to note:
When doing calculations your total should be set to whichever number you want it to start at, which for the most part should be 0.

For the rest of that lesson I just tried out some of Lalin's questions. 

My first program was used to add numbers together. The adding program:

The next program was the triangle area program:

There were more programs which I tried out, however they were a mix of lesson 2 and 3 and so I will cover them in my next post.

This was pretty much all I covered for lesson 2 on it's own. However there is more to understand when it comes to calculations in VB.

Whilst performing calculations you obviously use Maths, therefore in order to use Maths, you must have symbols which you can use in your code in order to perform Mathematical functions. These symbols are known as Arithmetic Operators or Relational Operators.

The Arithmetic Operators are:
+ add
- subtract
* multiply
/ divide
^ to the power of

The Relational Operators are:
= equals (only in calculations)
<> not equals
> greater than 
>= greater than or equal to
< less than
<= less than or equal to

Such as with Maths there is obviously an order in which the program will use operators. For example in standard Maths, BODMAS or BIDMAS applies. It is the same with programming. Precedence is the order in which operators are applied. The precedence in programming is:

Brackets ^ */ +- Not And Or = <> > >= < <=
(If they are the same colour it does not matter which is worked out first.)

It is essentially the same as BOD/BIDMAS, but with a few extra terms.
e.g. the 'Not', the 'And' and the 'Or'.

This is because there is another type of operator. The logical operators, which are not found in Maths. There are only three to know, but they are essential.

Logical Operators:
And - Both must be true.
Or - Either or both must be true.
Not - Not true.

I will use an examples of all the operators in future lessons.

I hope that has covered lesson 2 in enough detail. See you next time.


No comments:

Post a Comment