0% found this document useful (0 votes)
18 views

Lecture 4 Labwork

Uploaded by

Fred Neilly
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Lecture 4 Labwork

Uploaded by

Fred Neilly
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

VB.

NET Operators
In VB.NET programming, the Operator is a symbol that is used to perform various
operations on variables. VB.NET has different types of Operators that help in
performing logical and mathematical operations on data values. The Operator
precedence is used to determine the execution order of different Operators in
the VB.NET programming language

.What is VB.NET Operator?


In VB.NET, operator is a special symbol that tells the compiler to perform the specific
logical or mathematical operation on the data values. The data value itself (which can be
either a variable or a constant) is called an operand, and the Operator performs
various operations on the operand.

For example: In the expression,

3+2-1

The symbol + and - are the Operators, and the 3, 2, and 1 are operands.

Different Types of VB.NET Operators

Following are the different types of Operators available in VB.NET:

o Arithmetic Operators
o Comparison Operators
o Logical and Bitwise Operators
o Bit Shift Operators
o Assignment Operators
o Concatenation Operators
o Miscellaneous Operators

Arithmetic Operators
The Arithmetic Operators in VB.NET, used to perform mathematical operations such
as subtraction, addition, multiplication, division, etc. on the operands in VB.NET.
These are as follows:

1
Arithmetic Operators in VB.NET

Operators Description Example

^ It is an exponentiation Operator that is used to raises Y ^ X (X to


one operand to the power of another operand. the power Y)

+ The addition Operator is used to add numeric data, as X+Y


well as concatenate two string variables.

- It is a subtraction Operator, which is used to subtract X-Y


the second operand from the first operand.

* The multiplication Operator is used to multiply the X*Y


operands

/ It is a division Operator used to divide one operand by X/Y


another operand and returns a floating-point result.

\ It is an integer division Operator, which is similar to X\Y


division Operator, except that it returns an integer
result while dividing one operand to another operand.

Mod It is a modulo (Modulus) Operator, which is used to X Mod Y


divide two operands and returns only a remainder.

Example of Arithmetic Operators in VB.NET:

Arithmetic_Operator.vb

1. Imports System
2. Module Arithmetic_Operator
3. Sub Main()
4. 'Declare a, b And c as integer Data Type()
5. Dim a, b, c As Integer
6. Dim d As Single

