An operator is similar to a mathematical or boolean operator. Operators can be used for arithmetic (mathematical operator) or for comparison (boolean operator).

## Mathematical operators

There are a collection of mathematical operators that perform different tasks. The expressions used to represent such operators are shown in the following table, along with an example of each.

Name | Description | Operator Code | Example | Result |
---|---|---|---|---|

Addition | Simply used to add two numbers together. | `+` |
`5 + 10` |
15 |

Subtract | Used to subtract the second number from the first | `-` |
`10 - 5` |
5 |

Multiply | Used to multiply two numbers | `*` |
`7 * 4` |
28 |

Divide | Used to divide the first number by the second number and return a floating point number | `/` |
`50 / 7` |
7.14... |

Used to divide the first number by the second number and return an integer result | `\` |
`50 / 7` |
7 | |

Raise | Raise a number to another number in the same way as
the mathematical representation x^{y} |
`^` |
`2^7` |
128 |

Modulus | Used to get the remainder of a division as an integer. | `mod` |
`5 mod 4` |
1 |

These operations can be used to return a result as an integer or floating point number.

### Val function

The `Val`

function in VB.NET is a very useful one for casting (converting) values
to integers. It is particularly useful where a string is input, such as
when it is received from an `InputBox`

.

The following example shows how this works:

Dim a As Integer = InputBox("Number 1 please") Dim b As Integer = InputBox("Number 2 please") Dim result As Integer 'Could crash due to a string being inserted result = a + b 'This will convert strings that do not contain numbers to 0 result = Val(a) + Val(b) MsgBox(result)

## Boolean operators

A boolean operation can be used to return a boolean value (True or False). The table below specifies all of these available in VB.NET:

Name | Description | Operator Code | Example (of truth) |
---|---|---|---|

Greater than | True if the first expression is greater than the second expression | > | 7 > 4 |

Less than | True if the first expression is less than the second expression | < | 7 < 16 |

Greater than or equal to | True if the first expression is greater than or equal to the second expression | >= | 17 >= 16 |

Less than or equal to | True if the first expression is less than or equal to the second expression | <= | 16 <= 16 |

Equal to | True if both expressions are equal (in VB.NET this works also for strings) | = | 16 = 16 |

Not equal | True if both expressions are not equal (again, works with strings in VB.NET) | <> | 16 <> 7 |

The results of these operations can be placed in a boolean variable as shown in the following sample:

Dim a As Integer = 5 Dim b As Integer = 9 Dim c As Integer c = a + b - 7 Dim result As Boolean result = c > a MsgBox(result)

The above sample should return True through the use of a message box.

## Logical operators

Boolean logic also uses other operators. These can also be represented in VB.NET and are also known as bitwise operators. Formal logic and specification defines the way these work and VB.NET contains expressions that form statements like the following can be constructed in VB.NET:

x = (a ∧ b) ⊕ (c ∨ d)

The above expression will evaluate true when *a* and *b* are both false and either *c* or *d *are
true or when *a* and *b* are both true and both *c* and *d* are false due to the ⊕ operator.

The following table shows these operators.

Name | Description | Logical symbol | Operator code | Example (of truth) |
---|---|---|---|---|

And | Logical conjunction, true when both expressions are true. |
∧ | `And` |
`True And True` |

Or | Logical disjunction, true when either expressions are true. |
∨ | `Or` |
`True Or False` |

Not | Logical negation, returns true when given a false expression. |
¬ | `Not` |
`Not(False)` |

Exclusive or | Returns true when either expression is true, but not when both are true. |
⊕ | `Xor` |
`True Xor False` |

And also | Short-circuit logical and, true when both expressions are true. |
∧ | `AndAlso` |
`True AndAlso True` |

Or else | Short-circuit logical or, true when either expression is true. |
∨ | `OrElse` |
`True OrElse False` |

The following is a truth table that represents each of the operators shown above for some P and some Q.

P | Q | ¬P | P∧Q | P∨Q | P⊕Q | P AndAlso Q | P OrElse Q |
---|---|---|---|---|---|---|---|

T | T | F | T | T | F | T | T |

T | F | F | F | T | T | F | T |

F | T | T | F | T | T | F | T |

F | F | T | F | F | F | F | F |

Short-circuit logic is logic which saves time. Basically, the `AndAlso`

and `OrElse`

operators work exactly
like the And and the Or operators, but the difference is that they
follow a system where the code breaks when the first expression
to evaluate false (for `AndAlso`

) or true (for ` OrElse`

) occurs. This is shown here:

True And False And True And True And True

This will return False, as shown by the second expression - but
the program will check every expression anyway. With `AndAlso`

it will get to the first False expression (the second expression)
and return False.

The opposite happens with `OrElse`

. The following
example shows this:

`False Or True Or True Or False Or False`

This will return True, as shown by the second expression - but
the program will check every expression anyway. With `OrElse`

it will get to the first True expression (the second expression)
and return True.

They are very easy way to save system resources.