Visual Basic 2008 9.0 .NET Examples and Ebook
  Home

Introduction to Visual Basic

Arrays

Volgend Onderwerp

Logical Shortcircuit Operators AndAlso and OrElse

Vorig Onderwerp

Logical Disjunction Operator Xor

|

Operators

Volgend Onderwerp
Logical Shortcircuit Operator AndAlso

Logical Shortcircuit Operator AndAlso

Logical Shortcircuit Operator AndAlso

Logical Shortcircuit Operator AndAlso

Exercises

Exercises



Logical Shortcircuit Operator AndAlso


Suppose we need to give the sum of two entered values only 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 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

A more efficient solution would use the short-circuit conjunction operator AndAlso.


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 AndAlso 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

The result will always be False if the first evaluated condition is False :


 condition-1   condition-2     condition-1 And(Also) condition-2
 True          True            True
 True          False           False
 False         True            False
 False         False           False

AndAlso will only evaluate the second condition if the first condition is True.


Klik hier om terug naar boven te gaan.  Up


Logical Shortcircuit Operator AndAlso


The short-circuit disjunction operator OrElse will only evaluate the second condition if the first condition is False. The result will always be True if the first condition is True.


 condition-1   condition-2     condition-1 Or(Else) condition-2
 True          True            True
 True          False           True
 False         True            True
 False         False           False

To improve performance short-circuit operators ( AndAlso and OrElse ) should be used when available.
Only in a few rare circumstances the normal operators ( And and Or ) are needed. Later more about this circumstances.


Klik hier om terug naar boven te gaan.  Up


Exercises


Task :

Give the sum of two entered values only if exactly one value is positive ( above zero ).

Do this without using the Xor operator.


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

Output :

 Value 1 ?
 -5
 Value 2 ?
 -10

Solution :


Module Exercise1Solution
    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 AndAlso Not value2 <> 0 OrElse _
           Not value1 <> 0 AndAlso value2 <> 0 Then
            Console.WriteLine("Sum : " & (value1 + value2))
        End If
        '
        Console.ReadLine()
    End Sub
End Module
Download Broncode

Task :


Make a program that calculates the sum of all entered values, while values are entered that are between 10 and 20, more than 100 or smaller than or equal to 0.

Do this with a Do While and a Do Loop.


Output :

 Value ?
 15
 Value ?
 150
 Value ?
 0
 Value ?
 -5
 Value ?
 5
 Sum : 160

Solution :


Module Exercise2Solution
    Sub Main()
        Dim value, sum As Integer
        '
        ' 'Do While ... Loop' :
        Do While (value > 10 AndAlso value < 20) OrElse _
                 (value > 100) OrElse (value <= 0)
            sum = sum + value
            Console.WriteLine("Value ?")
            value = Console.ReadLine()
        Loop
        '
        '' 'Do Until ... Loop' : version 1 :
        'Do Until Not ((value > 10 AndAlso value < 20) OrElse _
        '              (value > 100) OrElse (value <= 0))
        '    sum = sum + value
        '    Console.WriteLine("Value ?")
        '    value = Console.ReadLine()
        'Loop
        '
        '' 'Do Until ... Loop' : version 2 :
        'Do Until (value <= 10 OrElse value >= 20) AndAlso _
        '         (value <= 100) AndAlso (value > 0)
        '    sum = sum + value
        '    Console.WriteLine("Value ?")
        '    value = Console.ReadLine()
        'Loop
        '
        Console.WriteLine("Sum : " & sum)
        '
        Console.ReadLine()
    End Sub
End Module
Download Broncode

Updated On : 2008-11-06

Download Broncode

Published On : 2008-11-06

Logical Shortcircuit Operators AndAlso and OrElse

Vorig Onderwerp

Logical Disjunction Operator Xor

|

Operators

Volgend Onderwerp

Introduction to Visual Basic

Arrays

Volgend Onderwerp
  Home  
Nederlands
Nederlands

Add to favorites (IE).


No printable version available.