2
7. a = 17
8. b=4
9. ' Use of + Operator
10. c=a+b
11. Console.WriteLine(" Sum of a + b is {0}", c)
12.
13. 'Use of - Operator
14. c=a-b
15. Console.WriteLine(" Subtraction of a - b is {0}", c)
16.
17. 'Use of * Operator
18. c=a*b
19. Console.WriteLine(" Multiplication of a * b is {0}", c)
20.
21. 'Use of / Operator
22. d=a/b
23. Console.WriteLine(" Division of a / b is {0}", d)
24.
25. 'Use of \ Operator
26. c=a\b
27. Console.WriteLine(" Similar to division Operator (return only integer value) o
f a - b is {0}", c)
28.
29. 'Use of Mod Operator
30. c = a Mod b
31. Console.WriteLine(" Modulus of a Mod b is {0}", c)
32.
33. 'Use of ^ Operator
34. c=a^b
35. Console.WriteLine(" Power of a ^ b is {0}", c)
36. Console.WriteLine("Press any key to exit...")
37. Console.ReadKey()
38. End Sub
39. End Module

3
Now compile and execute the above program, by pressing the F5 button or Start button
from the Visual Studio; then it shows the following result:

Comparison Operators
As the name suggests, the Comparison Operator is used to compare the value of two
variables or operands for the various condition such as greater, less than or equal, etc.
and returns a Boolean value either true or false based on the condition.

Operator Description Example

= It checks whether the value of the two (A = B)


operands is equal; If yes, it returns a true value,
otherwise it shows False.

<> It is a Non-Equality Operator that checks (A <> B), check Non-


whether the value of the two operands is not Equality
equal; it returns true; otherwise, it shows false.

> A greater than symbol or Operator is used to (A > B); if yes, TRUE,
determine whether the value of the left
operand is greater than the value of the right Else FALSE
operand; If the condition is true, it returns
TRUE; otherwise, it shows FALSE value.

< It is a less than symbol which checks whether (A < B); if the
the value of the left operand is less than the condition is true,
value of the right operand; If the condition is returns TRUE else
true, it returns TRUE; otherwise, it shows FALSE FALSE
value.

4
>= It is greater than equal to which checks two A >= B
conditions whether the first operand is greater
than or equal to the second operand; if yes, it
returns TRUE; otherwise, it shows False.

<= This symbol represents less than equal to A <= B


which determines the first operand is less than
or equal to the second operand, and if the
condition is true, it returns TRUE; otherwise, it
shows FALSE.

Is The Is Operator is used to validate whether the result = obj1 Is obj2


two objects reference the same variable or
object; If the test is true, it returns True;
otherwise, the result is False. In short, it checks
the equality of the objects. An Is Operator is
also used to determine whether the object
refers to a valid object.

IsNot The IsNot Operator is similar to Is Operator, Result = obj1 IsNot


except that the two object references the obj2
different object; if yes, the result is True;
otherwise, the result is False.

Like The Like Operator is used to check the pattern result = string Like
expression of string variable; And if the pattern the pattern, the
matched, the result is True; otherwise, it pattern represents
returns False. the series of
characters used by
Like Operator.

Example of Comparison Operators in VB.NET

Comparison_Operator.vb

1. Imports System
2. Module Comparison_Operator
3. Sub Main()
4. 'declaration of Integer, Object and String Data Type variables
5. Dim x As Integer = 5
5
6. Dim y As Integer = 10
7. Dim Result, obj, obj2 As Object
8. Dim str, str2 As String
9. str = "Apple12345"
10. str2 = "Apple12345"
11. obj = 10
12. obj2 = 20
13.
14. Console.WriteLine(" Program of Comparison Operator")
15. 'Use of > Operator
16. Console.WriteLine(" Output of x > y is {0}", x > y)
17.
18. 'Use of < Operator
19. Console.WriteLine(" Output of x < y is {0}", x < y)
20.
21. 'Use of = Operator
22. Console.WriteLine(" Output of x = y is {0}", x = y)
23.
24. 'Use of <> Operator
25. Console.WriteLine(" Output of x <> y is {0}", x <> y)
26.
27. 'Use of >= Operator
28. Console.WriteLine(" Output of x >= y is {0}", x >= y)
29.
30. 'Use of <= Operator
31. Console.WriteLine(" Output of x <= y is {0}", x <= y)
32.
33. 'Use of Is Operator
34. Result = obj Is obj2
35. Console.WriteLine(" Output of obj Is obj2 is {0}", Result)
36.
37. 'Use of Is Operator
38. Result = obj IsNot obj2
39. Console.WriteLine(" Output of obj IsNot obj2 is {0}", Result)

6
40.
41. 'Use of Like Operator
42. Result = str Like str2
43. Console.WriteLine(" Output of str Like str2 is {0}", Result)
44.
45. Console.WriteLine(" Press any key to exit...")
46. Console.ReadKey()
47. End Sub
48. End Module

Now compile and execute the above code by pressing the F5 button or Start button in
Visual studio, it returns the following output:

Logical and Bitwise Operators


The logical and bitwise Operators work with Boolean (true or false) conditions, and if the
conditions become true, it returns a Boolean value. The following are the logical and
bitwise Operators used to perform the various logical operations such as And, Or, Not,
etc. on the operands (variables). Suppose there are two operand A and B, where A is
True, and B is False.

7
Operator Description Example

And The And Operator represents, whether both the (A And B),
operands are true; the result is True. result = False

Or It is an Or Operator that returns a true value; if (A Or B),


anyone operand is true from both the operands. result = True

Not The Not Operator is used to reverse the logical Not A


condition. For example, if the operand's logic is
True, it reveres the condition and makes it False. Or

Not(A And B)
is True

Xor It is an Exclusive OR Operator that represents, A Xor B is


whether both the expression is true or false, the True
result is True; otherwise, the result is False.

AndAlso It is a logical AND Operator that performs short- A AndAlso B


circuit operation on the variables, and if both = False
the operands are true, the result is True else the
result is False.

OrElse It is a logical OR Operator that perform short- A OrElse B =


circuit operation on Boolean data. If anyone of True
the operand is true, the result is True else the
result is False.

IsFalse The IsFalse Operator is used to determine


whether an expression is False.

IsTrue The IsTrue Operator is used to determine


whether an expression is True.

8
Example of Logical and Bitwise Operator:

Logic_Bitwise.vb

1. Imports System
2. Module Logic_Bitwise
3. Sub Main()
4. Dim A As Boolean = True
5. Dim B As Boolean = False
6. Dim c, d As Integer
7. c = 10
8. d = 20
9.
10. 'Use of And Operator
11. If A And B Then
12. Console.WriteLine(" Operands A And B are True")
13. End If
14.
15. 'Use of Or Operator
16. If A Or B Then
17. Console.WriteLine(" Operands A Or B are True")
18. End If
19.
20. 'Use of Xor Operator
21. If A Xor B Then
22. Console.WriteLine(" Operands A Xor B is True")
23. End If
24.
25. 'Use of And Operator
26. If c And d Then
27. Console.WriteLine(" Operands c And d is True")
28. End If
29.
30. 'Use of Or Operator
31. If c Or d Then

9
32. Console.WriteLine(" Operands c Or d is True")
33. End If
34.
35. 'Use of AndAlso Operator
36. If A AndAlso B Then
37. Console.WriteLine(" Operand A AndAlso B is True")
38. End If
39.
40. 'Use of OrElse Operator
41. If A OrElse B Then
42. Console.WriteLine(" Operand A OrElse B is True")
43. End If
44.
45. 'Use of Not Operator
46. If Not (A And B) Then
47. Console.WriteLine(" Output of Not (A And B) is True")
48. End If
49.
50. Console.WriteLine(" Press any key to exit?")
51. Console.ReadKey()
52. End Sub
53. End Module

Now compile and execute the above code by pressing the F5 button or Start button in
Visual studio, it returns the following output:

10
Bit Shift Operators
The Bit Shit Operators are used to perform the bit shift operations on binary values
either to the right or to the left.

Bit Shift operations in VB.NET

Operator Description

AND The Binary AND Operator are used to copy the common binary bit
in the result if the bit exists in both operands.

OR The Binary OR Operator is used to copy a common binary bit in


the result if the bit found in either operand.

XOR The Binary XOR Operator in VB.NET, used to determine whether a


bit is available to copy in one operand instead of both.

Not The binary NOT Operator is also known as the binary Ones'
Compliment Operator, which is used to flip binary bits. This means
it converts the bits from 0 to 1 or 1 to 0 binary bits.

<< The Binary Left Shift Operator is used to shift the bit to the left
side.

>> The Binary Right Shift Operator is used to shift the bit to the right
side.

Example of Bit Shift Operator in VB.NET:

BitShift_Operator.vb

1. Imports System
2. Module Bitshift_Operator
3. Sub Main()
4. Dim x, y, z As Integer
5. x = 12
6. y = 25
7. Dim a, b As Double

11
8. a = 5 ' a = 5(00000101)
9. b = 9 ' b = 9(00001001)
10.
11. ' Use of And Operator
12. z = x And y
13. Console.WriteLine(" BitShift Operator x And y is {0}", z)
14.
15. 'Use of Or Operator
16. z = x Or y
17. Console.WriteLine(" BitShift Operator x Or y is {0}", z)
18.
19. z = x Xor y
20. Console.WriteLine(" BitShift Operator x Xor y is {0}", z)
21.
22. z = Not y
23. Console.WriteLine(" BitShift Operator Not y is {0}", z)
24.
25. 'Use of << Left-Shift Operator
26. ' Output is 00001010
27. Console.WriteLine(" Bitwise Left Shift Operator - a<<1 = {0}", a << 1)
28.
29. 'Output is 00010010
30. Console.WriteLine(" Bitwise Left Shift Operator - b<<1 = {0}", b << 1)
31.
32. 'Use of >> Right-Shift Operator
33. 'Output is 00000010
34. Console.WriteLine(" Bitwise Right Shift Operator - a>>1 = {0}", a << 1)
35.
36. 'Output is 00000100
37. Console.WriteLine(" Bitwise Right Shift Operator - b>>1 = {0}", a << 1)
38.
39. Console.WriteLine(" Press any key to exit...")
40. Console.ReadKey()
41. End Sub

12
42. End Module

Now compile and execute the above code by pressing the F5 button or Start button in
Visual studio, it returns the following output:

Assignment Operators
The Assignment Operators are used to assign the value to variables in VB.NET.

Assignment Operators in VB.NET

Operator Description Example

= It is a simple assignment Operator used to X = 5, X assign a value


assign a right-side operand or value to a left 5
side operand. X = P + Q, (P + Q)
variables or value
assign to X.

+= An Add AND assignment Operator is used to X += 5, which means


add the value of the right operand to the left X= X+5 ( 5 will add
operand. And the result is assigned to the left and assign to X and
operand. then result saved to
Left X operand)

-= It is a Subtract AND assignment Operator, X -= P, which is same


which subtracts the right operand or value as X = X - P
from the left operand. And then, the result
will be assigned to the left operand.

13
*= It is a Multiply AND assignment Operator, X *= P, which is same
which multiplies the right operand or value as X = X - P
with the left operand. And then, the result
will be assigned to the left operand.

/= It is a Divide AND assignment Operator, X /= P, which is same


which divides the left operand or value with as X = X - P
the right operand. And then, the result will be
assigned to the left operand (in floating-
point).

\= It is a Divide AND assignment Operator, X \= P, which is same


which divides the left operand or value with as X = X - P
the right operand. And then, the result will be
assigned to the left operand (in integer-point
division).

^= It is an expression AND assignment Operator, X ^= P, which is same


which raises the left operand or value to the as X = X ^ P
right operand's power. And then, the result
will be assigned to the left operand.

&= It is a concatenate string assignment Str &= name, which is


Operator used to bind the right-hand string same as Str = Str &
or variable with the left-hand string or name
variable. And then, the result will be assigned
to the left operand.

Example of Assignment Operator in VB.NET:

Assign_Operator.vb

1. Imports System
2. Module Assign_Operator
3. Sub Main()
4. 'Declare variable and b As Integer
5. Dim A As Integer = 5
6. Dim B As Integer
7. Dim Str, name As String

14
8. name = "come"
9. Str = "Wel"
10.
11. 'Use of = Operator
12. B=A
13. Console.WriteLine(" Assign value A to B is {0}", B)
14.
15. 'Use of += Operator
16. B += A
17. Console.WriteLine(" Output of B += A is {0}", B)
18.
19. 'Use of -= Operator
20. B -= A
21. Console.WriteLine(" Output of B -= A is {0}", B)
22.
23. 'Use of *= Operator
24. B *= A
25. Console.WriteLine(" Output of B *= A is {0}", B)
26.
27. 'Use of /= Operator
28. B /= A
29. Console.WriteLine(" Output of B /= A is {0}", B)
30.
31. 'Use of = Operator
32. B \= A
33. Console.WriteLine(" Output of B \= A is {0}", B)
34.
35. 'Use of ^= Operator
36. B ^= A
37. Console.WriteLine(" Output of B ^= A is {0}", B)
38.
39. 'Use of &= Operator
40. Str &= name
41. Console.WriteLine(" Output of Str &= name is {0}", Str)

15
42.
43. Console.WriteLine(" Press any key to exit...")
44. Console.ReadKey()
45. End Sub
46. End Module

Now compile and execute the above code by pressing the F5 button or Start button in
Visual studio, it returns the following output:

Concatenation Operators
In VB.NET, there are two concatenation Operators to bind the operands:

Operator Description Example

& It is an ampersand symbol that is used to bind two or Result = Wel


more operand together. Furthermore, a nonstring & come,
operand can also be concatenated with a string Result =
variable ( but in that case, Option Strict is on). Welcome

+ It is also used to add or concatenate two number or Result = Wel


string. + come,
Result =
Welcome

Example of Concatenation Operators in VB.NET.

MyProgram.vb

16
1. Imports System
2. Module MyProgram
3. Sub Main()
4. Dim str As String = "Wel"
5. Dim str2 As String = "come"
6. Dim str3 As String = " "
7. Dim str4 As String = "to VB.NET"
8. Dim result As String
9. Dim result2 As String
10. result = str & str2
11. Console.WriteLine(" Result = str & str2 gives = {0}", result)
12. result2 = str + str2 + str3 + str4
13. Console.WriteLine(" Result = str + str2 + str3 +str4 gives = {0}", result2.ToStr
ing)
14. Console.ReadLine()
15. End Sub
16. End Module

Now compile and execute the above code by pressing the F5 button or Start button in
Visual studio.

Miscellaneous Operators
There are some important Operator in VB.NET

Operator Description Example

Await An Await Operator is used in Dim output as out = Await


an operand to suspend the AsyncMethodThatReturnsResult()
execution of an asynchronous Await AsyncMethod()
method or lambda expression
until the awaited task
completes.

AddressOf The AddressOf Operator is AddHandler Button2.Click,

17
used to provide a reference to AddressOf Button2_Click
the address of a procedure.

GetType A GetType Operator is used to MsgBox(GetType(String).ToString())


retrieve the type of the
specified object. In addition,
the retrieved object type
provides various information
such as methods, properties,
and events.

Function It defines the lambda Dim mul2 = Function(num As


Expression expression, which declares the Integer) num * 4
parameter and code. A Console.WriteLine(mul2(4))
Lambda expression is a
function that is used to
calculate and return value
without defining the name.

If The If Operator using short Dim a = -4


circuit evaluation to Console.WriteLine(If (a >= 0,
conditionally return a single "Positive", "Negative"))
object value from two defined
object values. The If Operator
can be used with two or three
defined arguments.

Example of Miscellaneous Operators in VB.NET.

Misc_Operator.vb

1. Imports System
2. Module Misc_Operator
3. Sub Main()
4. ' Initialize a variable
5. Dim a As Integer = 50
6. ' GetType of the Defined Type
7. Console.WriteLine(GetType(Double).ToString())
8. Console.WriteLine(GetType(Integer).ToString())

18
9. Console.WriteLine(GetType(String).ToString())
10. Console.WriteLine(GetType(Single).ToString())
11. Console.WriteLine(GetType(Decimal).ToString())
12.
13. 'Use of Function()
14. Dim multiplywith10 = Function(sum As Integer) sum * 10
15. Console.WriteLine(multiplywith10(10))
16. Console.WriteLine(If(a >= 0, "Negative", "Positive"))
17.
18. Console.WriteLine(" Press any key to exit...")
19. Console.ReadLine()
20.
21. End Sub
22. End Module

Now compile and execute the above code by pressing the F5 button or Start button in
Visual studio, it returns the following output:

Operator Precedence in VB.NET


Operator precedence is used to determine the order in which different Operators in a
complex expression are evaluated. There are distinct levels of precedence, and an
Operator may belong to one of the levels. The Operators at a higher level of precedence
are evaluated first. Operators of similar precedents are evaluated at either the left-to-
right or the right-to-left level.

The Following table shows the operations, Operators and their precedence -

19
Operations Operators Precedence

Await Highest

Exponential ^

Unary identity and negation +, -

Multiplication and floating- *, /


point division

Integer division \

Modulus arithmetic Mod

Addition and Subtraction +, -

Arithmetic bit shift <<, >>

All comparison Operators =, <>, <, <=, >, >=,


Is, IsNot, Like,
TypeOf …is

Negation Not

Conjunction And, AndAlso

Inclusive disjunction Or, Else

Exclusive disjunction Xor Lowest

Example of Operator Precedence in VB.NET.

Operator_Precedence.vb

1. Imports System
2. Module Operator_Precedence
3. Sub Main()
4. 'Declare and Initialize p, q, r, s variables
5. Dim p As Integer = 30
6. Dim q As Integer = 15

20
7. Dim r As Integer = 10
8. Dim s As Integer = 5
9. Dim result As Integer
10.
11. Console.WriteLine("Check Operator Precedence in VB.NET")
12. 'Check Operator Precedence
13. result = (p + q) * r / s ' 45 * 10 / 5
14. Console.WriteLine("Output of (p + q) * r / s is : {0}", result)
15.
16. result = (p + q) * (r / s) ' (45) * (10/5)
17. Console.WriteLine("Output of (p + q) * (r / s) is : {0}", result)
18.
19. result = ((p + q) * r) / s ' (45 * 10 ) / 5
20. Console.WriteLine("Output of ((p + q) * r) / s is : {0}", result)
21.
22. result = p + (q * r) / s ' 30 + (150/5)
23. Console.WriteLine("Output of p + (q * r) / s is : {0}", result)
24.
25. result = ((p + q * r) / s) ' ((30 + 150) /5)
26. Console.WriteLine("Output of ((p + q * r) / s) is : {0}", result)
27.
28. Console.WriteLine(" Press any key to exit...")
29. Console.ReadKey()
30. End Sub
31. End Module

Now compile and execute the above code by pressing the F5 button or Start button in
Visual studio, it returns the following output:

21
22

You might also like