Visual Basic 2008 9.0 .NET Examples and Ebook
  Home

Introduction to Visual Basic

Arrays

Volgend Onderwerp

Logical Operators And, Or and Not

Vorig Onderwerp

Boolean Datatype and Expressions

|

Boolean Logical

Volgend Onderwerp
Conjunction Operator And

Conjunction Operator And

Disjunction Operator Or

Disjunction Operator Or

Negation Operator Not

Negation Operator Not



Conjunction Operator And


Suppose we need to print the sum of two entered values if both values are positive ( above zero ).


Module Example1
    Sub Main()
        Console.WriteLine("Value 1 ?")
        Dim value1 As Integer = Console.ReadLine()
        '
        Console.WriteLine("Value 2 ?")
        Dim value2 As Integer = Console.ReadLine()
        '
        If value1 > 0 Then
            If value2 > 0 Then
                Console.WriteLine("Sum : " & (value1 + value2))
            End If
        End If
        '
        Console.ReadLine()
    End Sub
End Module
Download Broncode

Output :

 Value 1 ?
 5
 Value 2 ?
 10
 Sum : 15

Output :

 Value 1 ?
 -5
 Value 2 ?
 10

Output :

 Value 1 ?
 5
 Value 2 ?
 -10

Output :

 Value 1 ?
 -5
 Value 2 ?
 -10

The goal is achieved by nesting two Ifs. Only if value1 is more than 0,
and value2 is more than 0, the sum will be given.

An alternative would be to use the logical operator And.
This operator combines two Boolean expressions to one Boolean expression.


Module Example2
    Sub Main()
        Console.WriteLine("Value 1 ?")
        Dim value1 As Integer = Console.ReadLine()
        '
        Console.WriteLine("Value 2 ?")
        Dim value2 As Integer = Console.ReadLine()
        '
        If value1 > 0 And value2 > 0 Then
            Console.WriteLine("Sum : " & (value1 + value2))
        End If
        '
        Console.ReadLine()
    End Sub
End Module
Download Broncode

Output :

 Value 1 ?
 5
 Value 2 ?
 10
 Sum : 15

Output :

 Value 1 ?
 -5
 Value 2 ?
 10

Output :

 Value 1 ?
 5
 Value 2 ?
 -10

Output :

 Value 1 ?
 -5
 Value 2 ?
 -10

Only if both ( combined ) Boolean expressions are correct ( True ) the result is True :


 condition-1      condition-2         condition-1 And condition-2
 True             True                True
 True             False               False
 False            True                False
 False            False               False

When two nested Ifs are used, each failing condition can have its alternate instructions, for instance to give specific errors like "Value 1 not positive." and "Value 2 not positive.".


Module Example3
    Sub Main()
        Console.WriteLine("Value 1 ?")
        Dim value1 As Integer = Console.ReadLine()
        '
        Console.WriteLine("Value 2 ?")
        Dim value2 As Integer = Console.ReadLine()
        '
        If value1 > 0 Then
            If value2 > 0 Then
                Console.WriteLine("Sum : " & _
                                  (value1 + value2))
            Else
                Console.WriteLine("Value 2 not positive.")
            End If
        Else
            Console.WriteLine("Value 1 not positive.")
        End If
        '
        Console.ReadLine()
    End Sub
End Module
Download Broncode

Output :

 Value 1 ?
 5
 Value 2 ?
 10
 Sum : 15

Output :

 Value 1 ?
 -5
 Value 2 ?
 10
 Value 1 not positive.

Output :

 Value 1 ?
 5
 Value 2 ?
 -10
 Value 2 not positive.

Output :

 Value 1 ?
 -5
 Value 2 ?
 -10
 Value 1 not positive.

It the And operator is used, only one general error "Value 1 and/or 2 not positive." can be given.


Module Example4
    Sub Main()
        Console.WriteLine("Value 1 ?")
        Dim value1 As Integer = Console.ReadLine()
        '
        Console.WriteLine("Value 2 ?")
        Dim value2 As Integer = Console.ReadLine()
        '
        If value1 > 0 And value2 > 0 Then
            Console.WriteLine("Sum : " & _
                              (value1 + value2))
        Else
            Console.WriteLine("Value 1 and/or 2 not positive.")
        End If
        '
        Console.ReadLine()
    End Sub
End Module
Download Broncode

Output :

 Value 1 ?
 5
 Value 2 ?
 10
 Sum : 15

Output :

 Value 1 ?
 -5
 Value 2 ?
 10
 Value 1 and/or 2 not positive.

Output :

 Value 1 ?
 5
 Value 2 ?
 -10
 Value 1 and/or 2 not positive.

Output :

 Value 1 ?
 -5
 Value 2 ?
 -10
 Value 1 and/or 2 not positive.

Therefore one could prefer the solution with the nested Ifs above the solution with the And operator.

The solution using the And operator is less efficient than the solution with the two nested Ifs.

