Post on 21-Dec-2015
transcript
There is a logical type in VBThere is a logical type in VB
Data Type Prefix
Boolean bln
Dim blnIsStudent As Boolean
blnIsStudent = True
….blnIsStudent = False
Boolean
A Boolean variable can only have one of two values, True or False
Note that “True” is not the same as True
Dim intMarysAge As Integer
intMarysAge = 22
intMarysAge = 10 + 12
intMarysAge = 10 + intA
Dim blnIsDrunk As Boolean
blnIsDrunk = False
blnIsDrunk = (1 < 2)
blnIsDrunk = (intNumBeers > 2)
We have seen this stuff for numbers…
Here are some analogous ideas for the Boolean type
blnIsDrunk = (1 < 2)
This is a relational operator,Here is the full set of relational operators…
Symbol Name
= Equality < > Inequality ()< Less Than> Greater Than<= Less Than or Equal To () >= Greater Than or Equal To ()
Dim blnB As Boolean
blnB = False
blnB = (1 < 2)
blnB = (2 = 2)
blnB = (1 = 2)
blnB = (1 <> 2)
blnB = (5 <> 5)
blnB = (5 < 5)
blnB = (5 <= 5)
The relational operators work as you might expect for numbers (with one important exception, which we will see later)
Dim blnB As Boolean
blnB = False
blnB = (“eamonn” = “joe”)
blnB = (“cat” = “cat”)
blnB = (“cat” < “can”)
blnB = (“Dog” < “dog”)
blnB = (“5” < “Five”)
blnB = ("5" = (1 + 4).ToString)
blnB = ("eamonn" > "eamon")
The relational operators are also defined for strings
“1”< “2”< ... <“A” < “B” <…< “a” < “b”…
True: Any character is alphabetically after none.
So (“E” > “”) is True
“cat”
“can”
Suppose we want a Boolean variable blnIsAdult to be True if some is aged 18 or older. Further suppose we have the persons age in shtAge…
Dim blnIsAdult As Boolean
blnIsAdult = (shtAge >= 18)
Suppose we want a Boolean variable blnNoSeniorDiscount to be True if some is aged 64 or less.
Dim blnNoSeniorDiscount As Boolean
blnNoSeniorDiscount = (shtAge <= 64)
Dim blnPaysFullPrice As Boolean
blnPaysFullPrice = (intAge >= 18) And (intAge <= 64)
Now suppose we want a Boolean variable blnPaysFullPrice to be True if some is aged 18 or older and 64 or younger…
Here the word And is a Boolean operator…
We know that the arithmetic operators take two numbers, and produce another number 5 + 2
7The logical operator And takes two logical values, and produce a logical value
True And False
False
a b a And bTrue True TrueTrue False FalseFalse True FalseFalse False False
a b a Or bTrue True TrueTrue False TrueFalse True TrueFalse False False
a Not a
True False
False True
a b a And bTrue True TrueTrue False FalseFalse True FalseFalse False False
Let a be true if a man is rich Let b be true if a man is tall
Would Patty marry...
Shaquille O'NealBill Gates Eamonn (dirt poor, 6’3)
Joe (dirt poor, 5’1)
I would marry a man if he was rich and tall
I would marry a man if he was rich and tall
a b a Or bTrue True TrueTrue False TrueFalse True TrueFalse False False
Let a be true if a woman is an astronaut Let b be true if a woman is a foreigner
Would Carl marry...•Valentina Tereshkova (Vostok 7)
•Sally Ride (STS-7 83) •Michelle Yeoh•Hillary Clinton
I would marry a woman if she was an astronaut or was a foreigner
I would marry a woman if she was an astronaut or was a foreigner
Let a be true if a woman is a divorcee
Would Ned marry...
• Elizabeth Taylor• Britney Spears • Mary Kate Olsen
a Not a
True False
False TrueI would marry a
woman if she was not a divorcee
I would marry a woman if she was not a divorcee
We know it is legal to combine multiple arithmetic operators...
2 * 3 + 5
6 + 5
11
The same is true for logic operators...
False Or False Or True
False Or True
TrueBut just like arithmetic there is a problem of ambiguity … (next slide)
A * B + C
X Or Y Or Z
I would marry a guy if he were
good looking and tall or very rich
I would marry a guy if he were
good looking and tall or very rich
This is ambiguous!
False And False Or True
False Or True
True
False And False Or True
False And True
False
Working left to right Working right to left
The ambiguity is solved with precedence
work from left to right Parentheses ()
Negation NotConjunction AndDisjunction Or
Precedence can be overwritten
with parentheses
False AND ( False OR True)
True
Never test for equality with real numbersNever test for equality with real numbers
Dim blnIsAdult As Boolean
Dim intAge As Integer
intAge = ….
blnIsAdult = (intAge = 18)
Dim blnIsPi As Boolean
Dim decAnswer As Decimal
decAnswer = ….
blnIsPi = (decAnswer = 3.1415)
This is fine
This is not
Never test for equality with real numbersNever test for equality with real numbers
Dim blnIsPi As Boolean
Dim decA As Decimal
decA = ….
blnIsPi = (decA > 3.1415 – 0.1) And (decA < 3.1415 + 0.1)
True Pi
2 3 4
0.1
Or…Or…
Dim blnIsPi As Boolean
Dim decA As Decimal
Const sngLoPi As Single = 3.1, sngHiPi As Single = 3.2
blnIsPi = (decA > sngLoPi) And (decA < sngHiPi )
True Pi
2 3 4 0.1
Note, I wrote sngLoPi to save space, sngLowestAcceptableValueForPi would be better
Or…Or…Dim blnIsPi As Boolean
Dim decA As Decimal
Const sngtolerance As Single = 0.1, sngPi As Single = 3.1415
blnIsPi = (decA > sngPi – sngtolerance ) And (decA < sngPi + sngtolerance )
True Pi
2 3 4
0.1
Common mistake: Unreachable codeCommon mistake: Unreachable code
(A > B) And (B = A) (A > B) Or (B >= A)
Can never be true Can never be false
Public Class Form1
Inherits System.Windows.Forms.Form
Dim intA As Integer
Private Sub bntBlueDemo_Click(…
intA = 0
bntRedDemo.Text = (intA).ToString
End Sub
Private Sub bntRedDemo_Click(….
intA = intA + 1
bntRedDemo.Text = intA.ToString
If intA = 10 Then
bntRedDemo.Text = "You have reached ten!"
End If
End Sub
End Class
Windows From Designer generated code
…
Conditional BranchingConditional Branching
If (Logical Expression) Then
Program statements to execute if expression evaluates to True
End If
If (Logical Expression) Then
Program statements to execute if expression evaluates to True
End If
We use If statement whenever we want to execute code, only under some circumstances
It is your birthday todayThe student has paid feesThe password is correct
Dim shtFavNumber As Short
shtFavNumber = …. ‘Assume we get the number from user
If shtFavNumber = 7 Then bntRedDemo.Text = “That’s my favorite number too!”End If
bntBlueDemo.Text = “Goodbye”
That’s my favorite number too!
Goodbye
Goodbye
Dim sngExamScore As Single ‘I am assuming the range [0-100]
sngExamScore = ….
If sngExamScore >= 90 Then bntRedDemo.Text = “Well Done!”End If
Well Done!
Dim sngExamScore As Single ‘I am assuming the range [0-100]
sngExamScore = ….
If sngExamScore >= 90 Then bntRedDemo.Text = “Well Done!”End If
If sngExamScore < 90 Then bntBlueDemo.Text = “Work Harder!”End If
Well Done!
Work Harder!
Legal, but bad idea
Legal, but bad idea
Dim sngExamScore As Single ‘I am assuming the range [0-100]
sngExamScore = ….
If sngExamScore >= 90 Then bntRedDemo.Text = “Well Done!”End If
If sngExamScore <= 90 Then bntRedDemo.Text = “Work Harder!”End If
Suppose we got the range wrong…
Suppose we got the range wrong…
Well Done!
Work Harder!
Well Done!
Work Harder!
45
97
90
If (Logical Expression) Then
Program statements to execute if expression evaluates to True
Else
Program statements to execute if expression evaluates to False
End If
If (Logical Expression) Then
Program statements to execute if expression evaluates to True
Else
Program statements to execute if expression evaluates to False
End If
Dim sngExamScore As Single ‘I am assuming the range [0-100]
sngExamScore = ….
If sngExamScore >= 90 Then
bntRedDemo.Text = “Well Done!”
Else
bntBlueDemo.Text = “Work Harder!”End If
Well Done!
Work Harder!
Much better than previous example
Much better than previous example
97
45
We use If Then Else statement whenever we want to divide the world into two, mutually exclusive possibilities…
Student/ nonStudentMarried/ notMarriedLiving/ Dead18 or older/ Under 18
Sometimes there are 3 or more mutually exclusive possibilities…
Student/ nonStudentMarried/ notMarriedLiving/ Dead18 or older/ Under 18
Undergrad/Grad/ Professor/ nonStudentMarried/ Widowed/ DivorcedLiving/ Dead / Never livedChild/ Adult / Senior
We can use the If Then Else If statement …
If sngExamScore >= 90 Then bntRedDemo.Text = “Well Done!”Else If sngExamScore >= 80 bntRedDemo.Text = “Not Bad!”Else bntRedDemo.Text = “Work Harder!”End If
Well Done!
Not Bad!
Work Harder!
If sngExamScore >= 80 Then bntRedDemo.Text = “Not Bad!”Else If sngExamScore >= 90 bntRedDemo.Text = “Well Done!”Else bntRedDemo.Text = “Work Harder!”End If
Not Bad
Not Bad!
Work Harder!
Unreachable code
Unreachable code
If (Condition) Then Do SomethingElseIf (Condition 2) Then Do Something ElseElseIf (Condition 3) Then Do Something Else...Else Do Something ElseEnd If
If (Condition) Then Do SomethingElseIf (Condition 2) Then Do Something ElseElseIf (Condition 3) Then Do Something Else...Else Do Something ElseEnd If
We can have as many Else If’s as we want …
We use If Then Else If statement whenever we want to divide the world into three or more, mutually exclusive possibilities…
Select Case Expression Case Possible Value or Range of Values Statement(s) Case Another Possible Value or Range of Values Statement(s) . . .
Case Else Statement(s)End Select
Select Case Expression Case Possible Value or Range of Values Statement(s) Case Another Possible Value or Range of Values Statement(s) . . .
Case Else Statement(s)End Select
The expression in a Select Case statement may be• a numeric variable
• a string variable
• a simple expression composed of operators and variables
The possible values in a Case statement may be• a numeric constant
• a string constant
• a numeric variable
• a string variable
• a range of values
• a combination of the above
Dim shtFavNumber As Short
shtFavNumber = …. ‘Assume we ask for a number between 0 and 10
Select Case shtFavNumber
Case 7
bntRedDemo.Text = “Seven is my favorite too!”
Case Else
bntRedDemo.Text = “That’s different to my favorite”
End Select
Dim shtFavNumber As Short
shtFavNumber = …. ‘Assume we ask for a number between 0 and 10
Select Case shtFavNumber
Case 7
bntRedDemo.Text = “Seven is my favorite too!”
Case Else
bntRedDemo.Text = “That’s different to my favorite”
End Select
Select Case shtFavNumber
Case 1 , 3 , 5 , 9
bntRedDemo.Text = “Odd number!”
Case 7
bntRedDemo.Text = “Odd number, and my favorite too”
Case 0, 2 , 4 , 6 , 8
bntRedDemo.Text = “Even number”
Case Else
bntRedDemo.Text = “Error!”
End Select
Select Case shtFavNumber
Case 1 , 3 , 5 , 9
bntRedDemo.Text = “Odd number!”
Case 7
bntRedDemo.Text = “Odd number, and my favorite too”
Case 0, 2 , 4 , 6 , 8
bntRedDemo.Text = “Even number”
Case Else
bntRedDemo.Text = “Error!”
End Select
Dim strUserInput As String
strUserInput = “Lenny”
Select Case strUserInput
Case “Homer”, “Marge”, “Maggie”, “Lisa”, “Bart”
bntRedDemo.Text = “Simpson Clan”
Case “Monty”, “Waylon”, “Carl”, “Lenny”
bntRedDemo.Text = “PowerPlant Worker”
Case Else
bntRedDemo.Text = “Other”
End Select
Dim strUserInput As String
strUserInput = “Lenny”
Select Case strUserInput
Case “Homer”, “Marge”, “Maggie”, “Lisa”, “Bart”
bntRedDemo.Text = “Simpson Clan”
Case “Monty”, “Waylon”, “Carl”, “Lenny”
bntRedDemo.Text = “PowerPlant Worker”
Case Else
bntRedDemo.Text = “Other”
End Select