20
VB Operators Lecture Outline: a) Arithmetic Operators b) Comparison Operators b) Comparison Operators c) Logical/Bitwise Operators d) Bit Shift Operators e) Assignment Operators f) Miscellaneous Operators g) Operator Precedence

VB Operators - Chinhoyi University of Technology · VB Operators Lecture Outline: a ... Console.ReadKey() End Sub End Module. Example 2 Dim x As New customer() ... Console.WriteLine("

Embed Size (px)

Citation preview

Page 1: VB Operators - Chinhoyi University of Technology · VB Operators Lecture Outline: a ... Console.ReadKey() End Sub End Module. Example 2 Dim x As New customer() ... Console.WriteLine("

VB Operators

Lecture Outline:

a) Arithmetic Operatorsb) Comparison Operatorsc) Logical/Bitwise Operatorsd) Bit Shift Operatorse) Assignment Operatorsf) Miscellaneous Operatorsg) Operator Precedence

Lecture Outline:

a) Arithmetic Operatorsb) Comparison Operatorsc) Logical/Bitwise Operatorsd) Bit Shift Operatorse) Assignment Operatorsf) Miscellaneous Operatorsg) Operator Precedence

Page 2: VB Operators - Chinhoyi University of Technology · VB Operators Lecture Outline: a ... Console.ReadKey() End Sub End Module. Example 2 Dim x As New customer() ... Console.WriteLine("

a) Arithmetic Operators

Operator Mathematical function Example^ Exponential 2^4=16* Multiplication 4*3=12/ Division (Floating point result) 12/4=3Mod Modulus(return remainder from integer division)15 Mod 4=3\ Integer Division(discards the decimal places) 19\4=4+ or & String concatenation "Visual"&"Basic"="Visual Basic"

Operator Mathematical function Example^ Exponential 2^4=16* Multiplication 4*3=12/ Division (Floating point result) 12/4=3Mod Modulus(return remainder from integer division)15 Mod 4=3\ Integer Division(discards the decimal places) 19\4=4+ or & String concatenation "Visual"&"Basic"="Visual Basic"

