VB.Net - 运算符优先级
2022-06-02 14:11 更新
运算符优先级确定表达式中的术语分组。 这会影响表达式的计算方式。 某些运营商比其他运营商具有更高的优先级; 例如,乘法运算符的优先级高于加法运算符:
例如,x = 7 + 3 * 2; 这里,x被分配13,而不是20,因为operator *具有比+高的优先级,因此它首先乘以3 * 2,然后加到7。
例如,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
以上内容是否对您有帮助:
更多建议: