目录

Show 例子

下表显示了VB.Net支持的所有逻辑运算符。 假设变量A保持布尔值True,变量B保持布尔值False,则 -

操作者 描述
And 它是逻辑以及按位AND运算符。 如果两个操作数都为真,则条件成立。 该运算符不执行短路,即它评估两个表达式。 (A和B)是假的。
Or 它是逻辑以及按位OR运算符。 如果两个操作数中的任何一个为真,则条件成立。 该运算符不执行短路,即它评估两个表达式。 (A或B)是真的。
Not 它是逻辑以及按位NOT运算符。 用于反转其操作数的逻辑状态。 如果条件为真,则Logical NOT运算符将为false。 不(A和B)是真的。
Xor 它是逻辑以及按位逻辑异或运算符。 如果两个表达式都为True或两个表达式都为False,则返回False; 否则,它返回True。 该运算符不执行短路,它总是计算两个表达式,并且没有该运算符的短路对应物 Xor B是真的。
AndAlso 它是逻辑AND运算符。 它仅适用于布尔数据。 它执行短路。 (A AndAlso B)是假的。
OrElse 它是逻辑OR运算符。 它仅适用于布尔数据。 它执行短路。 (A OrElse B)是真的。
IsFalse 它确定表达式是否为False。
IsTrue 它确定表达式是否为True。

尝试以下示例以了解VB.Net中可用的所有逻辑/按位运算符 -

Module logicalOp
   Sub Main()
      Dim a As Boolean = True
      Dim b As Boolean = True
      Dim c As Integer = 5
      Dim d As Integer = 20
      'logical And, Or and Xor Checking
      If (a And b) Then
         Console.WriteLine("Line 1 - Condition is true")
      End If
      If (a Or b) Then
          Console.WriteLine("Line 2 - Condition is true")
      End If
      If (a Xor b) Then
          Console.WriteLine("Line 3 - Condition is true")
      End If
        'bitwise And, Or and Xor Checking
      If (c And d) Then
         Console.WriteLine("Line 4 - Condition is true")
      End If
      If (c Or d) Then
         Console.WriteLine("Line 5 - Condition is true")
      End If
      If (c Or d) Then
         Console.WriteLine("Line 6 - Condition is true")
      End If
         'Only logical operators
      If (a AndAlso b) Then
         Console.WriteLine("Line 7 - Condition is true")
      End If
      If (a OrElse b) Then
         Console.WriteLine("Line 8 - Condition is true")
      End If
      ' lets change the value of  a and b 
      a = False
      b = True
      If (a And b) Then
         Console.WriteLine("Line 9 - Condition is true")
      Else
         Console.WriteLine("Line 9 - Condition is not true")
      End If
      If (Not (a And b)) Then
         Console.WriteLine("Line 10 - Condition is true")
      End If
         Console.ReadLine()
   End Sub
End Module

编译并执行上述代码时,会产生以下结果 -

Line 1 - Condition is true
Line 2 - Condition is true
Line 3 - 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
↑回到顶部↑
WIKI教程 @2018