Chapter 5 Decisions 1

advertisement
Chapter 5
Decisions
Chapter 5 - Visual Basic
Schneider
1
Outline and Objectives
Relational
and Logical Operators
If
Blocks
Select Case Blocks
Chapter 5 - Visual Basic
Schneider
2
Relational Operators



The execution of an If block is controlled by a
condition
A condition can be either true or false
Conditions can be formed by using the six
Relational operators and the three Logical
operators
Chapter 5 - Visual Basic
Schneider
3
Relational Operators
Mathematical
Notation
=
≠
<
>
<=
>=
Visual Basic Notation
Numeric Meaning
=
<>
<
>
<=
>=
Equal to
Not equal to
Less than
Greater than
Less than or equal to
Greater than or equal to
Chapter 5 - Visual Basic
Schneider
4
Using Relational Operators on
Strings




Computers use a special coding system to
compare character strings called ANSI
The ANSI values of characters are given in
Appendix A. A snippet included in Slide 34
You do not need to memorize the table
Just know the numbers, small and capital letters
“0” = 48
“a” = 97
“A”= 65
Punctuation < Capitals letters < Small letters
Chapter 5 - Visual Basic
Schneider
5
Example of Comparing Character
Strings
“Chase” <
“Chaz”
True
“ Cat”
<
“Cat”
True
“Pay”
<
“Pay “
True
“Jones” <> “James”
True
“Hope”
True
< “Hopeful”
Chapter 5 - Visual Basic
Schneider
6
Logical Operators
The result of a logical operator is also
True or False
 The three Logical Operators are:
Not
And
Or
 Precedence?!

Chapter 5 - Visual Basic
Schneider
7
Not

Not: Negates a single expression

Example: Suppose answ = “Y”
Not (answ = “y”) is True
Chapter 5 - Visual Basic
Schneider
8
And

Takes two expressions, returns True only if both
expressions are evaluated as being true

Example:
 (answ
Suppose answ = “Y”
= “Y”) And (answ = “y”) is False
Chapter 5 - Visual Basic
Schneider
9
Or

Takes two expressions, returns true if either one of
the expressions is evaluated as being true.

Example: Suppose answ = “Y”
 (answ
= “Y”) Or (answ = “y”) is True
Chapter 5 - Visual Basic
Schneider
10
Determine whether the following conditions are
true or false?

(“Y” <> “X”) And (143.55 < 143.55)

(0 = 14) Or (6 ^ 2 - 3 <= 4 / 2 + 8)

(Not (6 = 7)) And (44 > 33)
Chapter 5 - Visual Basic
Schneider
11
Truth Tables
X
Y
X And Y
X Or Y
Not X
F
F
F
F
T
F
T
F
T
T
T
F
F
T
F
T
T
T
T
F
Not ( X And Y ) =
 Not ( X Or Y ) =

Chapter 5 - Visual Basic
Schneider
12
Determine if True or False
a = 2, b=3
 a*a < b Or Not a*a <a
 True
 ((a=b) Or Not (b<a)) And ((a<b) Or (b=a+1))
 True
 “9W” >= “9W” And “duck” < “Du” & ”ck”
 False

Chapter 5 - Visual Basic
Schneider
13
Are the conditions equivelent?
Not(a<m); n>m
 (a=b) and (a<b); a<>b
 Not ((a=b) or (a=c)) ; a<>b and a<>c
 Not (a>=b); (a<b) and not (a=b)

Chapter 5 - Visual Basic
Schneider
14
Write an Exp equal to the
negation of
a>b
 (a<b) and (c<>d)
 (a=b) or (a=d)
 Not ( (a=b) or (a>b) )
 (a<> “”) and (a<b) and (len(a) < 5)

Chapter 5 - Visual Basic
Schneider
15
Types of Decision Structures

If Block Statement
 Single
alternative: If...Then
 Compound alternative: If Then...Else

Select Case Statement
Chapter 5 - Visual Basic
Schneider
16
Single Alternative Decision



An action is taken if the condition is true,
otherwise the control goes to the next statement.
Syntax:
If condition Then
action
End If
If condition is true, action is executed. Otherwise,
action is skipped
Chapter 5 - Visual Basic
Schneider
17
Example of single-alternative decision
If (grade > = 90) Then
picOutput.Print “Excellent Student”
letterGrade = “A”
picOutput.Print “Your grade is “; letterGrade
End If
Chapter 5 - Visual Basic
Schneider
18
Compound Alternative Decision

Syntax
If condition Then
action1
Else
action 2
End If
Chapter 5 - Visual Basic
Schneider
19
Example of Compound If statement
Private Sub cmdFindLarger_Click()
Dim largerNum As Single
If Val(txtFirstNum.Text) > Val(txtSecondNum.Text) Then
largerNum = Val(txtFirstNum.Text)
Else
largerNum = Val(txtSecondNum.Text)
End If
picResult.Print "The larger number is"; largerNum
End Sub
Chapter 5 - Visual Basic
Schneider
20
Important Points in Using If
Statement
Use indentation
 In the nested If statement, each If must have
its own End If statement
 Care should be taken to make If blocks easy
to read and understand

Chapter 5 - Visual Basic
Schneider
21
Example
If cond1 Then
If cond2 Then
action(s)
End If
End If
If cond1 And cond2 Then
action(s)
End If
A confusing If Block
This is easier to understand
Chapter 5 - Visual Basic
Schneider
22
Compound Alternative Decision

