VB.Net Tutorial/Operator/Logical operators

Материал из VB Эксперт
Перейти к: навигация, поиск

Logical operator: And

Module Tester
    Sub Main()
      Console.WriteLine("And" & vbCrLf & vbCrLf & _
         "False And False: " & (False And False) & vbCrLf & _
         "False And True: " & (False And True) & vbCrLf & _
         "True And False: " & (True And False) & vbCrLf & _
         "True And True: " & (True And True))
    End Sub 
End Module
And
False And False: False
False And True: False
True And False: False
True And True: True

Logical operator: AndAlso

Module Tester
    Sub Main()
      Console.WriteLine("AndAlso" & vbCrLf & vbCrLf & _
         "False AndAlso False: " & (False AndAlso False) & _
         vbCrLf & "False AndAlso True: " & _
         (False AndAlso True) & vbCrLf & _
         "True AndAlso False: " & (True AndAlso False) & _
         vbCrLf & "True AndAlso True: " & (True AndAlso True))
    End Sub 
End Module
AndAlso
False AndAlso False: False
False AndAlso True: False
True AndAlso False: False
True AndAlso True: True

Logical operator: Not

Module Tester
    Sub Main()
      Console.WriteLine("Not" & vbCrLf & vbCrLf & _
         "Not False: " & (Not False) & vbCrLf & "Not True: " & _
         (Not True))
    End Sub 
End Module
Not
Not False: True
Not True: False

Logical operator: Or

Module Tester
    Sub Main()
      Console.WriteLine("Or" & vbCrLf & _
         vbCrLf & "False Or False: " & (False Or False) & _
         vbCrLf & "False Or True: " & (False Or True) & _
         vbCrLf & "True Or False: " & (True Or False) & _
         vbCrLf & "True Or True: " & (True Or True))
    End Sub 
End Module
Or
False Or False: False
False Or True: True
True Or False: True
True Or True: True

Logical operator: OrElse

Module Tester
    Sub Main()
      Console.WriteLine("OrElse" & vbCrLf & vbCrLf & _
         "False OrElse False: " & (False OrElse False) & _
         vbCrLf & "False OrElse True: " & (False OrElse True) & _
         vbCrLf & "True OrElse False: " & (True OrElse False) & _
         vbCrLf & "True OrElse True: " & (True OrElse True))
    End Sub 
End Module
OrElse
False OrElse False: False
False OrElse True: True
True OrElse False: True
True OrElse True: True

Logical operator: Xor

Module Tester
    Sub Main()
      Console.WriteLine("Xor" & vbCrLf & _
         vbCrLf & "False Xor False: " & (False Xor False) & _
         vbCrLf & "False Xor True: " & (False Xor True) & _
         vbCrLf & "True Xor False: " & (True Xor False) & _
         vbCrLf & "True Xor True: " & (True Xor True))
    End Sub 
End Module
Xor
False Xor False: False
False Xor True: True
True Xor False: True
True Xor True: False

Use Logic operator Or to link two functions

Module LogicTest
    Sub Main()
        Dim x As Integer = 1
        Dim y As Integer = 1
        If A(x) Or B(y) Then
            Console.WriteLine("x= " & CStr(x) & ", y = " & CStr(y))
        End If
        If A(x) OrElse B(y) Then
            Console.WriteLine("x= " & CStr(x) & ", y = " & CStr(y))
        End If
    End Sub
    Function A(ByVal v1 As Integer) As Boolean
        Return True
    End Function
    Function B(ByVal v1 As Integer) As Boolean
        Return True
    End Function
End Module
x= 1, y = 1
x= 1, y = 1

Use logic operators in If statement: Not, And, Or

Module Module1
    Sub Main()
        Dim A As Boolean = False
        Dim B As Boolean = True
        Dim C As Boolean = True
        If (Not A) Then
            Console.WriteLine("Not A")
        End If
        If (B Or C) Then
            Console.WriteLine("B Or C")
        End If
        If (B And C) Then
            Console.WriteLine("B And C")
        End If
    End Sub
End Module
Not A
B Or C
B And C