Home

Logical Conjunction and Disjunction

     

Conjunction

 

You can use a logical conjunction to combine two or more Boolean expressions. To do this, use the Where statement to create the expression as you see fit and add an And conjunction to it. Here is an expample:

Imports System.Linq
Imports System.Collections.Generic

Module Exercise
    Public Function Main() As Integer
        Dim Numbers = New List(Of Integer)

        Numbers.Add(12)
        Numbers.Add(45)
        Numbers.Add(38)
        Numbers.Add(5)
        Numbers.Add(128)
        Numbers.Add(525)
        Numbers.Add(2448)
        Numbers.Add(39)
        Numbers.Add(632)
        Numbers.Add(207)

        Dim Number = From N
                     In Numbers
                     Where N Mod 2 = 0 And N <= 100
                     Select N

        For Each member In Number
            Console.WriteLine(member)
        Next

        Console.WriteLine()
        Return 0
    End Function
End Module

This would produce:

Conjunction

Once again, remember that the use of parentheses makes it easier to read the code and better understand it:

Imports System.Linq
Imports System.Collections.Generic

Module Exercise
    Public Function Main() As Integer
        Dim Numbers = New List(Of Integer)

        Numbers.Add(12)
        Numbers.Add(45)
        Numbers.Add(38)
        Numbers.Add(5)
        Numbers.Add(128)
        Numbers.Add(525)
        Numbers.Add(2448)
        Numbers.Add(39)
        Numbers.Add(632)
        Numbers.Add(207)

        Dim Number = From N
                     In Numbers
                     Where (N Mod 2 = 0) And (N <= 100)
                     Select N

        For Each member In Number
            Console.WriteLine(member)
        Next

        Console.WriteLine()
        Return 0
    End Function
End Module

Or better yet:

Imports System.Linq
Imports System.Collections.Generic

Module Exercise
    Public Function Main() As Integer
        Dim Numbers = New List(Of Integer)

        Numbers.Add(12)
        Numbers.Add(45)
        Numbers.Add(38)
        Numbers.Add(5)
        Numbers.Add(128)
        Numbers.Add(525)
        Numbers.Add(2448)
        Numbers.Add(39)
        Numbers.Add(632)
        Numbers.Add(207)

        Dim Number = From N
                     In Numbers
                     Where ((N Mod 2) = 0) And (N <= 100)
                     Select N

        For Each member In Number
            Console.WriteLine(member)
        Next

        Console.WriteLine()
        Return 0
    End Function
End Module

To negate a conjunction, precede it with a ! operator. Remember that a conjunction is made of two parts. Therefore, if you want to negate only the first part, precede it with !. If you want to negate the whole conjunction, put the conjunction in parentheses but precede it with ! (outside the parentheses).

Disjunction

When creating a LINQ statement, you can use a logical disjunction, which is represented by the Or operator. Here is an example:

Imports System.Linq
Imports System.Collections.Generic

Module Exercise
    Public Function Main() As Integer
        Dim Numbers = New List(Of Integer)

        Numbers.Add(12)
        Numbers.Add(45)
        Numbers.Add(38)
        Numbers.Add(5)
        Numbers.Add(128)
        Numbers.Add(525)
        Numbers.Add(2448)
        Numbers.Add(39)
        Numbers.Add(632)
        Numbers.Add(207)

        Dim Number = From N
                     In Numbers
 		     Where ((N Mod 2) = 0) Or (N Mod 5 = 0)
                     Select N

        For Each member In Number
            Console.WriteLine(member)
        Next

        Console.WriteLine()
        Return 0
    End Function
End Module

This would produce:

12
45
38
5
128
525
2448
632

Press any key to continue . . .
 
 
     
 

Previous Copyright © 2010-2016, FunctionX Next