|
|
 |
|
|
|
|
|
|
|
|
Visual Basic 2008 9.0 .NET Examples and Ebook
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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. |
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. |
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 |
| 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 |
| 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 |
| Module Exercise2Solution
Sub Main()
Dim value, sum As Integer
Do While (value > 10 AndAlso value < 20) OrElse _
(value > 100) OrElse (value <= 0)
sum = sum + value
Console.WriteLine("Value ?")
value = Console.ReadLine()
Loop
Console.WriteLine("Sum : " & sum)
Console.ReadLine()
End Sub
End Module Download Broncode |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|