Lecture 4 Labwork
Lecture 4 Labwork
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
3+2-1
The symbol + and - are the Operators, and the 3, 2, and 1 are operands.
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
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.
> 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.
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.
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:
7
Operator Description Example
And The And Operator represents, whether both the (A And B),
operands are true; the result is True. result = False
Not(A And B)
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.
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.
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.
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.
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.
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:
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
17
used to provide a reference to AddressOf Button2_Click
the address of a procedure.
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:
The Following table shows the operations, Operators and their precedence -
19
Operations Operators Precedence
Await Highest
Exponential ^
Integer division \
Negation Not
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