Show 例子 7
优质
小牛编辑
128浏览
2023-12-01
运算符优先级确定表达式中的术语分组。 这会影响表达式的计算方式。 某些运算符的优先级高于其他运算符; 例如,乘法运算符的优先级高于加法运算符 -
例如,x = 7 + 3 * 2; 这里,x被赋值为13而不是20,因为operator *的优先级高于+,所以它首先乘以3 * 2然后加到7中。
此处,具有最高优先级的运算符显示在表的顶部,具有最低优先级的运算符显示在底部。 在表达式中,将首先评估更高优先级的运算符。
操作者 | 优先权 |
---|---|
Await | Highest |
Exponentiation (^) | |
一元认同和否定(+, - ) | |
乘法和浮点除法(*,/) | |
Integer division (\) | |
Modulus arithmetic (Mod) | |
加法和减法(+, - ) | |
算术位移(<>) | |
所有比较运算符(=,<>,,> =,Is,IsNot,Like,TypeOf ...是) | |
Negation (Not) | |
Conjunction (And, AndAlso) | |
Inclusive disjunction (Or, OrElse) | |
Exclusive disjunction (Xor) | Lowest |
例子 (Example)
以下示例以简单的方式演示运算符优先级 -
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