The expression with the And operator will always evaluate both ( combined ) expressions.
The version with the two nested Ifs will only evaluate value2 > 0 in worst case scenario ( if value1 > 0 is correct ).

Later on we'll see how the 'AndAlso' operator can be used to avoid unnecessary checks.


Klik hier om terug naar boven te gaan.  Up


Disjunction Operator Or


Suppose we need to give the sum of two entered values if at least one of the values is positive ( above zero ).


Module Example5
    Sub Main()
        Console.WriteLine("Value 1 ?")
        Dim value1 As Integer = Console.ReadLine()
        '
        Console.WriteLine("Value 2 ?")
        Dim value2 As Integer = Console.ReadLine()
        '
        If value1 > 0 Then
            Console.WriteLine("Sum : " & (value1 + value2))
        Else
            If value2 > 0 Then
                Console.WriteLine("Sum : " & (value1 + value2))
            End If
        End If
        '
        Console.ReadLine()
    End Sub
End Module
Download Broncode

Output :

 Value 1 ?
 5
 Value 2 ?
 10
 Sum : 15

Output :

 Value 1 ?
 -5
 Value 2 ?
 10
 Sum : 5

Output :

 Value 1 ?
 5
 Value 2 ?
 -10
 Sum : -5

Output :

 Value 1 ?
 -5
 Value 2 ?
 -10

Again by using two nested Ifs this result can be achieved.

ElseIf can also be used.


Module Example6
    Sub Main()
        Console.WriteLine("Value 1 ?")
        Dim value1 As Integer = Console.ReadLine()
        '
        Console.WriteLine("Value 2 ?")
        Dim value2 As Integer = Console.ReadLine()
        '
        If value1 > 0 Then
            Console.WriteLine("Sum : " & (value1 + value2))
        ElseIf value2 > 0 Then
            Console.WriteLine("Sum : " & (value1 + value2))
        End If
        '
        Console.ReadLine()
    End Sub
End Module
Download Broncode

Output :

 Value 1 ?
 5
 Value 2 ?
 10
 Sum : 15

Output :

 Value 1 ?
 -5
 Value 2 ?
 10
 Sum : 5

Output :

 Value 1 ?
 5
 Value 2 ?
 -10
 Sum : -5

Output :

 Value 1 ?
 -5
 Value 2 ?
 -10

Another solution could use the logical operator Or.
This operator combines two Boolean expressions and results in one Boolean expression.


Module Example7
    Sub Main()
        Console.WriteLine("Value 1 ?")
        Dim value1 As Integer = Console.ReadLine()
        '
        Console.WriteLine("Value 2 ?")
        Dim value2 As Integer = Console.ReadLine()
        '
        If value1 > 0 Or value2 > 0 Then
            Console.WriteLine("Sum : " & (value1 + value2))
        End If
        '
        Console.ReadLine()
    End Sub
End Module
Download Broncode

Output :

 Value 1 ?
 5
 Value 2 ?
 10
 Sum : 15

Output :

 Value 1 ?
 -5
 Value 2 ?
 10
 Sum : 5

Output :

 Value 1 ?
 5
 Value 2 ?
 -10
 Sum : -5

Output :

 Value 1 ?
 -5
 Value 2 ?
 -10

When at least one of the combined conditions is True the result will be True :


 condition-1      condition-2         condition-1 Or condition-2
 True             True                True
 True             False               True
 False            True                True
 False            False               False

Klik hier om terug naar boven te gaan.  Up


Negation Operator Not


Following example prints out all values from 1 to 10.


Module Example8
    Sub Main()
        Dim value As Integer
        '
        Do While value < 10
            value = value + 1
            Console.WriteLine(value)
        Loop
        '
        Console.ReadLine()
    End Sub
End Module
Download Broncode

Output :

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10

When we want to convert the Do While to a Do Loop, we need to invert the condition.

A value is not less than 10 only when it is more than 10 or equal to 10.


Module Example9
    Sub Main()
        Dim value As Integer
        '
        Do Until value >= 10
            value = value + 1
            Console.WriteLine(value)
        Loop
        '
        Console.ReadLine()
    End Sub
End Module
Download Broncode

Output :

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10

An easier way to invert a condition is by using the negation operator Not.
This operator inverts one Boolean expression.


 condition-1       Not condition-1
 True              False
 False             True


Module Example10
    Sub Main()
        Dim value As Integer
        '
        Do Until Not value < 10
            value = value + 1
            Console.WriteLine(value)
        Loop
        '
        Console.ReadLine()
    End Sub
End Module
Download Broncode

Output :

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10

Updated On : 2008-11-06

Download Broncode

Published On : 2008-11-06

Logical Operators And, Or and Not

Vorig Onderwerp

Boolean Datatype and Expressions

|

Boolean Logical

Volgend Onderwerp

Introduction to Visual Basic

Arrays

Volgend Onderwerp
  Home  
Nederlands
Nederlands

Add to favorites (IE).


No printable version available.