Syntax
If condition1 Then
action1
ElseIf condition2 Then
action 2
ElseIf condittion3 Then
action3
Else
action4
End If
Chapter 5 - Visual Basic
Schneider
23
Example (find the larger of two numbers, and
report if the two numbers are equal)
Private Sub cmdFindLarger_Click()
picResult.Cls
If Val(txtFirstNum.Text) > Val(txtSecondNum.Text) Then
picResult.Print "The larger number is"; txtFirstNum.Text
ElseIf Val(txtSecondNum.Text) > Val(txtFirstNum.Text) Then
picResult.Print "The larger number is "; txtSecondNum.Text
Else
picResult.Print "The two numbers are equal."
End If
End Sub
Chapter 5 - Visual Basic
Schneider
24
Example (find cost of phone call from NY to LA)
Private Sub DisplayCost (length As Single)
PicOutput.Print "Cost of call: "; FormatCurrency(Cost(length))
End Sub
Private Function Cost (length As Single) As Single
If length < 1 Then
Cost = .46
Else
Cost = .46 + (length –1) * .36
End If
End Function
Chapter 5 - Visual Basic
Schneider
25
Single Line If Statement

Syntax
If condition Then action
There is no End If or Else blocks
 If the condition is true, the action will be
executed
 If the condition is false, no action will be
taken and the next statement will be
executed

Chapter 5 - Visual Basic
Schneider
26
Examples
If 5 Then
action(s)
End If
If n Then
action(s)
End If
If Not(n < m) Then
action(s)
End If
Equivalent to
If (n >= m) Then
If 2 < n < 5 Then
action(s)
End If
Wrong!
(n > 2) And (n < 5)
Chapter 5 - Visual Basic
Schneider
27
The Select Case Block
Similar to If statement
 Used instead of compound If statements
 Action is selected from a list of alternatives
 Avoids confusion of deeply nested If blocks

Chapter 5 - Visual Basic
Schneider
28
Select Case Block (Syntax)
Select Case selector
Case value-list-1
action1
Case value-list-2
action2
…..
Case Else
action of last resort
End Select
Chapter 5 - Visual Basic
Schneider
29
Select Case Block
Each value-list contains one or more of the following types of
items separated by a comma



a constant
a variable
an expression

an inequality sign preceded by Is and followed by
a constant, variable, or expression

a range expressed in the form a To b, where a and
b are constants, variables, or expressions.
Chapter 5 - Visual Basic
Schneider
30
Example of Select Case
letterGrade = txtGrade.text
Select Case letterGrade
Case “A”, “B”
picOutput.Print “ Good Work”
Case “C”
picOutput.Print “Average Work”
Case Else
picOutputPrint “Poor Work”
End Select
Chapter 5 - Visual Basic
Schneider
31
Example of Select Case
letterGrade = txtGrade.text
letterGrade = txtGrade.text
Select Case letterGrade
If(letterGrade = “A” or letterGrade=“B”) The
Case “A”, “B”
picOutput.Print “ Good Work”
picOutput.Print “ Good
Elseif (letterGrade=“C”) Then
Work”
picOutput.Print “Average Work”
Case “C”
Else
picOutput.Print “Average
picOutputPrint “Poor Work”
Work”
End if
Case Else
picOutputPrint “Poor
Work”
End Select
Chapter 5 - Visual Basic Schneider
32
Example of If statement
letterGrade = txtGrade.Text
If (letterGrade = “A”) Or (letterGrade = “B”) Then
picOutput.print “Good Work”
ElseIf (letterGrade = “C”) Then
picOutput.Print “Average Work”
Else
picOutput.Print “Poor Work”
End If
Chapter 5 - Visual Basic
Schneider
33
Several different types of value-list
Private Sub cmdInterpret_Click()
Dim x As Integer, y As Integer, num As Integer
x=2
num = Val(txtNumber.Text)
Select Case num
Case 3 - x, x
picPhrase.Print "Buckle my shoe."
Case Is <= 4
picPhrase.Print "Shut the door."
Case x + 3 To x * 3
picPhrase.Print "Pick up sticks."
Case Else
picPhrase.Print "Start all over again.“
End Select
End Sub
Chapter 5 - Visual Basic
Schneider
34
Selector Is a String Variable
Private Sub cmdInterpret_Click()
Select Case txtFirstName.Text
Case "Thomas"
picSolution.Print "Correct."
Case "Woodrow"
picSolution.Print "Sorry, his full name was"
picSolution.Print "Thomas Woodrow Wilson."
Case "President"
picSolution.Print "Are you for real?"
Case Else
picSolution.Print "Nice try, but no cigar."
End Select
End Sub
Chapter 5 - Visual Basic
Schneider
35
Char
SP
!
"
#
$
%
&
`
(
)
*
+
,
.
/
0
1
2
3
4
5
6
7
8
9
Value
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
Char
:
;
<
=
>
?
@
[
\
]
^
_
`
{
|
}
~
DEL
Value
58
59
60
61
62
63
64
Char
Value
A
65
B
66
C
67
D
68
E
69
F
70
G
71
H
72
91
I
73
92
J
74
93
K
75
94
L
76
95
M
77
96
N
78
O
79
123
P
80
124
Q
81
125
R
82
126
S
83
127
T
84
U
85
V
86
W
87
X
88
89
Chapter 5 - Visual Basic Y Schneider
Z
90
Char
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
Value
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
36
122
Download