Transcript Section 4.1 - Kennewick High School
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 32 (space) 48 0 33 ! 34 “ 49 1 57 9 35 # 65 A 162 ¢ 169 © 176 ° 177 ± 178 ² 179 ³ 66 B 90 Z 97 a 98 b 122 z 123 { 125 } 126 ~ 181 µ 188 ¼ 189 ½ 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) Chr(162) is is A ¢
9
Asc 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
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