下表显示了VB.Net支持的所有逻辑运算符。 假设变量A保持布尔值True,变量B保持布尔值False,则:
运算符 | 描述 | 例 |
---|---|---|
And | 它是逻辑以及按位AND运算符。 如果两个操作数都为真,则条件为真。 此运算符不执行短路,即,它评估两个表达式。 | (A和B)为假。 |
Or | 它是逻辑以及按位或运算符。 如果两个操作数中的任何一个为真,则条件为真。 此运算符不执行短路,即,它评估两个表达式。 | (A或B)为真。 |
Not | 它是逻辑以及按位非运算符。 用于反转其操作数的逻辑状态。 如果条件为真,则逻辑非运算符将为假。 | 没有(A和B)为真。 |
Xor | 它是逻辑以及按位逻辑异或运算符。 如果两个表达式都为True或两个表达式都为False,则返回True; 否则返回False。 该运算符不会执行短路,它总是评估这两个表达式,并且没有该运算符的短路对应。 | 异或B为真。 |
AndAlso | 它是逻辑 AND 运算符。它仅适用于布尔型数据。它执行短路。 | (A AndAlso运算B)为假。 |
OrElse | 它是逻辑或运算符。 它只适用于布尔数据。 它执行短路。 | (A OrElse运算B)为真。 |
IsFalse | 它确定表达式是否为假。 | |
IsTrue | 它确定表达式是否为真。 |
尝试以下示例来了解VB.Net中提供的所有逻辑/按位运算符:
Dim a As Boolean = True Dim b As Boolean = True Dim c As Integer = 5 Dim d As Integer = 20 '逻辑运算 And, Or and Xor Checking If (a And b) Then Proj.MsgDebug.Add("Line 1 - Condition is true") End If If (a Or b) Then Proj.MsgDebug.Add("Line 2 - Condition is true") End If If (a Xor b) Then Proj.MsgDebug.Add("Line 3 - Condition is true") End If '位运算 And, Or and Xor Checking If (c And d) Then Proj.MsgDebug.Add("Line 4 - Condition is true") End If If (c Or d) Then Proj.MsgDebug.Add("Line 5 - Condition is true") End If If (c Or d) Then Proj.MsgDebug.Add("Line 6 - Condition is true") End If 'Only logical operators If (a AndAlso b) Then Proj.MsgDebug.Add("Line 7 - Condition is true") End If If (a OrElse b) Then Proj.MsgDebug.Add("Line 8 - Condition is true") End If ' lets change the value of a and b a = False b = True If (a And b) Then Proj.MsgDebug.Add("Line 9 - Condition is true") Else Proj.MsgDebug.Add("Line 9 - Condition is not true") End If If (Not (a And b)) Then Proj.MsgDebug.Add("Line 10 - Condition is true") End If '返回结果: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 |