# operations

Document Sample

```					              VB Operators
• VB uses many of the operators common to
other programming languages
• It has the typical math, string, conditional,
logical, and compound operators
Operators
•   = : assignment
•   - : subtraction
•   * : multiplication
Operators
•   / : floating point division
•   \ : integer division
•   ^ : exponentiation
•   Mod : modulus (remainder)
•   & : string concatenation
Compound Operators
•   -= : subtraction assignment
•   *= : multiplication assignment
•   /= : floating point division assignment
•   \= : integer division assignment
•   ^= : exponentiation assignment
•   &= : string concatenation assignment
Conditional Operators
•   < : less than
•   > : greater than
•   = : equality
•   <> : inequality
•   <= : less than or equal to
•   >= : greater than or equal to
Logical Operators
• And : and
• Or : or
• AndAlso : a short-circuit And
• OrElse : a short-circuit Or
• Xor : exclusive Or
• Not : negation
Sample Arithmetic Operators
•   9 / 7 → 1.28571428571429 (float division)
•   9 \ 7 → 1 (integer division)
•   9 Mod 7 → 2
•   2.5 Mod 1.2 → 0.1
•   3^2→9
•   2.5 ^ 1.2 → 3.00281108495358
Sample String Operators
• str1 = "Hello"
str2 = "World"
str3 = str1 & " " & str2
(str3 → "Hello World")
Sample Compound Operators
• x=5
x *= 2 (x → 10)
• y=8
y \= 2 (y → 4)
• str1 = "Hello"
str1 &= " World" (str1 → "Hello World")
Sample Logical Operators
• The difference between And and AndAlso
is subtle but important. And evaluates all
conditions in an expression. AndAlso is a
short circuit version of And: if the condition
n in an expression is False, it does not
bother to evaluate condition n+1.
Sample Logical Operators (Cont'd)
• In this example AndAlso works correctly,
but And would fail. If the 1st condition fails,
the 2nd condition is not evaluated, which it
shouldn't be if objA is not given a value.
Dim objA As Object
…
If (Not (objA Is Nothing) AndAlso
objA.GetType().ToString = "System.Object") Then
…
End If
Sample Logical Operators (Cont'd)
• The difference between Or and OrElse is
also subtle but less important. Or
evaluates all conditions in an expression.
OrElse is a short circuit version of Or: if
the condition n in an expression is True, it
does not bother to evaluate condition n+1.
• This does not lead to the same problems
as with And and AndAlso.

```
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
 views: 6 posted: 8/5/2012 language: English pages: 12