课程表

VB.Net基本教程

VB.Net高级教程

工具箱
速查手册

VB.Net - 运算符优先级

当前位置:免费教程 » 程序设计 » 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)最低

示例:

以下示例以简单的方式演示运算符优先级:
  1. Module assignment
  2. Sub Main()
  3. Dim a As Integer = 20
  4. Dim b As Integer = 10
  5. Dim c As Integer = 15
  6. Dim d As Integer = 5
  7. Dim e As Integer
  8. e = (a + b) * c / d ' ( 30 * 15 ) / 5
  9. Console.WriteLine("Value of (a + b) * c / d is : {0}", e)
  10. e = ((a + b) * c) / d ' (30 * 15 ) / 5
  11. Console.WriteLine("Value of ((a + b) * c) / d is : {0}", e)
  12. e = (a + b) * (c / d) ' (30) * (15/5)
  13. Console.WriteLine("Value of (a + b) * (c / d) is : {0}", e)
  14. e = a + (b * c) / d ' 20 + (150/5)
  15. Console.WriteLine("Value of a + (b * c) / d is : {0}", e)
  16. Console.ReadLine()
  17. End Sub
  18. End Module

当上述代码被编译和执行时,它产生以下结果:
  1. Value of (a + b) * c / d is : 90
  2. Value of ((a + b) * c) / d is : 90
  3. Value of (a + b) * (c / d) is : 90
  4. Value of a + (b * c) / d is : 50
转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号