VB.Net - 運(yùn)算符優(yōu)先級(jí)

2022-06-02 14:11 更新
運(yùn)算符優(yōu)先級(jí)確定表達(dá)式中的術(shù)語分組。 這會(huì)影響表達(dá)式的計(jì)算方式。 某些運(yùn)營(yíng)商比其他運(yùn)營(yíng)商具有更高的優(yōu)先級(jí); 例如,乘法運(yùn)算符的優(yōu)先級(jí)高于加法運(yùn)算符:
例如,x = 7 + 3 * 2; 這里,x被分配13,而不是20,因?yàn)閛perator *具有比+高的優(yōu)先級(jí),因此它首先乘以3 * 2,然后加到7。

這里,具有最高優(yōu)先級(jí)的運(yùn)算符出現(xiàn)在表的頂部,具有最低優(yōu)先級(jí)的運(yùn)算符出現(xiàn)在底部。 在表達(dá)式中,將首先計(jì)算較高優(yōu)先級(jí)運(yùn)算符。

操作符優(yōu)先級(jí)
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)最低

示例:

以下示例以簡(jiǎn)單的方式演示運(yùn)算符優(yōu)先級(jí):
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
當(dāng)上述代碼被編譯和執(zhí)行時(shí),它產(chǎn)生以下結(jié)果:
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
以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)