Section 4.1 - Kennewick High School

advertisement
Chapter 4 – Decisions
4.1 Relational and Logical Operators
4.2 If Blocks
4.3 Select Case Blocks
4.4 Input via User Selection
1
4.1 Relational and Logical
Operators
•
•
•
•
•
•
•
Conditions
Relational Operators
ANSI Values
ASC and CHR Functions
Logical Operators
Using Boolean Variables
Boolean Methods
2
Condition
• 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.”
3
Relational Operators
<
<=
>
>=
=
<>
less than
less than or equal to
greater than
greater than or equal to
equal to
not equal to
Value of a relational expression will always be
True or False
4
Example
When a = 3, b = 4
(a + b) < 2 * a
3+4=7
2*3=6
7 is NOT less than 6 and so the value of
the expression is False
5
Another Example
a = 4 b = 3 c = "hello"
( c.Length – b ) = ( a / 2 )
5–3=2
4/2=2
True because 2 equals 2
6
Relational Operators and Strings
• ANSI values are used to decide order for
strings
•
•
•
•
•
“Cat” < “cat”
“visual basic” > “Magic”
“car” < “cat”
“CAT” <> “cat”
“cat” < “catalog”
is True
is True
is True
is True
is True
See next slide / Appendix A for ANSI values
ANSI Character Set (Appendix A)
There is a numeric representation for every
key on the keyboard and for other assorted
characters.
32
33
34
35
(space)
!
“
#
162 ¢
169 ©
176 °
48
49
57
65
177 ±
178 ²
179 ³
0
1
9
A
66
90
97
98
B
Z
a
b
181 µ
188 ¼
189 ½
122
123
125
126
z
{
}
~
190 ¼
247 ÷
248 ø
8
Chr 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 ¢
9
Asc Function
For a string str,
Asc(str)
is the ANSI value of the first character of str.
Examples:
is 65
Asc("¢25") is 162
Asc("A")
10
ASCI 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
Logical 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
12
Truth Tables
Truth tables help
us evaluate logical
operators
Example 4.3
n = 4, answ = “Y” Are the following
expressions true or false?
Not (n < 6)
(answ = "Y") Or (answ = "y")
(answ = "Y") And (answ = "y")
Not(answ = "y")
14
Boolean 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) < 6)
displays True in the text box.
15
Boolean 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 < 6
txtBox.Text = CStr(boolVar)
Displays True in the text box.
16
Common Errors with
Relational Operators
The following is NOT a valid way to
test whether n falls between 2 and 5:
2<n<5
Instead use:
(2 < n ) And ( n < 5 )
A complete relational expression must be on
either side of the logical operators And and Or.
17
Common Error in
Boolean Expressions
•
A common error is to replace the
condition Not ( 2 < 3 ) with the
condition ( 2 > 3 ).
•
The correct replacement is ( 2 >= 3 )
because >= is the opposite of <, just
as <= is the opposite of >.
18
Boolean-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")
TRUE
Note: String literals can be used instead of string
variables
19
Boolean-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
20
Boolean-Valued Function
IsNumeric
• 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.
21
Download