Visual Basic II   
 

Opp

 

 

 

 

 

 

 

 

 

Visual Basic: the Basics part II



Operators

 

Logical operators

AND = logical and
OR = logical either or
XOR = logical or (not either)
NOT = the negation of the statement

These are the main logical operators in visual basic. There are others, but we will come to them later. It is best to learn about logical operators by example, they are usually used in IF clauses. Each operator is the equivalent of its logical counterpart in the real world (hence the name). Study this example:

Private Sub Form_Load()

Dim First As Integer
Dim Second As Integer
Dim Third As Integer

First = InputBox("Enter a number")
Second = InputBox("Enter another number")
Third = InputBox("One more!")

If First > Second And Second > Third Then
MsgBox("The First number is the biggest")

ElseIf First = 2 OR Second = 2 OR Third = 2 Then
MsgBox ("One of the numbers was 2")
End
Else
MsgBox "Nice numbers"

End
End If
End Sub

This example, in fact, uses both logical and relational operators. The logical being the AND and the OR, the relational being the > sign.

Without running the program, predict the outcome if the numbers entered were 10, 9 and then 6. Implement the program, did you get it right? If not, look back over the program. The computer takes in the three numbers and then moves to the 'if clause', it tests for the first one and sees that, because 10 is the first number and is bigger than 9 and 9 is the second number and is bigger than 6 which is the third number, it fits the specification of the first one perfectly. Therefore it implements it and displays the message.

Can you predict the outcome if the numbers entered were 2, 10 and then 5? Implement the program, did you get it right? If not, look back over the program. The computer takes in the three numbers and moves to the if clause and sees that the first one is false because 2 is not bigger than 10. It then moves to the second one and tests true, because 2 = 2. Therefore it displays the message.

An example of the usage of the XOR operator is:

Private Sub Form_load()
Dim FirstNumber As Integer
Dim SecondNumber As Integer

FirstNumber = InputBox("Enter a number")
SecondNumber = InputBox("Enter another number")

If FirstNumber = 7 XOR SecondNumber = 3 Then
MsgBox("Either the first number was 7, or the second number was 3")
End
Else
MsgBox("Either the first number was not 7 and the second number was not 3. Or the first number was 7 and the second number was 3")
End
End If

End sub

Here, as per usual, the user is asked to input 2 numbers. The computer then checks the two numbers against the argument of the if statement. This argument states that if the first number is 7 or the second number is 3 but not if they are both 7 and 3 respectively, then the computer will execute the block of code which follows. Otherwise, the computer will execute the block of code after the else statement.

Notice the usage of the word End before the end if statement which tells the computer to stop executing the code any further.


Relational operators

The Relational operators are:

< The thing on the left is less than the thing on the right.
> The thing on the left is greater than the thing on the right.
<= The thing on the left is less than or equal to the thing on the right.
>= The thing on the left is greater than or equal to the thing on the right.
= The thing on the left is exactly equal to the thing on the right.

These are simple to use as is displayed in the previous sections of code.


Arithmetic operators

Cast your mind back to my first tutorial; you met your first arithmetic operators: + and =. Here is a list of all the arithmetic operators:

^ exponentiation : the thing on the left to the power of the thing on the right ie 2 ^ 2 = 4
+ addition
- subtraction
= equals: make the thing on the left equal to the thing on the right.
/ Division
Mod Modulus : the computer will perform a division of this kind and only display the remainder
i.e. 10 Mod 3 = 1
\ Integer Division : the computer will perform a division of this kind and only display the whole number part of the answer
i.e. 10 \ 3 = 3
The other thing about division in Visual Basic is that the computer will round up or down before it does the division
i.e. 10.1 / 3.1 = 3 or 10.1 Mod 3.1 = 1 etc.

Order of precedence:

In programming, the computer has an order in which it works through the code in which multiple operators are involved. So in each branch of the operators, there is a table of 'more important' ones that are done first:

Logical Operators:

1. Not
2. And
3. Or
4. XOr

Arithmetic operators:

1. ^
2. *, /
3. \
4. Mod
5. +, -

Comparison operators all have equal precedence and are evaluated in the left to right order in which they appear.

When expressions contain operators from more than one category they are done in this order:

1. Arithmetic operators
2. Comparison operators
3. Logical operators


Well, we have covered operators, you are on your way to becoming a competent programmer...but there is still a way to go. Next time we will be looking at working with the GUI. Happy programming!

Startside ] Opp ] [Søk]

Copyright © 2002 Øyvind Haugland
Sist endret:  13 januar 2019
 

  Interested in this stuff? Please write to:
 

HTML Counter            stats counter