VB Operators

Assignment Operator
The assignment operator (=) is used to store information. Example: NetWorth = Assets Liabilities

VB Operators
Just like any other programming language, VB uses operators to perform operations like addition, comparison and negation in the program.

There are are three types of Visual Basic Operators:

Arithmetic operators
The simplest operators carry out arithmetic operations. These operators in their order of precedence are:

Parentheses: When placed around expressions, parenthesis change the precedence order since whatever is within them is evaluated first.

To concatentate (join) two strings, use the & symbol or the + symbol: lblTime.Caption = "The time is" & Format(Now, "hh:mm") txtConcatenate.Text = "Join this " + "to this"

Relational Operators
There are six comparison operators in Visual Basic:

The result of a comparison operation is a Boolean value (True or False). Relational (comarison) operators work with selective and / or repetitive control structures. Example 1: private sub cmdOperate_KeyPress(KeyAscii As Integer) Dim x, y, m, n As Integer 'Program to generate even numbers x = 0 while (x < 100) if (x mod 2 = 0) then y = x ^ 2 m = ((x + y) / 2 - 1)\3 print "x = ";x,"y = "; y,"m = ", m:    end if     x = x + 1 wend end sub Example 2: private sub cmdOperate_Click Dim x, y, m, n As Integer 'Program to generate odd and even numbers 'It prints them in different picture boxes x = 300 while (x >= 1) if (x mod 2 <> 0) then picOdd.print x,    else picEven.print x,    end if     x = x + 1 wend end sub

logical operators
Example (OR): private sub cmdOperate_MouseMove Dim x, y, m, n As Integer x = Val(txtXes.Text) y = Val(txtYes.Text) m = InputBox("Enter a value for m", "Enter M") n = InputBox("Enter a value for n", "Enter N") if ((x < y) OR (m = n)) then print "Operation success" end if end sub Example (OR): private sub cmdOperate_Click Dim x, y, m, n As Integer x = Val(txtXes.Text) y = Val(txtYes.Text) m = InputBox("Enter a value for m", "Enter M") n = InputBox("Enter a value for n", "Enter N") while ((x < y) OR (m = n)) print "Operation success" if (m = n) then m = m + 1 end if     x = x + 2 wend end sub

Oder of Precedence
PEMDMDAS Below is the order of precedence of operators.

P - Parenthesis. E - Exponentiation. M & D - Multiplication & Division (same level left most evaluates 1st) M & D - Modulus and Integer Division are on same level A & S - Addition and Subtraction on same level String concatenation opertor (+) String concatenation opertor (&) Relational operators Logical operators and Bitwise operators

Writing VB Satements on a single line
VB Statements normally take up a single line with no terminator. However statements can be stacked by using a colon to separate them (this alows the writing of more than one statement on a single line). Example: StartTime = Now : EndTime = StartTime + 10 (Be careful when stacking statements, especially with If/End If  structures. Because you may not get the desired response.)

Writing a long statement
If a statement is too long, it may be continued to the next line using the continuation character, an underscore (_). Example: Months = "The months worked are" & Log(Final * IntRate / Deposit + 1) _ / Log(1 + IntRate)

Visual Basic Comments
Comments are non executable text that form part of the programs documentation. They are very important during error correction, maintenance and program upgrade /expansion.

Comment statements begin with the keyword Rem or a single quote ('). Rem must appear at the beginning of a line. Single quote (') can occur anywhere on the line. Example: Rem This is a remark ' This is also a remark x = 2 * y ' another way to write a remark or comment As a programmer, you should decide how much to comment your code and where to place the comments. Consider such factors as maintenance, reuse, the users, and the legacy of your code.