Title: Chapter 4
1Chapter 4 Decisions
- 4.1 Relational and Logical Operators
- 4.2 If Blocks
- 4.3 Select Case Blocks
- 4.4 Input via User Selection
24.1 Relational and Logical Operators
- Conditions
- Relational Operators
- ANSI Values
- ASC and CHR Functions
- Logical Operators
- Using Boolean Variables
- Boolean Methods
3Condition
- A condition is an expression involving relational
and/or logical operators - The value of a condition is Boolean that is,
True or False
Conditional statements we make If it is rainy
outside, then I will read a book.
4Relational Operators
- lt less than
- lt less than or equal to
- gt greater than
- gt greater than or equal to
- equal to
- ltgt not equal to
Value of a relational expression will always be
True or False
5Example
- When a 3, b 4
- (a b) lt 2 a
2 3 6
3 4 7
7 is NOT less than 6 and so the value of the
expression is False
6Another Example
- a 4 b 3 c "hello"
- ( c.Length b ) ( a / 2 )
5 3 2
4 / 2 2
True because 2 equals 2
7Relational Operators and Strings
- ANSI values are used to decide order for strings
- Cat lt cat is True
- visual basic gt Magic is True
- car lt cat is True
- CAT ltgt cat is True
- cat lt catalog is True
See next slide / Appendix A for ANSI values
8ANSI Character Set (Appendix A)
- There is a numeric representation for every key
on the keyboard and for other assorted
characters.
9Chr Function
- Given n between 0 and 255,
- Chr(n)
- returns a string consisting of the character
with ANSI value n. - Examples Chr(65) is A
- Chr(162) is
-
10Asc Function
- For a string str,
- Asc(str)
- is the ANSI value of the first character of str.
- Examples Asc("A") is 65
- Asc("25") is 162
-
11ASCI to ANSI Conversion
- Problem Create a program to convert ASCI letters
to ANSI values and visa versa.
Public Class Converter Private Sub
btnAnsi2Ascii_Click() Handles btnAnsi2Ascii.Click
txtASCII.Text Chr(CInt(txtANSI.Text))
End Sub Private Sub btnAscii2Ansi_Click()
Handles btnAscii2Ansi.Click txtANSI.Text
CStr(Asc(txtASCII.Text)) End Sub End Class
12Logical Operators
- Used with Boolean expressions
- Not makes a False expression True and vice
versa - And will yield a True if and only if both
expressions are True - Or will yield a True if at least one of both
expressions are True
13Truth Tables
Truth tables help us evaluate logical operators
14Example 4.3
- n 4, answ Y Are the following
expressions true or false? - Not (n lt 6)
- (answ "Y") Or (answ "y")
- (answ "Y") And (answ "y")
- Not(answ "y")
15Boolean Expression
- An expression that evaluates to either True or
False is said to have Boolean data type. - Example
- The statement
- txtBox.Text CStr((2 3) lt 6)
- displays True in the text box.
16Boolean Variable
- The following statement declares var as a
Boolean data type. - Dim var As Boolean
- It can only be assigned one of two values
either True and False. - Example Dim boolVar As Boolean
- boolVar 2 lt 6
- txtBox.Text CStr(boolVar)
-
Displays True in the text box.
17Common Errors with Relational Operators
- The following is NOT a valid way to test whether
n falls between 2 and 5 - 2 lt n lt 5
- Instead use
- (2 lt n ) And ( n lt 5 )
A complete relational expression must be on
either side of the logical operators And and Or.
18Common Error in Boolean Expressions
- A common error is to replace the condition Not (
2 lt 3 ) with the condition ( 2 gt 3 ). - The correct replacement is ( 2 gt 3 )
- because gt is the opposite of lt, just as lt
is the opposite of gt.
19Boolean-Valued Method EndsWith
- The expression strVar1.EndsWith(strVar2)
- is true if the value of the first variable ends
with the value of the second variable. - Dim course As String Visual Basic"
- txtBox1.Text course.EndsWith(Basic")
- Note String literals can be used instead of
string variables
TRUE
20Boolean-Valued Method StartsWith
- The expression strVar1.StartsWith(strVar2)
- is true if the value of the first variable
begins with the value of the second variable. - Dim course As String Visual Basic"
- txtBox2.Text course.StartsWith(Visu")
TRUE
21Boolean-Valued FunctionIsNumeric
- The expression IsNumeric(strVar)
- is true if the value of strVar can be converted
to a number with CInt or CDbl. - Examples IsNumeric("123") is true
- IsNumeric("123") is true
- IsNumeric("3 - 2") is false
Note The string literal can be replaced with a
string variable.