Vb.net-logical-operators

提供:Dev Guides
移動先:案内検索

VB.Net-論理/ビット演算子

次の表は、VB.Netでサポートされているすべての論理演算子を示しています。 変数Aがブール値Trueを保持し、変数Bがブール値Falseを保持すると仮定します-

Operator Description Example
And It is the logical as well as bitwise AND operator. If both the operands are true, then condition becomes true. This operator does not perform short-circuiting, i.e., it evaluates both the expressions. (A And B) is False.
Or It is the logical as well as bitwise OR operator. If any of the two operands is true, then condition becomes true. This operator does not perform short-circuiting, i.e., it evaluates both the expressions. (A Or B) is True.
Not It is the logical as well as bitwise NOT operator. Used to reverse the logical state of its operand. If a condition is true, then Logical NOT operator will make false. Not(A And B) is True.
Xor It is the logical as well as bitwise Logical Exclusive OR operator. It returns False if both expressions are True or both expressions are False; otherwise, it returns True. This operator does not perform short-circuiting, it always evaluates both expressions and there is no short-circuiting counterpart of this operator A Xor B is True.
AndAlso It is the logical AND operator. It works only on Boolean data. It performs short-circuiting. (A AndAlso B) is False.
OrElse It is the logical OR operator. It works only on Boolean data. It performs short-circuiting. (A OrElse B) is True.
IsFalse It determines whether an expression is False.
IsTrue It determines whether an expression is 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