Page 3: VB Operators - Chinhoyi University of Technology · VB Operators Lecture Outline: a ... Console.ReadKey() End Sub End Module. Example 2 Dim x As New customer() ... Console.WriteLine("

ExamplePrivate Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

Dim number1, number2, number3, number4, number5, total As IntegerDim firstName, secondName, yourName As StringDim average As DecimalfirstName = TextBox3.TextsecondName = TextBox4.TextyourName = firstName + secondNamenumber1 = Val(TextBox1.Text)number2 = Val(TextBox2.Text)number3 = number1 * (number2 ^ 3)number4 = number3 Mod 2number5 = number4 \ number1total = number1 + number2 + number3 + number4 + number5average = total / 5nameTxt.Text = yourNametotalTxt.Text = totalaverageTxt.Text = average

End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

Dim number1, number2, number3, number4, number5, total As IntegerDim firstName, secondName, yourName As StringDim average As DecimalfirstName = TextBox3.TextsecondName = TextBox4.TextyourName = firstName + secondNamenumber1 = Val(TextBox1.Text)number2 = Val(TextBox2.Text)number3 = number1 * (number2 ^ 3)number4 = number3 Mod 2number5 = number4 \ number1total = number1 + number2 + number3 + number4 + number5average = total / 5nameTxt.Text = yourNametotalTxt.Text = totalaverageTxt.Text = average

End Sub

Page 4: VB Operators - Chinhoyi University of Technology · VB Operators Lecture Outline: a ... Console.ReadKey() End Sub End Module. Example 2 Dim x As New customer() ... Console.WriteLine("

Example 2

Dim a As Single, b As Doublea = 1 / 3a = a * 100000Console.WriteLine(a)b = 1 / 3Console.WriteLine(b)b = b * 100000Console.WriteLine(b)

Dim a As Single, b As Doublea = 1 / 3a = a * 100000Console.WriteLine(a)b = 1 / 3Console.WriteLine(b)b = b * 100000Console.WriteLine(b)

Page 5: VB Operators - Chinhoyi University of Technology · VB Operators Lecture Outline: a ... Console.ReadKey() End Sub End Module. Example 2 Dim x As New customer() ... Console.WriteLine("

b) Comparison Operators

Page 6: VB Operators - Chinhoyi University of Technology · VB Operators Lecture Outline: a ... Console.ReadKey() End Sub End Module. Example 2 Dim x As New customer() ... Console.WriteLine("
Page 7: VB Operators - Chinhoyi University of Technology · VB Operators Lecture Outline: a ... Console.ReadKey() End Sub End Module. Example 2 Dim x As New customer() ... Console.WriteLine("

ExampleModule Module1

Sub Main()Dim a = 2, b = 7, c = -1, d = 9, e

As Integer

If (a < 5) ThenConsole.WriteLine("A is less

than 5")End If

If (b = 7) ThenConsole.WriteLine("B is

equal to 7")End If

If (c <= 0) ThenConsole.WriteLine("C is less

than 0")End If

If (d <> 0) ThenConsole.WriteLine("D is not

equal to 0")End If

Console.ReadKey()

End Sub

End Module

Module Module1

Sub Main()Dim a = 2, b = 7, c = -1, d = 9, e

As Integer

If (a < 5) ThenConsole.WriteLine("A is less

than 5")End If

If (b = 7) ThenConsole.WriteLine("B is

equal to 7")End If

If (c <= 0) ThenConsole.WriteLine("C is less

than 0")End If

If (d <> 0) ThenConsole.WriteLine("D is not

equal to 0")End If

Console.ReadKey()

End Sub

End Module

Page 8: VB Operators - Chinhoyi University of Technology · VB Operators Lecture Outline: a ... Console.ReadKey() End Sub End Module. Example 2 Dim x As New customer() ... Console.WriteLine("

Example 2

Dim x As New customer()Dim y As New customer()If x Is y

Then ' Insert code to run if x and y point to the sameinstance.

End If

Dim a As New classA()Dim b As New classB()If a IsNot b

Then' Insert code to run if a and b point to different instances.

End If

Dim x As New customer()Dim y As New customer()If x Is y

Then ' Insert code to run if x and y point to the sameinstance.

End If

Dim a As New classA()Dim b As New classB()If a IsNot b

Then' Insert code to run if a and b point to different instances.

End If

Page 9: VB Operators - Chinhoyi University of Technology · VB Operators Lecture Outline: a ... Console.ReadKey() End Sub End Module. Example 2 Dim x As New customer() ... Console.WriteLine("

c) Logical Operators

Page 10: VB Operators - Chinhoyi University of Technology · VB Operators Lecture Outline: a ... Console.ReadKey() End Sub End Module. Example 2 Dim x As New customer() ... Console.WriteLine("
Page 11: VB Operators - Chinhoyi University of Technology · VB Operators Lecture Outline: a ... Console.ReadKey() End Sub End Module. Example 2 Dim x As New customer() ... Console.WriteLine("

ExampleSub Main()

Dim a As Boolean = TrueDim b As Boolean = TrueDim c As Integer = 5Dim d As Integer = 20'logical And, Or and Xor CheckingIf (a And b) Then

Console.WriteLine("Line 1 - Condition is true")End IfIf (a Or b) Then

Console.WriteLine("Line 2 - Condition is true")End IfIf (a Xor b) Then

Console.WriteLine("Line 3 - Condition is true")End If'bitwise And, Or and Xor CheckingIf (c And d) Then

Console.WriteLine("Line 4 - Condition is true")End IfIf (c Or d) Then

Console.WriteLine("Line 5 - Condition is true")End IfIf (c Xor d) Then

Console.WriteLine("Line 6 - Condition is true")End If'Only logical operators

If (a AndAlso b) ThenConsole.WriteLine("Line 7 - Condition is true")

End IfIf (a OrElse b) Then

Console.WriteLine("Line 8 - Condition is true")End If

' lets change the value of a and ba = Falseb = TrueIf (a And b) Then

Console.WriteLine("Line 9 - Condition is true")Else

Console.WriteLine("Line 9 - Condition is nottrue")End IfIf (Not (a And b)) Then

Console.WriteLine("Line 10 - Condition is true")End IfConsole.ReadLine()

End Sub

Sub Main()Dim a As Boolean = TrueDim b As Boolean = TrueDim c As Integer = 5Dim d As Integer = 20'logical And, Or and Xor CheckingIf (a And b) Then

Console.WriteLine("Line 1 - Condition is true")End IfIf (a Or b) Then

Console.WriteLine("Line 2 - Condition is true")End IfIf (a Xor b) Then

Console.WriteLine("Line 3 - Condition is true")End If'bitwise And, Or and Xor CheckingIf (c And d) Then

Console.WriteLine("Line 4 - Condition is true")End IfIf (c Or d) Then

Console.WriteLine("Line 5 - Condition is true")End IfIf (c Xor d) Then

Console.WriteLine("Line 6 - Condition is true")End If'Only logical operators

If (a AndAlso b) ThenConsole.WriteLine("Line 7 - Condition is true")

End IfIf (a OrElse b) Then

Console.WriteLine("Line 8 - Condition is true")End If

' lets change the value of a and ba = Falseb = TrueIf (a And b) Then

Console.WriteLine("Line 9 - Condition is true")Else

Console.WriteLine("Line 9 - Condition is nottrue")End IfIf (Not (a And b)) Then

Console.WriteLine("Line 10 - Condition is true")End IfConsole.ReadLine()

End Sub

Page 12: VB Operators - Chinhoyi University of Technology · VB Operators Lecture Outline: a ... Console.ReadKey() End Sub End Module. Example 2 Dim x As New customer() ... Console.WriteLine("

d) BitShift Operators• Perform the shift operations on binary values.

Page 13: VB Operators - Chinhoyi University of Technology · VB Operators Lecture Outline: a ... Console.ReadKey() End Sub End Module. Example 2 Dim x As New customer() ... Console.WriteLine("

ExampleSub Main()

Dim a As Integer = 60 ' 60 = 0011 1100Dim b As Integer = 13 ' 13 = 0000 1101Dim c As Integer = 0c = a And b ' 12 = 0000 1100Console.WriteLine("Line 1 - Value of c is {0}", c)c = a Or b ' 61 = 0011 1101Console.WriteLine("Line 2 - Value of c is {0}", c)c = a Xor b ' 49 = 0011 0001Console.WriteLine("Line 3 - Value of c is {0}", c)c = Not a ' -61 = 1100 0011Console.WriteLine("Line 4 - Value of c is {0}", c)c = a << 2 ' 240 = 1111 0000Console.WriteLine("Line 5 - Value of c is {0}", c)c = a >> 2 ' 15 = 0000 1111Console.WriteLine("Line 6 - Value of c is {0}", c)Console.ReadLine()

Sub Main()Dim a As Integer = 60 ' 60 = 0011 1100Dim b As Integer = 13 ' 13 = 0000 1101Dim c As Integer = 0c = a And b ' 12 = 0000 1100Console.WriteLine("Line 1 - Value of c is {0}", c)c = a Or b ' 61 = 0011 1101Console.WriteLine("Line 2 - Value of c is {0}", c)c = a Xor b ' 49 = 0011 0001Console.WriteLine("Line 3 - Value of c is {0}", c)c = Not a ' -61 = 1100 0011Console.WriteLine("Line 4 - Value of c is {0}", c)c = a << 2 ' 240 = 1111 0000Console.WriteLine("Line 5 - Value of c is {0}", c)c = a >> 2 ' 15 = 0000 1111Console.WriteLine("Line 6 - Value of c is {0}", c)Console.ReadLine()

Page 14: VB Operators - Chinhoyi University of Technology · VB Operators Lecture Outline: a ... Console.ReadKey() End Sub End Module. Example 2 Dim x As New customer() ... Console.WriteLine("

e) Assignment Operators

Page 15: VB Operators - Chinhoyi University of Technology · VB Operators Lecture Outline: a ... Console.ReadKey() End Sub End Module. Example 2 Dim x As New customer() ... Console.WriteLine("
Page 16: VB Operators - Chinhoyi University of Technology · VB Operators Lecture Outline: a ... Console.ReadKey() End Sub End Module. Example 2 Dim x As New customer() ... Console.WriteLine("

ExampleSub Main()

Dim a As Integer = 21Dim pow As Integer = 2Dim str1 As String = "Hello! "Dim str2 As String = "VB

Programmers"Dim c As Integerc = aConsole.WriteLine("Line 1 - =

Operator Example, Value of c = {0}", c)c += aConsole.WriteLine("Line 2 - +=

Operator Example,Value of c = {0}", c)c -= aConsole.WriteLine("Line 3 - -=

Operator Example,Value of c = {0}", c)c *= aConsole.WriteLine("Line 4 - *=

Operator Example,Value of c = {0}", c)c /= a

Console.WriteLine("Line 5 - /=Operator Example,Value of c = {0}", c)c = 20c ^= powConsole.WriteLine("Line 6 - ^=

Operator Example,Value of c = {0}", c)c <<= 2Console.WriteLine("Line 7 - <<=

Operator Example,Value of c = {0}", c)c >>= 2Console.WriteLine("Line 8 - >>=

Operator Example,Value of c = {0}", c)str1 &= str2Console.WriteLine("Line 9 - &=

Operator Example,Value of str1 = {0}",str1)Console.ReadLine()

End Sub

Sub Main()Dim a As Integer = 21Dim pow As Integer = 2Dim str1 As String = "Hello! "Dim str2 As String = "VB

Programmers"Dim c As Integerc = aConsole.WriteLine("Line 1 - =

Operator Example, Value of c = {0}", c)c += aConsole.WriteLine("Line 2 - +=

Operator Example,Value of c = {0}", c)c -= aConsole.WriteLine("Line 3 - -=

Operator Example,Value of c = {0}", c)c *= aConsole.WriteLine("Line 4 - *=

Operator Example,Value of c = {0}", c)c /= a

Console.WriteLine("Line 5 - /=Operator Example,Value of c = {0}", c)c = 20c ^= powConsole.WriteLine("Line 6 - ^=

Operator Example,Value of c = {0}", c)c <<= 2Console.WriteLine("Line 7 - <<=

Operator Example,Value of c = {0}", c)c >>= 2Console.WriteLine("Line 8 - >>=

Operator Example,Value of c = {0}", c)str1 &= str2Console.WriteLine("Line 9 - &=

Operator Example,Value of str1 = {0}",str1)Console.ReadLine()

End Sub

Page 17: VB Operators - Chinhoyi University of Technology · VB Operators Lecture Outline: a ... Console.ReadKey() End Sub End Module. Example 2 Dim x As New customer() ... Console.WriteLine("

f) Miscellaneous Operators

Page 18: VB Operators - Chinhoyi University of Technology · VB Operators Lecture Outline: a ... Console.ReadKey() End Sub End Module. Example 2 Dim x As New customer() ... Console.WriteLine("

ExampleSub Main()

Dim a As Integer = 21Console.WriteLine(GetType(Integer).ToString())Console.WriteLine(GetType(Double).ToString())Console.WriteLine(GetType(String).ToString())Dim multiplywith5 = Function(num As Integer) num * 5Console.WriteLine(multiplywith5(5))Console.WriteLine(If(a >= 0, "Positive", "Negative"))Console.ReadLine()

End Sub

Sub Main()

Dim a As Integer = 21Console.WriteLine(GetType(Integer).ToString())Console.WriteLine(GetType(Double).ToString())Console.WriteLine(GetType(String).ToString())Dim multiplywith5 = Function(num As Integer) num * 5Console.WriteLine(multiplywith5(5))Console.WriteLine(If(a >= 0, "Positive", "Negative"))Console.ReadLine()

End Sub

Page 19: VB Operators - Chinhoyi University of Technology · VB Operators Lecture Outline: a ... Console.ReadKey() End Sub End Module. Example 2 Dim x As New customer() ... Console.WriteLine("

g) Operator Precedence

Page 20: VB Operators - Chinhoyi University of Technology · VB Operators Lecture Outline: a ... Console.ReadKey() End Sub End Module. Example 2 Dim x As New customer() ... Console.WriteLine("

ExampleSub Main()

Dim a As Integer = 20Dim b As Integer = 10Dim c As Integer = 15Dim d As Integer = 5Dim e As Integere = (a + b) * c / d ' ( 30 * 15 ) / 5Console.WriteLine("Value of (a + b) * c / d is : {0}", e)e = ((a + b) * c) / d ' (30 * 15 ) / 5Console.WriteLine("Value of ((a + b) * c) / d is : {0}", e)e = (a + b) * (c / d) ' (30) * (15/5)Console.WriteLine("Value of (a + b) * (c / d) is : {0}", e)e = a + (b * c) / d ' 20 + (150/5)Console.WriteLine("Value of a + (b * c) / d is : {0}", e)Console.ReadLine()

End Sub

Sub Main()Dim a As Integer = 20Dim b As Integer = 10Dim c As Integer = 15Dim d As Integer = 5Dim e As Integere = (a + b) * c / d ' ( 30 * 15 ) / 5Console.WriteLine("Value of (a + b) * c / d is : {0}", e)e = ((a + b) * c) / d ' (30 * 15 ) / 5Console.WriteLine("Value of ((a + b) * c) / d is : {0}", e)e = (a + b) * (c / d) ' (30) * (15/5)Console.WriteLine("Value of (a + b) * (c / d) is : {0}", e)e = a + (b * c) / d ' 20 + (150/5)Console.WriteLine("Value of a + (b * c) / d is : {0}", e)Console.ReadLine()

End Sub