There is a logical type in VB Boolean
There is a logical type in VB Boolean A Boolean variable can only have one of two values, True or False Data Type Prefix Boolean bln Dim bln. Is. Student As Boolean bln. Is. Student = True …. bln. Is. Student = False Note that “True” is not the same as True
Dim int. Marys. Age As Integer int. Marys. Age = 22 We have seen this stuff for numbers… int. Marys. Age = 10 + 12 Here are some analogous ideas for the Boolean type Dim bln. Is. Drunk As Boolean int. Marys. Age = 10 + int. A bln. Is. Drunk = False bln. Is. Drunk = (1 < 2) bln. Is. Drunk = (int. Num. Beers > 2)
bln. Is. Drunk = (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 bln. B As Boolean bln. B = False bln. B = (1 < 2) bln. B = (2 = 2) bln. B = (1 <> 2) bln. B = (5 <> 5) bln. B = (5 <= 5) The relational operators work as you might expect for numbers (with one important exception, which we will see later)
“ 1”< “ 2”<. . . <“A” < “B” <…< “a” < “b”… Dim bln. B As Boolean bln. B = False bln. B = (“eamonn” = “joe”) The relational operators are also defined for strings bln. B = (“cat” = “cat”) “cat” bln. B = (“cat” < “can”) “can” bln. B = (“Dog” < “dog”) bln. B = (“ 5” < “Five”) bln. B = ("5" = (1 + 4). To. String) True: Any character is alphabetically after none. bln. B = ("eamonn" > "eamon") So (“E” > “”) is True
Suppose we want a Boolean variable bln. Is. Adult to be True if some is aged 18 or older. Further suppose we have the persons age in sht. Age… Dim bln. Is. Adult As Boolean bln. Is. Adult = (sht. Age >= 18) Suppose we want a Boolean variable bln. No. Senior. Discount to be True if some is aged 64 or less. Dim bln. No. Senior. Discount As Boolean bln. No. Senior. Discount = (sht. Age <= 64)
Now suppose we want a Boolean variable bln. Pays. Full. Price to be True if some is aged 18 or older and 64 or younger… Dim bln. Pays. Full. Price As Boolean bln. Pays. Full. Price = (int. Age >= 18) And (int. Age <= 64) Here the word And is a Boolean operator…
We know that the arithmetic operators take two numbers, and produce another number 5 + 2 7 The logical operator And takes two logical values, and produce a logical value True And False
a True False b a And b True False False a True False b True False a Or b True False a Not a True False True
I would marry a man if he was rich and tall a True False b a And b True 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'Neal Bill Gates Eamonn (dirt poor, 6’ 3) Joe (dirt poor, 5’ 1)
a True False I would marry a woman if she was an astronaut or was a foreigner b True False a Or b True 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 not a divorcee a Not a True False True Let a be true if a woman is a divorcee Would Ned marry. . . • Elizabeth Taylor • Britney Spears • Mary Kate Olsen
We know it is legal to combine multiple arithmetic operators. . . A * B + C 2 * 3 + 5 6 + 5 11 The same is true for logic operators. . . X Or Y Or Z False Or True But just like arithmetic there is a problem of ambiguity … (next slide)
I would marry a guy if he were good looking and tall or very rich This is ambiguous!
False And False Or True False And True Working left to right False Working right to left The ambiguity is solved with precedence work from left to right Parentheses () Negation Not Conjunction And Disjunction Or Precedence can be overwritten with parentheses False AND ( False OR True) True
Never test for equality with real numbers Dim bln. Is. Adult As Boolean Dim int. Age As Integer This is fine int. Age = …. bln. Is. Adult = (int. Age = 18) Dim bln. Is. Pi As Boolean Dim dec. Answer As Decimal dec. Answer = …. bln. Is. Pi = (dec. Answer = 3. 1415) This is not
Never test for equality with real numbers Dim bln. Is. Pi As Boolean Dim dec. A As Decimal dec. A = …. bln. Is. Pi = (dec. A > 3. 1415 – 0. 1) And (dec. A < 3. 1415 + 0. 1) True Pi 2 3 4 0. 1
Or… Dim bln. Is. Pi As Boolean Dim dec. A As Decimal Const sng. Lo. Pi As Single = 3. 1, sng. Hi. Pi As Single = 3. 2 bln. Is. Pi = (dec. A > sng. Lo. Pi) And (dec. A < sng. Hi. Pi ) Note, I wrote sng. Lo. Pi to save space, sng. Lowest. Acceptable. Value. For. Pi would be better True Pi 2 3 4 0. 1
Or… Dim bln. Is. Pi As Boolean Dim dec. A As Decimal Const sngtolerance As Single = 0. 1, sng. Pi As Single = 3. 1415 bln. Is. Pi = (dec. A > sng. Pi – sngtolerance ) And (dec. A < sng. Pi + sngtolerance ) True Pi 2 3 4 0. 1
Common mistake: Unreachable code Can never be true (A > B) And (B = A) Can never be false (A > B) Or (B >= A)
Public Class Form 1 Inherits System. Windows. Form Dim int. A As Integer Windows From Designer generated code Private Sub bnt. Blue. Demo_Click(… int. A = 0 bnt. Red. Demo. Text = (int. A). To. String End Sub Private Sub bnt. Red. Demo_Click(…. int. A = int. A + 1 bnt. Red. Demo. Text = int. A. To. String If int. A = 10 Then bnt. Red. Demo. Text = "You have reached ten!" End If End Sub End Class …
Conditional Branching 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 today The student has paid fees The password is correct
Dim sht. Fav. Number As Short sht. Fav. Number = …. ‘Assume we get the number from user If sht. Fav. Number = 7 Then bnt. Red. Demo. Text = “That’s my favorite number too!” End If bnt. Blue. Demo. Text = “Goodbye” That’s my favorite number too! Goodbye
Dim sng. Exam. Score As Single ‘I am assuming the range [0 -100] sng. Exam. Score = …. If sng. Exam. Score >= 90 Then bnt. Red. Demo. Text = “Well Done!” End If Well Done!
Dim sng. Exam. Score As Single ‘I am assuming the range [0 -100] sng. Exam. Score = …. If sng. Exam. Score >= 90 Then bnt. Red. Demo. Text = “Well Done!” End If If sng. Exam. Score < 90 Then bnt. Blue. Demo. Text = “Work Harder!” End If Well Done! Work Harder! Legal, but bad idea
Dim sng. Exam. Score As Single ‘I am assuming the range [0 -100] sng. Exam. Score = …. If sng. Exam. Score >= 90 Then bnt. Red. Demo. Text = “Well Done!” End If If sng. Exam. Score <= 90 Then bnt. Red. Demo. Text = “Work Harder!” End If Well Done! 90 Work Harder! Well Done! 97 45 Work Harder! Suppose we got the range wrong…
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 sng. Exam. Score As Single ‘I am assuming the range [0 -100] sng. Exam. Score = …. If sng. Exam. Score >= 90 Then bnt. Red. Demo. Text = “Well Done!” Else bnt. Blue. Demo. Text = “Work Harder!” End If 97 Well Done! 45 Work Harder! Much better than previous example
We use If Then Else statement whenever we want to divide the world into two, mutually exclusive possibilities… Student/ non. Student Married/ not. Married Living/ Dead 18 or older/ Under 18
Sometimes there are 3 or more mutually exclusive possibilities… Student/ non. Student Married/ not. Married Living/ Dead 18 or older/ Under 18 Undergrad/Grad/ Professor/ non. Student Married/ Widowed/ Divorced Living/ Dead / Never lived Child/ Adult / Senior We can use the If Then Else If statement …
If sng. Exam. Score >= 90 Then bnt. Red. Demo. Text = “Well Done!” Else If sng. Exam. Score >= 80 bnt. Red. Demo. Text = “Not Bad!” Else bnt. Red. Demo. Text = “Work Harder!” End If Well Done! Not Bad! Work Harder!
If sng. Exam. Score >= 80 Then bnt. Red. Demo. Text = “Not Bad!” Else If sng. Exam. Score >= 90 bnt. Red. Demo. Text = “Well Done!” Else bnt. Red. Demo. Text = “Work Harder!” End If Not Bad! Work Harder! Unreachable code
2 nd 0 10 20 30 40 50 60 70 80 1 st 90 1 st 0 10 20 30 40 50 60 70 80 2 nd 0 10 20 30 40 50 60 70 100
We can have as many Else If’s as we want … If (Condition) Then Do Something Else. If (Condition 2) Then Do Something Else. If (Condition 3) Then Do Something Else. . . Else Do Something Else End If
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
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 sht. Fav. Number As Short sht. Fav. Number = …. ‘Assume we ask for a number between 0 and 10 Select Case sht. Fav. Number Case 7 bnt. Red. Demo. Text = “Seven is my favorite too!” Case Else bnt. Red. Demo. Text = “That’s different to my favorite” End Select
Select Case sht. Fav. Number Case 1 , 3 , 5 , 9 bnt. Red. Demo. Text = “Odd number!” Case 7 bnt. Red. Demo. Text = “Odd number, and my favorite too” Case 0, 2 , 4 , 6 , 8 bnt. Red. Demo. Text = “Even number” Case Else bnt. Red. Demo. Text = “Error!” End Select
Dim str. User. Input As String str. User. Input = “Lenny” Select Case str. User. Input Case “Homer”, “Marge”, “Maggie”, “Lisa”, “Bart” bnt. Red. Demo. Text = “Simpson Clan” Case “Monty”, “Waylon”, “Carl”, “Lenny” bnt. Red. Demo. Text = “Power. Plant Worker” Case Else bnt. Red. Demo. Text = “Other” End Select
- Slides: 41