|
|
 |
|
|
|
|
Visual Basic 2008 9.0 .NET Examples and Ebook
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| In both If and Do statements we need to work with conditions. These conditions are formed by conditional expressions.
In a statically types programming language like Visual Basic .NET all expressions are of a - by the compiler know - datatype. Conditional expressions are of type Boolean, therefore they are also called Boolean expressions.
Boolean expressions can only evaluated to two possible values, True or False.
Just like any other datatype variables can be declared of type Boolean. |
| Module Example1
Sub Main()
Console.WriteLine("Value 1 ?")
Dim value1 As Integer = Console.ReadLine()
Console.WriteLine("Value 2 ?")
Dim value2 As Integer = Console.ReadLine()
Dim value1Higher As Boolean = value1 > value2
If value1Higher Then
Console.WriteLine(value1 & " > " & value2)
Else
Console.WriteLine(value1 & " <= " & value2)
End If
Console.ReadLine()
End Sub
End Module Download Broncode |
| Output : Value 1 ?
<i>10</i>
Value 2 ?
<i>5</i>
10 > 5 |
| Output : Value 1 ?
<i>5</i>
Value 2 ?
<i>10</i>
5 <= 10 |
| Variable value1Higher is declared of type Boolean, and is assigned a Boolean value.
A iteration ( Do ) or decision ( If ) can use this variables of type Boolean to form the condition.
In the above example variable value1Higher represents a value ( True or False ) whether ( True ) or not ( False ) value1 is more than value2.
When we want to check equality ( is value1 equal to value2 ) the equality operator = can be used ( value1 = value2 ). |
| Module Example2
Sub Main()
Console.WriteLine("Value 1 ?")
Dim value1 As Integer = Console.ReadLine()
Console.WriteLine("Value 2 ?")
Dim value2 As Integer = Console.ReadLine()
Dim equal As Boolean = value1 = value2
If equal Then
Console.WriteLine(value1 & " = " & value2)
Else
Console.WriteLine(value1 & " <> " & value2)
End If
Console.ReadLine()
End Sub
End Module Download Broncode |
| Output : Value 1 ?
<i>5</i>
Value 2 ?
<i>5</i>
5 = 5 |
| Output : Value 1 ?
<i>5</i>
Value 2 ?
<i>10</i>
5 <> 10 |
| Don't confuse the equality operator '=' with the assignment operator '='. The same symbol is used, but for a different purpose, depending on the syntactical use.
A more readable version of line (1) can be formed when using parentheses : |
| Dim equal As Boolean = (value1 = value2)
|
Default Values and Literals
| Module Example3
Sub Main()
Dim integerVariable As Integer
Console.WriteLine("Integer default value : " & integerVariable)
integerVariable = 5
Console.WriteLine("Integer changed value : " & integerVariable)
Dim stringVariable As String
Dim x As String = ""
Console.WriteLine("String default value : " & stringVariable)
stringVariable = "text"
Console.WriteLine("String changed value : " & stringVariable)
Dim booleanVariable As Boolean
Console.WriteLine("Boolean default value : " & booleanVariable)
booleanVariable = True
Console.WriteLine("Boolean changed value : " & booleanVariable)
booleanVariable = False
Console.WriteLine("Boolean changed value : " & booleanVariable)
Console.ReadLine()
End Sub
End Module Download Broncode |
| Output : Integer default value : 0
Integer changed value : 5
String default value :
String changed value : text
Boolean default value : False
Boolean changed value : True
Boolean changed value : False |
| The above example illustrates the default values of the different datatypes. A default value is a value a variable hold after declaration ( without initialization ).
- 0 for Integer variables ( and all other numeric datatypes ) - Nothing for String variables ( printed out as "" ( no characters ) ) - False for Boolean variables
You can also see how literals ( constant expressions ) are formed with these datatypes :
- Integer literals are formed by an integral value ( between -2147483648 and +2147483647 ) - String literals are formed with Nothing or surrounding double quotes ( " ) - Boolean literals are formed with True or False |
Up
Conjunction Operator And
| Suppose we need to give the sum of two entered values if both values are positive ( above zero ). |
| 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 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 ?
<i>5</i>
Value 2 ?
<i>10</i>
Sum : 15 |
| Output : Value 1 ?
<i>-5</i>
Value 2 ?
<i>10</i> |
| Output : Value 1 ?
<i>5</i>
Value 2 ?
<i>-10</i> |
| Output : Value 1 ?
<i>-5</i>
Value 2 ?
<i>-10</i> |
| 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 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 And value2 > 0 Then
Console.WriteLine("Sum : " & (value1 + value2))
End If
Console.ReadLine()
End Sub
End Module Download Broncode |
| Output : Value 1 ?
<i>5</i>
Value 2 ?
<i>10</i>
Sum : 15 |
| Output : Value 1 ?
<i>-5</i>
Value 2 ?
<i>10</i> |
| Output : Value 1 ?
<i>5</i>
Value 2 ?
<i>-10</i> |
| Output : Value 1 ?
<i>-5</i>
Value 2 ?
<i>-10</i> |
| 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 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
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 ?
<i>5</i>
Value 2 ?
<i>10</i>
Sum : 15 |
| Output : Value 1 ?
<i>-5</i>
Value 2 ?
<i>10</i>
Value 1 not positive. |
| Output : Value 1 ?
<i>5</i>
Value 2 ?
<i>-10</i>
Value 2 not positive. |
| Output : Value 1 ?
<i>-5</i>
Value 2 ?
<i>-10</i>
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 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 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 ?
<i>5</i>
Value 2 ?
<i>10</i>
Sum : 15 |
| Output : Value 1 ?
<i>-5</i>
Value 2 ?
<i>10</i>
Value 1 and/or 2 not positive. |
| Output : Value 1 ?
<i>5</i>
Value 2 ?
<i>-10</i>
Value 1 and/or 2 not positive. |
| Output : Value 1 ?
<i>-5</i>
Value 2 ?
<i>-10</i>
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. |
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 Example8
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 ?
<i>5</i>
Value 2 ?
<i>10</i>
Sum : 15 |
| Output : Value 1 ?
<i>-5</i>
Value 2 ?
<i>10</i>
Sum : 5 |
| Output : Value 1 ?
<i>5</i>
Value 2 ?
<i>-10</i>
Sum : -5 |
| Output : Value 1 ?
<i>-5</i>
Value 2 ?
<i>-10</i> |
| Again by using two nested Ifs this result can be achieved.
ElseIf can also be used. |
| Module Example9
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 ?
<i>5</i>
Value 2 ?
<i>10</i>
Sum : 15 |
| Output : Value 1 ?
<i>-5</i>
Value 2 ?
<i>10</i>
Sum : 5 |
| Output : Value 1 ?
<i>5</i>
Value 2 ?
<i>-10</i>
Sum : -5 |
| Output : Value 1 ?
<i>-5</i>
Value 2 ?
<i>-10</i> |
| Another solution could use the logical operator Or. This operator combines two Boolean expressions and results in one Boolean expression. |
| Module Example10
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 ?
<i>5</i>
Value 2 ?
<i>10</i>
Sum : 15 |
| Output : Value 1 ?
<i>-5</i>
Value 2 ?
<i>10</i>
Sum : 5 |
| Output : Value 1 ?
<i>5</i>
Value 2 ?
<i>-10</i>
Sum : -5 |
| Output : Value 1 ?
<i>-5</i>
Value 2 ?
<i>-10</i> |
| 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 |
Up
Negation Operator Not
| Following example prints out all values from 1 to 10. |
| Module Example11
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 Example12
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 Example13
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 |
| Suppose we need to give the sum of two entered values if the first value is not more than 10 and the second value is not less than or equal to 100.
The condition Not number1 > 10 And Not number2 <= 100 can be used. |
| Module Example14
Sub Main()
Console.WriteLine("Value 1 ?")
Dim value1 As Integer = Console.ReadLine()
Console.WriteLine("Value 2 ?")
Dim value2 As Integer = Console.ReadLine()
If Not value1 > 10 And Not value2 <= 100 Then
Console.WriteLine("Sum : " & (value1 + value2))
End If
Console.ReadLine()
End Sub
End Module Download Broncode |
| Output : Value 1 ?
<i>11</i>
Value 2 ?
<i>200</i> |
| Output : Value 1 ?
<i>9</i>
Value 2 ?
<i>200</i>
Sum : 209 |
| Equal resulting conditional expressions are value1 <= 10 And value2 > 100 and Not (value1 > 10 Or value2 <= 100) ( parentheses are required ). |
| Module Example15
Sub Main()
Console.WriteLine("Value 1 ?")
Dim value1 As Integer = Console.ReadLine()
Console.WriteLine("Value 2 ?")
Dim value2 As Integer = Console.ReadLine()
If Not (value1 > 10 Or value2 <= 100) Then
Console.WriteLine("Sum : " & (value1 + value2))
End If
Console.ReadLine()
End Sub
End Module Download Broncode |
| Output : Value 1 ?
<i>11</i>
Value 2 ?
<i>200</i> |
| Output : Value 1 ?
<i>9</i>
Value 2 ?
<i>200</i>
Sum : 209 |
| Lets examine some situations to see if both solutions will result in identical behaviour.
Suppose the first value is 11 and the second value is 200. The first value exceeds 10, so no sum may be printed out. |
| Output : Value 1 ?
<i>11</i>
Value 2 ?
<i>200</i> |
| Not value1 > 10 And Not value2 <= 100 with value1 11 and value2 200 : = Not 11 > 10 And Not 200 <= 100 = Not True And Not False = False And True = False No sum will be printed out.
Not (value1 > 10 Or value2 <= 100) with value1 11 and value2 200 : = Not (11 > 10 Or 200 <= 100) = Not (True Or False) = Not True = False No sum will be printed out.
Suppose the first value is 9 and the second value is 200. In this case the sum may be printed out. |
| Output : Value 1 ?
<i>9</i>
Value 2 ?
<i>200</i>
Sum : 209 |
| Not value1 > 10 And Not value2 <= 100 with value1 9 and value2 200 : = Not 9 > 10 And Not 200 <= 100 = Not False And Not False = True And True = True The sum will be printed out.
Not (value1 > 10 Or value2 <= 100) with value1 9 and value2 200 : = Not (9 > 10 Or 200 <= 100) = Not (False Or False) = Not False = True The sum will be printed out.
Generally : |
| Not X And Not Y = Not (X Or Y) |
| Not X Or Not Y = Not (X And Y) |
| These are called the rules of "De Morgan".
These rules can also be proven by following truth table : |
| Not X And Not Y = Not (X Or Y) :
X | Y | Not X | Not Y | X Or Y | Not X And Not Y | Not (X Or Y)
True | True | False | False | True | False | False
True | False | False | True | True | False | False
False | True | True | False | True | False | False
False | False | True | True | False | True | True |
| Last two columns have identical result, so : |
| Not X And Not Y = Not (X Or Y) |
| Not X Or Not Y = Not (X And Y) :
X | Y | Not X | Not Y | X And Y | Not X Or Not Y | Not (X And Y)
True | True | False | False | True | False | False
True | False | False | True | False | True | True
False | True | True | False | False | True | True
False | False | True | True | False | True | True |
| Last two columns have identical result, so : |
| Not X Or Not Y = Not (X And Y) |
| Beside the rules of De Morgan other rules exist to simplify conditional expressions. |
| p And False = False
p Or False = p
p And True = p
p Or True = True
p And p = p
p Or p = p
p And (Not p) = False
p Or (Not p) = True
Not (Not p) = p |
| (p And q) = (q And p) -> And is commutative
(p Or q) = (q Or p) -> Or is commutative |
| p And (q Or r) = (p And q) Or (p And r) -> And distributes over Or
p Or (q And r) = (p Or q) And (p Or r) -> Or distributes over And |
| p And (q And r) = (p And q) And r -> And is associative
p Or (q Or r) = (p Or q) Or r -> Or is associative |
Up
Disjunction Operator Xor
| An "exclusive or" operator Xor exists. A combination of two Boolean expressions with this operator will only result in True when exactly one of the two combined expressions is True : |
| condition-1 condition-2 condition-1 Xor condition-2
False False False
False True True
True False True
True True False |
| Suppose we need to give the sum of two entered values only if exactly one of the two values is positive ( above zero ). |
| Module Example16
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 Xor value2 > 0 Then
Console.WriteLine("Sum : " & (value1 + value2))
End If
Console.ReadLine()
End Sub
End Module Download Broncode |
| Output : Value 1 ?
<i>5</i>
Value 2 ?
<i>-10</i>
Sum : -5 |
| Output : Value 1 ?
<i>-5</i>
Value 2 ?
<i>10</i>
Sum : 5 |
| Output : Value 1 ?
<i>5</i>
Value 2 ?
<i>10</i> |
| Output : Value 1 ?
<i>-5</i>
Value 2 ?
<i>-10</i> |
Up
Short-Circuit Operators AndAlso and OrElse
| Suppose we need to give the sum of two entered values only if both values are positive ( above zero ). |
| Module Example17
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 ?
<i>5</i>
Value 2 ?
<i>10</i>
Sum : 15 |
| Output : Value 1 ?
<i>-5</i>
Value 2 ?
<i>10</i> |
| Output : Value 1 ?
<i>5</i>
Value 2 ?
<i>-10</i> |
| Output : Value 1 ?
<i>-5</i>
Value 2 ?
<i>-10</i> |
| A more efficient solution would use the short-circuit conjunction operator AndAlso. |
| Module Example18
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 ?
<i>5</i>
Value 2 ?
<i>10</i>
Sum : 15 |
| Output : Value 1 ?
<i>-5</i>
Value 2 ?
<i>10</i> |
| Output : Value 1 ?
<i>5</i>
Value 2 ?
<i>-10</i> |
| Output : Value 1 ?
<i>-5</i>
Value 2 ?
<i>-10</i> |
| 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.
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 ?
<i>5</i>
Value 2 ?
<i>-10</i>
Sum : -5 |
| Output : Value 1 ?
<i>-5</i>
Value 2 ?
<i>10</i>
Sum : 5 |
| Output : Value 1 ?
<i>5</i>
Value 2 ?
<i>10</i> |
| Output : Value 1 ?
<i>-5</i>
Value 2 ?
<i>-10</i> |
| 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 ?
<i>15</i>
Value ?
<i>150</i>
Value ?
<i>0</i>
Value ?
<i>-5</i>
Value ?
<i>5</i>
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 |
This version ( published on 2008-06-24 ) is printed from http://www.studyvb.com, visit the website for more recent information.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|