运算符是一个符号,通知编译器执行特定的数学或逻辑操作。 VB.Net丰富的内置运算符,并提供以下类型的常用运算符:
算术运算符
比较运算符
逻辑/位运算符
位移位运算符
赋值运算符
其他运算符
本教程将介绍最常用的运算符。
算术运算符
下表显示了VB.Net支持的所有算术运算符。 假设变量A保持2,变量B保持7,则:
运算符 | 描述 | 例 |
---|---|---|
^ | Raises one operand to the power of anothe 将一个操作数作为为另一个的幂 | B^A will give 49 |
+ | Adds two operand 添加两个操作数s | A + B will give 9 |
– | Subtracts second operand from the first 从第一个操作数中减去第二个操作数 | A – B will give -5 |
* | Multiplies both operands 将两个操作数相乘 | A * B will give 14 |
/ | Divides one operand by another and returns a floating point result 将一个操作数除以另一个操作数,并返回一个浮点结果 | B / A will give 3.5 |
Divides one operand by another and returns an integer result 将一个操作数除以另一个操作数,并返回一个整数结果 | B A will give 3 | |
MOD | Modulus Operator and remainder of after an integer division 模数运算符和整数除法后的余数 | B MOD A will give 1 |
示例:
Module operators
Sub Main()
Dim a As Integer = 21
Dim b As Integer = 10
Dim p As Integer = 2
Dim c As Integer
Dim d As Single
c = a + b
Console.WriteLine("Line 1 - Value of c is {0}", c)
c = a - b
Console.WriteLine("Line 2 - Value of c is {0}", c)
c = a * b
Console.WriteLine("Line 3 - Value of c is {0}", c)
d = a / b
Console.WriteLine("Line 4 - Value of d is {0}", d)
c = a \ b
Console.WriteLine("Line 5 - Value of c is {0}", c)
c = a Mod b
Console.WriteLine("Line 6 - Value of c is {0}", c)
c = b ^ p
Console.WriteLine("Line 7 - Value of c is {0}", c)
Console.ReadLine()
End Sub
End Module
Line 1 - Value of c is 31
Line 2 - Value of c is 11
Line 3 - Value of c is 210
Line 4 - Value of d is 2.1
Line 5 - Value of c is 2
Line 6 - Value of c is 1
Line 7 - Value of c is 100
比较运算符
下表显示了VB.Net支持的所有比较运算符。 假设变量A保持10,变量B保持20,则:
运算符 | 描述 | 例 |
---|---|---|
= | Checks if the values of two operands are equal or not; if yes, then condition becomes true. 检查两个操作数的值是否相等; 如果是,则条件变为真。 | (A = B)是不正确的。 |
<> | Checks if the values of two operands are equal or not; if values are not equal, then condition becomes true. 检查两个操作数的值是否相等; 如果值不相等,则条件为真。 | (A<>B)为真。 |
> | Checks if the value of left operand is greater than the value of right operand; if yes, then condition becomes true. 检查左操作数的值是否大于右操作数的值; 如果是,则条件变为真。 | (A> B)是不正确的。 |
< | Checks if the value of left operand is less than the value of right operand; if yes, then condition becomes true. 检查左操作数的值是否小于右操作数的值; 如果是,则条件变为真。 | (A <B)为真。 |
> = | Checks if the value of left operand is greater than or equal to the value of right operand; if yes, then condition becomes true. 检查左操作数的值是否大于或等于右操作数的值; 如果是,则条件变为真。 | (A> = B)是不正确的。 |
<= | Checks if the value of left operand is less than or equal to the value of right operand; if yes, then condition becomes true. 检查左操作数的值是否小于或等于右操作数的值; 如果是,则条件变为真。 | (A <= B)为真。 |
尝试以下示例来了解VB.Net中提供的所有关系运算符:
Module operators
Sub Main()
Dim a As Integer = 21
Dim b As Integer = 10
If (a = b) Then
Console.WriteLine("Line 1 - a is equal to b")
Else
Console.WriteLine("Line 1 - a is not equal to b")
End If
If (a < b) Then
Console.WriteLine("Line 2 - a is less than b")
Else
Console.WriteLine("Line 2 - a is not less than b")
End If
If (a > b) Then
Console.WriteLine("Line 3 - a is greater than b")
Else
Console.WriteLine("Line 3 - a is not greater than b")
End If
' Lets change value of a and b
a = 5
b = 20
If (a <= b) Then
Console.WriteLine("Line 4 - a is either less than or equal to b")
End If
If (b >= a) Then
Console.WriteLine("Line 5 - b is either greater than or equal to b")
End If
Console.ReadLine()
End Sub
End Module
当上述代码被编译和执行时,它产生以下结果:
Line 1 - a is not equal to b
Line 2 - a is not less than b
Line 3 - a is greater than b
Line 4 - a is either less than or equal to b
Line 5 - b is either greater than or equal to b
除了上述情况外,VB.Net提供了三个比较运算符,我们将在以后的章节中使用; 然而,我们在这里给出一个简短的描述。
1、Is运算符 – 它比较两个对象引用变量,并确定两个对象引用是否引用相同的对象,而不执行值比较。 如果object1和object2都引用完全相同的对象实例,则result为True; 否则,result为False。
2、IsNot运算符 – 它还比较两个对象引用变量,并确定两个对象引用是否引用不同的对象。 如果object1和object2都引用完全相同的对象实例,则result为False; 否则,result为True。
3、Like运算符 – 它将字符串与模式进行比较。
逻辑/位运算符
下表显示了VB.Net支持的所有逻辑运算符。 假设变量A保持布尔值True,变量B保持布尔值False,则:
运算符 | 描述 | 例 |
---|---|---|
And | It is the logical as well as bitwise AND operator. If both the operands are true, then condition becomes true. This operator does not perform short-circuiting, i.e., it evaluates both the expressions. 它是逻辑以及按位AND运算符。 如果两个操作数都为真,则条件为真。 此运算符不执行短路,即,它评估两个表达式。 | (A和B)为假。 |
Or | It is the logical as well as bitwise OR operator. If any of the two operands is true, then condition becomes true. This operator does not perform short-circuiting, i.e., it evaluates both the expressions. 它是逻辑以及按位或运算符。 如果两个操作数中的任何一个为真,则条件为真。 此运算符不执行短路,即,它评估两个表达式。 | (A或B)为真。 |
Not | It is the logical as well as bitwise NOT operator. Use to reverses the logical state of its operand. If a condition is true, then Logical NOT operator will make false. 它是逻辑以及按位非运算符。 用于反转其操作数的逻辑状态。 如果条件为真,则逻辑非运算符将为假。 | 没有(A和B)为真。 |
Xor | It is the logical as well as bitwise Logical Exclusive OR operator. It returns True if both expressions are True or both expressions are False; otherwise it returns False. This operator does not perform short-circuiting, it always evaluates both expressions and there is no short-circuiting counterpart of this operator. 它是逻辑以及按位逻辑异或运算符。 如果两个表达式都为True或两个表达式都为False,则返回True; 否则返回False。 该运算符不会执行短路,它总是评估这两个表达式,并且没有该运算符的短路对应。 | 异或B为真。 |
AndAlso | It is the logical AND operator. It works only on Boolean data. It performs short-circuiting. 它是逻辑 AND 运算符。它仅适用于布尔型数据。它执行短路。 | (A AndAlso运算B)为假。 |
OrElse | It is the logical OR operator. It works only on Boolean data. It performs short-circuiting. 它是逻辑或运算符。 它只适用于布尔数据。 它执行短路。 | (A OrElse运算B)为真。 |
IsFalse | It determines whether an expression is False. 它确定表达式是否为假。 | |
IsTrue | It determines whether an expression is True. 它确定表达式是否为真。 |
短路运算:一种逻辑运算规则,我们知道与逻辑运算的算法是只要有一个假就能判定为假,或运算符只要有一个为真就能判定为真。短路运算的算法就是建立在这一基础上。
以与运算为例:当两个表达式前一个表达式判定为假时,第二个表达式不会进行计算,直接返回假
这样的计算被称为短路运算,它可以提高逻辑运算的速度(毕竟有一部分情况只需要计算第一个表达式即可)
但是短路运算也有其缺点,就是不够严谨,特别是部分情况下还是需要运算第二个表达式。所以很多语言都有提供短路版本和非短路版本的与运算和或运算。
尝试以下示例来了解VB.Net中提供的所有逻辑/按位运算符:
Module logicalOp
Sub Main()
Dim a As Boolean = True
Dim b As Boolean = True
Dim c As Integer = 5
Dim d As Integer = 20
'logical And, Or and Xor Checking
If (a And b) Then
Console.WriteLine("Line 1 - Condition is true")
End If
If (a Or b) Then
Console.WriteLine("Line 2 - Condition is true")
End If
If (a Xor b) Then
Console.WriteLine("Line 3 - Condition is true")
End If
'bitwise And, Or and Xor Checking
If (c And d) Then
Console.WriteLine("Line 4 - Condition is true")
End If
If (c Or d) Then
Console.WriteLine("Line 5 - Condition is true")
End If
If (c Or d) Then
Console.WriteLine("Line 6 - Condition is true")
End If
'Only logical operators
If (a AndAlso b) Then
Console.WriteLine("Line 7 - Condition is true")
End If
If (a OrElse b) Then
Console.WriteLine("Line 8 - Condition is true")
End If
' lets change the value of a and b
a = False
b = True
If (a And b) Then
Console.WriteLine("Line 9 - Condition is true")
Else
Console.WriteLine("Line 9 - Condition is not true")
End If
If (Not (a And b)) Then
Console.WriteLine("Line 10 - Condition is true")
End If
Console.ReadLine()
End Sub
End Module
当上述代码被编译和执行时,它产生以下结果:
Line 1 - Condition is true
Line 2 - Condition is true
Line 4 - Condition is true
Line 5 - Condition is true
Line 6 - Condition is true
Line 7 - Condition is true
Line 8 - Condition is true
Line 9 - Condition is not true
Line 10 - Condition is true
位移运算符
我们已经讨论了按位运算符。 位移运算符对二进制值执行移位操作。 在进入位移运算符之前,让我们来了解位操作。
按位运算符处理位并执行逐位操作。 &,|和^的真值表如下:
p | q | p&Q | p | q | p ^ Q |
---|---|---|---|---|
0 | 0 | 0 | 0 | 0 |
0 | 1 | 0 | 1 | 1 |
1 | 1 | 1 | 1 | 0 |
1 | 0 | 0 | 1 | 1 |
假设A = 60; 和B = 13; 现在的二进制格式,他们将如下:
A = 0011 1100
B = 0000 1101
—————–
A&B = 0000 1100
A | B = 0011 1101
A ^ B = 0011 0001
〜A = 1100 0011
我们已经看到VB.Net支持的Bitwise运算符是And,Or,Xor和Not。 位移位算子分别是用于左移和右移的>>和<<。
假设变量A保持60,变量B保持13,则:
运算符 | 描述 | 示例 |
---|---|---|
And | Bitwise AND Operator copies a bit to the result if it exists in both operands. 如果两个操作数都存在,则按位AND运算符将一个位复制到结果。 | (A AND B) will give 12, which is 0000 1100 |
Or | Binary OR Operator copies a bit if it exists in either operand. 二进制OR运算符复制一个位,如果它存在于任一操作数。 | (A Or B) will give 61, which is 0011 1101 |
Xor | Binary XOR Operator copies the bit if it is set in one operand but not both. 二进制XOR运算符复制该位,如果它在一个操作数中设置,但不是两个操作数。 | (A Xor B) will give 49, which is 0011 0001 |
Not | Binary Ones Complement Operator is unary and has the effect of flipping bits. 二进制补码运算符是一元的,具有“翻转”位的效果。 | (Not A ) will give -61, which is 1100 0011 in 2s complement form due to a signed binary number. |
<< | Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand. 二进制左移位运算符。 左操作数值向左移动由右操作数指定的位数。 | A << 2 will give 240, which is 1111 0000 |
>> | Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand. 二进制右移运算符。 左操作数值向右移动由右操作数指定的位数。 | A >> 2 will give 15, which is 0000 1111 |
尝试以下示例来了解VB.Net中提供的所有位运算符:
Module BitwiseOp
Sub Main()
Dim a As Integer = 60 ' 60 = 0011 1100
Dim b As Integer = 13 ' 13 = 0000 1101
Dim c As Integer = 0
c = a And b ' 12 = 0000 1100
Console.WriteLine("Line 1 - Value of c is {0}", c)
c = a Or b ' 61 = 0011 1101
Console.WriteLine("Line 2 - Value of c is {0}", c)
c = a Xor b ' 49 = 0011 0001
Console.WriteLine("Line 3 - Value of c is {0}", c)
c = Not a ' -61 = 1100 0011
Console.WriteLine("Line 4 - Value of c is {0}", c)
c = a << 2 ' 240 = 1111 0000
Console.WriteLine("Line 5 - Value of c is {0}", c)
c = a >> 2 ' 15 = 0000 1111
Console.WriteLine("Line 6 - Value of c is {0}", c)
Console.ReadLine()
End Sub
End Module
当上述代码被编译和执行时,它产生以下结果:
Line 1 - Value of c is 12
Line 2 - Value of c is 61
Line 3 - Value of c is 49
Line 4 - Value of c is -61
Line 5 - Value of c is 240
Line 6 - Value of c is 15
赋值运算符
VB.Net支持以下赋值运算符:
运算符 | 描述 | 例 |
---|---|---|
= | Simple assignment operator, Assigns values from right side operands to left side operand 简单赋值操作符,将值从右侧操作数分配给左侧操作数 | C = A + B A + B将赋值为C |
+ = | Add AND assignment operator, It adds right operand to the left operand and assigns the result to left operand 添加AND赋值运算符,向左操作数添加右操作数,并将结果赋值给左操作数 | C + = A等于C = C + A |
– = | Subtract AND assignment operator, It subtracts right operand from the left operand and assigns the result to left operand 减法AND赋值运算符,它从左操作数中减去右操作数,并将结果赋值给左操作数 | Ç – = A等于C = C – A |
* = | Multiply AND assignment operator, It multiplies right operand with the left operand and assigns the result to left operand 乘法AND赋值运算符,它将右操作数与左操作数相乘,并将结果赋值给左操作数 | C * = A等于C = C * A |
/ = | Divide AND assignment operator, It divides left operand with the right operand and assigns the result to left operand (floating point division) 除法AND赋值运算符,它用右操作数划分左操作数,并将结果分配给左操作数(浮点除法) | C / = A等于C = C / A |
= | Divide AND assignment operator, It divides left operand with the right operand and assigns the result to left operand (Integer division) 除法AND赋值运算符,它用右操作数划分左操作数,并将结果分配给左操作数(整数除法) | ç = A等于C = C A |
^ = | Exponentiation and assignment operator. It raises the left operand to the power of the right operand and assigns the result to left operand. 指数和赋值运算符。 它将左操作数提升为右操作数的幂,并将结果分配给左操作数。 | C ^ = A等于C = C ^ A |
<< = | Left shift AND assignment operator 左移AND赋值运算符 | C语言的<< = 2是同C = C << 2 |
>> = | Right shift AND assignment operator 右移AND赋值运算符 | C >> = 2 >> 2同C = C |
&= | Concatenates a String expression to a String variable or property and assigns the result to the variable or property. 将String表达式连接到String变量或属性,并将结果分配给变量或属性。 | STR1&= STR2赛车是一样的 STR1 = STR1与STR2 |
Module assignment
Sub Main()
Dim a As Integer = 21
Dim pow As Integer = 2
Dim str1 As String = "Hello! "
Dim str2 As String = "VB Programmers"
Dim c As Integer
c = a
Console.WriteLine("Line 1 - = Operator Example, _
Value of c = {0}", c)
c += a
Console.WriteLine("Line 2 - += Operator Example, _
Value of c = {0}", c)
c -= a
Console.WriteLine("Line 3 - -= Operator Example, _
Value of c = {0}", c)
c *= a
Console.WriteLine("Line 4 - *= Operator Example, _
Value of c = {0}", c)
c /= a
Console.WriteLine("Line 5 - /= Operator Example, _
Value of c = {0}", c)
c = 20
c ^= pow
Console.WriteLine("Line 6 - ^= Operator Example, _
Value of c = {0}", c)
c <<= 2
Console.WriteLine("Line 7 - <<= Operator Example,_
Value of c = {0}", c)
c >>= 2
Console.WriteLine("Line 8 - >>= Operator Example,_
Value of c = {0}", c)
str1 &= str2
Console.WriteLine("Line 9 - &= Operator Example,_
Value of str1 = {0}", str1)
Console.ReadLine()
End Sub
End Module
Line 1 - = Operator Example, Value of c = 21
Line 2 - += Operator Example, Value of c = 42
Line 3 - -= Operator Example, Value of c = 21
Line 4 - *= Operator Example, Value of c = 441
Line 5 - /= Operator Example, Value of c = 21
Line 6 - ^= Operator Example, Value of c = 400
Line 7 - <<= Operator Example, Value of c = 1600
Line 8 - >>= Operator Example, Value of c = 400
Line 9 - &= Operator Example, Value of str1 = Hello! VB Programmers
其他运算符
有很少其他重要的操作系统支持VB.Net。
运算符 | 描述 | 例 |
---|---|---|
AddressOf | Returns the address of a procedure. 返回过程的地址。 | AddHandler Button1.Click, AddressOf Button1_Click |
Await | It is applied to an operand in an asynchronous method or lambda expression to suspend execution of the method until the awaited task completes. 它应用于异步方法或lambda表达式中的操作数,以暂停该方法的执行,直到等待的任务完成。 | Dim result As res = Await AsyncMethodThatReturnsResult() Await AsyncMethod() |
GetType | It returns a Type object for the specified type. The Type object provides information about the type such as its properties, methods, and events. 它返回指定类型的Type对象。 Type对象提供有关类型的信息,例如其属性,方法和事件。 | MsgBox(GetType(Integer).ToString()) |
Function Expression | It declares the parameters and code that define a function lambda expression. 它声明定义函数lambda表达式的参数和代码。 | Dim add5 = Function(num As Integer) num + 5 prints 10 Console.WriteLine(add5(5)) |
If | It uses short-circuit evaluation to conditionally return one of two values. The If operator can be called with three arguments or with two arguments. 它使用短路评估有条件地返回两个值之一。 可以使用三个参数或两个参数调用If运算符。 | Dim num = 5 Console.WriteLine(If(num >= 0, "Positive", "Negative")) |
Module assignment
Sub Main()
Dim a As Integer = 21
Console.WriteLine(GetType(Integer).ToString())
Console.WriteLine(GetType(Double).ToString())
Console.WriteLine(GetType(String).ToString())
Dim multiplywith5 = Function(num As Integer) num * 5
Console.WriteLine(multiplywith5(5))
Console.WriteLine(If(a >= 0, "Positive", "Negative"))
Console.ReadLine()
End Sub
End Module
System.Int32
System.Double
System.String
25
Positive
VB.Net中的运算符优先级
运算符优先级确定表达式中的术语分组。 这会影响表达式的计算方式。 某些运算符比其他运算符具有更高的优先级; 例如,乘法运算符的优先级高于加法运算符:
例如,x = 7 + 3 * 2; 这里,x被分配13,而不是20,因为operator *具有比+高的优先级,所以它首先乘以3 * 2,然后加到7。
这里,具有最高优先级的运算符出现在表的顶部,具有最低优先级的运算符出现在底部。 在表达式中,将首先计算较高优先级运算符。
运算符 | 优先级 |
---|---|
Await | 最高 |
Exponentiation (^) | |
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, OrElse) | |
Exclusive disjunction (Xor) | 最低 |
Module assignment
Sub Main()
Dim a As Integer = 20
Dim b As Integer = 10
Dim c As Integer = 15
Dim d As Integer = 5
Dim e As Integer
e = (a + b) * c / d ' ( 30 * 15 ) / 5
Console.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) ' (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
End Module
Value of (a + b) * c / d is : 90
Value of ((a + b) * c) / d is : 90
Value of (a + b) * (c / d) is : 90
Value of a + (b * c) / d is : 50
作者:唐伯虎点蚊香,如若转载,请注明出处:https://www.web176.com/vbnet/11348.html