Title: Decisions
1Chapter 5
2Outline and Objective
- Relational and Logical Operators
- If Blocks
- Select Case Blocks
3Relational Operators
- The execution of the block If is controlled by a
Boolean Expression (called condition) - A condition can be either True or False
- Conditions are formed by using six Relational
operators and three Logical operators
4Relational Operators
5Using Relational Operators on Strings
- Computer uses a special coding system to compare
character strings - Usually ANSI (also called ASCII) coding system is
used on PC. - The association between characters and values of
characters are given in Appendix A.
6Example of Comparing Character Strings
- Chase lt Chaz True
- Cat lt Cat True
- Pay lt Pay True
- Jones ltgt James True
- Hope lt Hopeful True
- 9W lt bat True
- Dog lt cat True
7Logical Operators
- The result of a logical operator is also True or
False - The three Logical Operators are
- Not
- And
- Or
8Not
- Not Negates a single expression
- Syntax Not cond
- Example Suppose answ Y
- Not (answ y) is True
9And
- Combines two expressions each expression must
be True for the entire expression to be True.
- Syntax cond1 And cond2
- Example Suppose answ Y
- (answ Y) And (answ y) is False
10Or
- Combines two expressions either expression (or
both expressions) must be True for the entire
expression to be True. -
- Syntax cond1 Or cond2
- Example Suppose answ Y
- (answ Y) Or (answ y) is True
11Determine whether the following conditions are
true or false?
- (Y ltgt X) And (143.55 lt 143.55)
- ( 0 14) Or (6 2 - 3 lt 4 / 2 8)
- Not ( 6 7) And ( 44 gt 33)
12Logical Operators
13Control Structures
- Allows the programmer to alter the normal flow of
statement execution. - Types of control structures
- Loop
- Decision
14 Types of Decision Structures
- The Block If Statement
- Simple alternative if
- Compound alternative if.else
- Select Case Statement
15Single Alternative Decision
- An action is taken if the condition is true,
otherwise the control goes to the next statement.
16Single Alternative Decision
- Syntax
- If condition Then
- action(s)
- End If
- If condition is true, action(s) is executed
otherwise action(s) is skipped
17Example of single-alternative decision
- If (grade gt 90) Then
- picOutput.Print Excellent Student
- letterGrade A
- picOutput.Print Your grade is letterGrade
- End If
18Compound Alternative Decision
- Syntax
- If condition Then
- action1
- Else
- action 2
- End If
19Example of Compound If statement (find the
larger of two numbers)
- Private Sub cmdFindLarger_Click()
- Dim largerNum As Single
- picResult.Cls
- If Val(txtFirstNum.Text) gt Val(txtSecondNum.Tex
t) Then - largerNum Val(txtFirstNum.Text)
- Else
- largerNum Val(txtSecondNum.Text)
- End If
- picResult.Print "The larger number is"
largerNum - End Sub
20Important 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
understand
21Example
- If cond1 Then
- If cond2 Then
- action(s)
- End If
- End If
- If cond1 And cond2 Then
- action(s)
- End If
This is easier to understand
A confusing If Block
22Compound Alternative Decision
- Syntax
- If condition1 Then
- action1
- ElseIf condition2 Then
- action 2
- ElseIf condittion3 Then
- action3
- Else
- action4
- End If
23Example (find the larger of two numbers, and
report if the two numbers are equal)
- Private Sub cmdFindLarger_Click()
- picResult.Cls
- If Val(txtFirstNum.Text) gt
Val(txtSecondNum.Text) Then - picResult.Print "The larger number is"
txtFirstNum.Text - ElseIf Val(txtSecondNum.Text) gt
Val(txtFirstNum.Text) Then - picResult.Print "The larger number is "
txtSecondNum.Text - Else
- picResult.Print "The two numbers are
equal." - End If
- End Sub
24The Select Case Block
- Similar to If statement
- Used instead of nested If statements
- An action to be selected from a list of
alternatives - Avoids confusion of deeply nested If blocks
25 Select Case Block (Syntax)
- Select Case selector
- Case valueList1
- action1
- Case valueList2
- action2
- ..
- Case Else
- action of last resort
- End Select
26Select Case Block
- Each value-list contains one or more of the
following types of items separated by commas - 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.
27Example 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
28Example 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
29Several different types of value-list
- Private Sub cmdInterpret_Click() Dim x As
Integer, y As Integer, num As Integer ' One,
Two, Buckle My Shoe picPhrase.Cls x 2
y 3 num Val(txtNumber.Text) Select Case
num Case y - x, x
picPhrase.Print "Buckle my shoe." Case
Is lt 4 picPhrase.Print "Shut the
door." Case x y To x y
picPhrase.Print "Pick up sticks." Case
7, 8 picPhrase.Print "Lay them
straight." Case Else
picPhrase.Print "Start all over again." End
SelectEnd Sub
30Selector Is a String Variable
- Private Sub cmdInterpret_Click()
- Dim firstName As String
- firstName txtAnswer.Text
- Select Case firstName
- 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
31Summary Points
- How to use If statements
- Select Case statements
- How to use relational operators (lt, gt, , ltgt, lt,
and gt - How to use logical operators (Not, And, and Or)
in Boolean expressions