chapter 5 decisions. outline and objectives relational and logical operators if blocks select case...
Post on 18-Dec-2015
221 views
TRANSCRIPT
![Page 1: Chapter 5 Decisions. Outline and Objectives Relational and Logical Operators If Blocks Select Case Blocks](https://reader030.vdocuments.net/reader030/viewer/2022032703/56649d235503460f949f90a4/html5/thumbnails/1.jpg)
Chapter 5
Decisions
![Page 2: Chapter 5 Decisions. Outline and Objectives Relational and Logical Operators If Blocks Select Case Blocks](https://reader030.vdocuments.net/reader030/viewer/2022032703/56649d235503460f949f90a4/html5/thumbnails/2.jpg)
Outline and Objectives
Relational and Logical Operators If Blocks Select Case Blocks
![Page 3: Chapter 5 Decisions. Outline and Objectives Relational and Logical Operators If Blocks Select Case Blocks](https://reader030.vdocuments.net/reader030/viewer/2022032703/56649d235503460f949f90a4/html5/thumbnails/3.jpg)
Condition
A condition is a Boolean expression that can be either true or false
Conditions can be formed by using the six Relational operators and the three Logical operators
![Page 4: Chapter 5 Decisions. Outline and Objectives Relational and Logical Operators If Blocks Select Case Blocks](https://reader030.vdocuments.net/reader030/viewer/2022032703/56649d235503460f949f90a4/html5/thumbnails/4.jpg)
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
![Page 5: Chapter 5 Decisions. Outline and Objectives Relational and Logical Operators If Blocks Select Case Blocks](https://reader030.vdocuments.net/reader030/viewer/2022032703/56649d235503460f949f90a4/html5/thumbnails/5.jpg)
Examples
1<=1 True1 < 1 False2 < 5 True3 <> 3 False0 < 3.5 True5 <= 3 False
![Page 6: Chapter 5 Decisions. Outline and Objectives Relational and Logical Operators If Blocks Select Case Blocks](https://reader030.vdocuments.net/reader030/viewer/2022032703/56649d235503460f949f90a4/html5/thumbnails/6.jpg)
Examples
Determine whether each of the following conditions is true or false
assume that: •a = 4, b = 3, c = “hello” , d = “bye”
(a+b) < 2 * a True
(Len(c) – b) = (a / 2) True
![Page 7: Chapter 5 Decisions. Outline and Objectives Relational and Logical Operators If Blocks Select Case Blocks](https://reader030.vdocuments.net/reader030/viewer/2022032703/56649d235503460f949f90a4/html5/thumbnails/7.jpg)
Computers use a special coding system to compare character strings called ANSI
With ANSI each character has a number associated with it, this number is called ANSI value of the character.
The ANSI values of characters are given in following table. (Appendix A)
You do not need to memorize the table
Just know the numbers, small and capital lettersA= 65 a=97 sp=32 0=48
Using Relational Operators on Strings
![Page 8: Chapter 5 Decisions. Outline and Objectives Relational and Logical Operators If Blocks Select Case Blocks](https://reader030.vdocuments.net/reader030/viewer/2022032703/56649d235503460f949f90a4/html5/thumbnails/8.jpg)
8
Punctuation < Capitals letters < Small letters
![Page 9: Chapter 5 Decisions. Outline and Objectives Relational and Logical Operators If Blocks Select Case Blocks](https://reader030.vdocuments.net/reader030/viewer/2022032703/56649d235503460f949f90a4/html5/thumbnails/9.jpg)
Using Relational Operators on Strings
The string str1 is said to be less than the string str2 if str1 precedes str2 alphabetically when using the ANSI (or ASCII) table
Two strings are compared working from left to right, character by character, to determine which one should precede the other.
![Page 10: Chapter 5 Decisions. Outline and Objectives Relational and Logical Operators If Blocks Select Case Blocks](https://reader030.vdocuments.net/reader030/viewer/2022032703/56649d235503460f949f90a4/html5/thumbnails/10.jpg)
Example of Comparing Character Strings
“Hope” < “Hopeful”
“Chase” < “Chaz” True
“ Cat” < “Cat” True
“Pay” < “Pay “ True
“Jones” <> “James” True
True
![Page 11: Chapter 5 Decisions. Outline and Objectives Relational and Logical Operators If Blocks Select Case Blocks](https://reader030.vdocuments.net/reader030/viewer/2022032703/56649d235503460f949f90a4/html5/thumbnails/11.jpg)
Example…
Determine whether the following condition is true or false
(assume that c = “hello” and d = “bye”)
c < (“good” & d) False
![Page 12: Chapter 5 Decisions. Outline and Objectives Relational and Logical Operators If Blocks Select Case Blocks](https://reader030.vdocuments.net/reader030/viewer/2022032703/56649d235503460f949f90a4/html5/thumbnails/12.jpg)
Logical Operators
The result of a logical operator is also True or False
The three Logical Operators are: Not And Or
![Page 13: Chapter 5 Decisions. Outline and Objectives Relational and Logical Operators If Blocks Select Case Blocks](https://reader030.vdocuments.net/reader030/viewer/2022032703/56649d235503460f949f90a4/html5/thumbnails/13.jpg)
Not Not: Negates a single expression Example: Suppose answ = “Y”
Not (answ = “y”) is True
X Not X
F T
T F
![Page 14: Chapter 5 Decisions. Outline and Objectives Relational and Logical Operators If Blocks Select Case Blocks](https://reader030.vdocuments.net/reader030/viewer/2022032703/56649d235503460f949f90a4/html5/thumbnails/14.jpg)
And Takes two expressions, returns True only if
both expressions are evaluated as being true
Example: Suppose answ = “Y”
•(answ = “Y”) And (answ = “y”) is False
X Y X And Y
F F F
F T F
T F F
T T T
![Page 15: Chapter 5 Decisions. Outline and Objectives Relational and Logical Operators If Blocks Select Case Blocks](https://reader030.vdocuments.net/reader030/viewer/2022032703/56649d235503460f949f90a4/html5/thumbnails/15.jpg)
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
X Y X Or Y
F F F
F T T
T F T
T T T
![Page 16: Chapter 5 Decisions. Outline and Objectives Relational and Logical Operators If Blocks Select Case Blocks](https://reader030.vdocuments.net/reader030/viewer/2022032703/56649d235503460f949f90a4/html5/thumbnails/16.jpg)
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
Chapter 5 - Visual Basic Schneider 16
Not ( X And Y ) = Not X OR Not Y Not ( X Or Y ) = Not X And Not Y
![Page 17: Chapter 5 Decisions. Outline and Objectives Relational and Logical Operators If Blocks Select Case Blocks](https://reader030.vdocuments.net/reader030/viewer/2022032703/56649d235503460f949f90a4/html5/thumbnails/17.jpg)
Precedence
First, all Arithmetic operations are carried out
Then all expressions involving >, <, and = are evaluated to true or false
The logical operations are next applied in the order Not, then And, and Finally Or.
![Page 18: Chapter 5 Decisions. Outline and Objectives Relational and Logical Operators If Blocks Select Case Blocks](https://reader030.vdocuments.net/reader030/viewer/2022032703/56649d235503460f949f90a4/html5/thumbnails/18.jpg)
Examples…
![Page 19: Chapter 5 Decisions. Outline and Objectives Relational and Logical Operators If Blocks Select Case Blocks](https://reader030.vdocuments.net/reader030/viewer/2022032703/56649d235503460f949f90a4/html5/thumbnails/19.jpg)
![Page 20: Chapter 5 Decisions. Outline and Objectives Relational and Logical Operators If Blocks Select Case Blocks](https://reader030.vdocuments.net/reader030/viewer/2022032703/56649d235503460f949f90a4/html5/thumbnails/20.jpg)
Evaluate the following to True or False
Assume a=4, b=3 Print a*3-2=5-b False Print a=b+3<=a+b True Print not (a<2) or b<4 = a+b True Print Not(a<b)=(a>=b+2)
FalseBe careful about the priorities
![Page 21: Chapter 5 Decisions. Outline and Objectives Relational and Logical Operators If Blocks Select Case Blocks](https://reader030.vdocuments.net/reader030/viewer/2022032703/56649d235503460f949f90a4/html5/thumbnails/21.jpg)
Notes A Condition such as 2<n<5 (not a
syntax error) but should never be used, because visual basic will not evaluate it as intended.
The correct condition is (2<n) And (n<5)
The following conditions are equivalent:a<>b Not (a=b)a>b Not (a<=b)Not (n<m) (n>=m)
![Page 22: Chapter 5 Decisions. Outline and Objectives Relational and Logical Operators If Blocks Select Case Blocks](https://reader030.vdocuments.net/reader030/viewer/2022032703/56649d235503460f949f90a4/html5/thumbnails/22.jpg)
Are these conditions equivalent?
Not(n<m); n>m n>=m
(a=b) and (a<b); a<>b try a=4 b=5
Not ((a=b) or (a=c)) ; a<>b and a<>c
Not (a>=b); (a<b) and not (a=b)
a<b is enough
Chapter 5 - Visual Basic Schneider 22
![Page 23: Chapter 5 Decisions. Outline and Objectives Relational and Logical Operators If Blocks Select Case Blocks](https://reader030.vdocuments.net/reader030/viewer/2022032703/56649d235503460f949f90a4/html5/thumbnails/23.jpg)
Write an Exp equal to the negation of
a>b a<b Or a=b a<=b (a<b) and (c<>d) (a>=b) Or (c=d) (a=b) or (a=d) (a<>b) and (a<>d) Not ( (a=b) or (a>b) ) (a=b) or (a>b)
(a<> “”) and (a<b) and (len(a) < 5)(a =“”) Or (a>=b) Or (len(a)>=5)
Chapter 5 - Visual Basic Schneider 23
![Page 24: Chapter 5 Decisions. Outline and Objectives Relational and Logical Operators If Blocks Select Case Blocks](https://reader030.vdocuments.net/reader030/viewer/2022032703/56649d235503460f949f90a4/html5/thumbnails/24.jpg)
Types of Decision Structures
If Block Statement•Single alternative: If...Then
•Compound alternative: If Then...Else
Select Case Statement
![Page 25: Chapter 5 Decisions. Outline and Objectives Relational and Logical Operators If Blocks Select Case Blocks](https://reader030.vdocuments.net/reader030/viewer/2022032703/56649d235503460f949f90a4/html5/thumbnails/25.jpg)
Single Alternative Decision An action is taken if the condition is
true, otherwise the control goes to the next statement.
Syntax:If condition Then actionEnd If
If condition is true, action is executed. Otherwise, action is skipped
![Page 26: Chapter 5 Decisions. Outline and Objectives Relational and Logical Operators If Blocks Select Case Blocks](https://reader030.vdocuments.net/reader030/viewer/2022032703/56649d235503460f949f90a4/html5/thumbnails/26.jpg)
Example
![Page 27: Chapter 5 Decisions. Outline and Objectives Relational and Logical Operators If Blocks Select Case Blocks](https://reader030.vdocuments.net/reader030/viewer/2022032703/56649d235503460f949f90a4/html5/thumbnails/27.jpg)
Example
![Page 28: Chapter 5 Decisions. Outline and Objectives Relational and Logical Operators If Blocks Select Case Blocks](https://reader030.vdocuments.net/reader030/viewer/2022032703/56649d235503460f949f90a4/html5/thumbnails/28.jpg)
Compound Alternative Decision
SyntaxIf condition Then action1 Else action 2End If
![Page 29: Chapter 5 Decisions. Outline and Objectives Relational and Logical Operators If Blocks Select Case Blocks](https://reader030.vdocuments.net/reader030/viewer/2022032703/56649d235503460f949f90a4/html5/thumbnails/29.jpg)
Example
![Page 30: Chapter 5 Decisions. Outline and Objectives Relational and Logical Operators If Blocks Select Case Blocks](https://reader030.vdocuments.net/reader030/viewer/2022032703/56649d235503460f949f90a4/html5/thumbnails/30.jpg)
Example
![Page 31: Chapter 5 Decisions. Outline and Objectives Relational and Logical Operators If Blocks Select Case Blocks](https://reader030.vdocuments.net/reader030/viewer/2022032703/56649d235503460f949f90a4/html5/thumbnails/31.jpg)
Example
![Page 32: Chapter 5 Decisions. Outline and Objectives Relational and Logical Operators If Blocks Select Case Blocks](https://reader030.vdocuments.net/reader030/viewer/2022032703/56649d235503460f949f90a4/html5/thumbnails/32.jpg)
Example
If cond1 Then If cond2 Then action(s) End IfEnd If
If cond1 And cond2 Then action(s)End If
This is easier to understandA confusing If Block
![Page 33: Chapter 5 Decisions. Outline and Objectives Relational and Logical Operators If Blocks Select Case Blocks](https://reader030.vdocuments.net/reader030/viewer/2022032703/56649d235503460f949f90a4/html5/thumbnails/33.jpg)
Compound Alternative Decision
SyntaxIf condition1 Then action1 ElseIf condition2 Then action 2 ElseIf condittion3 Then action3 Else action4End If
This block statement searches for the first True condition, carries out its action, and then skips to the statement following end if.
If none of the conditions is true, then else’s action is carried out.
In general, an IF block can contain any number of ElseIf clauses.
![Page 34: Chapter 5 Decisions. Outline and Objectives Relational and Logical Operators If Blocks Select Case Blocks](https://reader030.vdocuments.net/reader030/viewer/2022032703/56649d235503460f949f90a4/html5/thumbnails/34.jpg)
Example
![Page 35: Chapter 5 Decisions. Outline and Objectives Relational and Logical Operators If Blocks Select Case Blocks](https://reader030.vdocuments.net/reader030/viewer/2022032703/56649d235503460f949f90a4/html5/thumbnails/35.jpg)
Single Line If Statement
SyntaxIf condition Then action
There is no End If If the condition is true, the action will
be executed If the condition is false, the (else)
action will be taken
![Page 36: Chapter 5 Decisions. Outline and Objectives Relational and Logical Operators If Blocks Select Case Blocks](https://reader030.vdocuments.net/reader030/viewer/2022032703/56649d235503460f949f90a4/html5/thumbnails/36.jpg)
Example
![Page 37: Chapter 5 Decisions. Outline and Objectives Relational and Logical Operators If Blocks Select Case Blocks](https://reader030.vdocuments.net/reader030/viewer/2022032703/56649d235503460f949f90a4/html5/thumbnails/37.jpg)
Example
![Page 38: Chapter 5 Decisions. Outline and Objectives Relational and Logical Operators If Blocks Select Case Blocks](https://reader030.vdocuments.net/reader030/viewer/2022032703/56649d235503460f949f90a4/html5/thumbnails/38.jpg)
Examples
If 2 < n < 5 Then
action(s)
End If
Wrong (not a compiler error)
(n > 2) And (n < 5)
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
action
End If
If n <> 0 then
action(s)
End if
![Page 39: Chapter 5 Decisions. Outline and Objectives Relational and Logical Operators If Blocks Select Case Blocks](https://reader030.vdocuments.net/reader030/viewer/2022032703/56649d235503460f949f90a4/html5/thumbnails/39.jpg)
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
![Page 40: Chapter 5 Decisions. Outline and Objectives Relational and Logical Operators If Blocks Select Case Blocks](https://reader030.vdocuments.net/reader030/viewer/2022032703/56649d235503460f949f90a4/html5/thumbnails/40.jpg)
Select Case Block (Syntax)
Select Case selector Case value-list-1 action1 Case value-list-2 action2 ….. Case Else action of last resortEnd Select
![Page 41: Chapter 5 Decisions. Outline and Objectives Relational and Logical Operators If Blocks Select Case Blocks](https://reader030.vdocuments.net/reader030/viewer/2022032703/56649d235503460f949f90a4/html5/thumbnails/41.jpg)
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.
![Page 42: Chapter 5 Decisions. Outline and Objectives Relational and Logical Operators If Blocks Select Case Blocks](https://reader030.vdocuments.net/reader030/viewer/2022032703/56649d235503460f949f90a4/html5/thumbnails/42.jpg)
Example
![Page 43: Chapter 5 Decisions. Outline and Objectives Relational and Logical Operators If Blocks Select Case Blocks](https://reader030.vdocuments.net/reader030/viewer/2022032703/56649d235503460f949f90a4/html5/thumbnails/43.jpg)
Example
![Page 44: Chapter 5 Decisions. Outline and Objectives Relational and Logical Operators If Blocks Select Case Blocks](https://reader030.vdocuments.net/reader030/viewer/2022032703/56649d235503460f949f90a4/html5/thumbnails/44.jpg)
Example
![Page 45: Chapter 5 Decisions. Outline and Objectives Relational and Logical Operators If Blocks Select Case Blocks](https://reader030.vdocuments.net/reader030/viewer/2022032703/56649d235503460f949f90a4/html5/thumbnails/45.jpg)
Example
![Page 46: Chapter 5 Decisions. Outline and Objectives Relational and Logical Operators If Blocks Select Case Blocks](https://reader030.vdocuments.net/reader030/viewer/2022032703/56649d235503460f949f90a4/html5/thumbnails/46.jpg)
Example
![Page 47: Chapter 5 Decisions. Outline and Objectives Relational and Logical Operators If Blocks Select Case Blocks](https://reader030.vdocuments.net/reader030/viewer/2022032703/56649d235503460f949f90a4/html5/thumbnails/47.jpg)
Example
![Page 48: Chapter 5 Decisions. Outline and Objectives Relational and Logical Operators If Blocks Select Case Blocks](https://reader030.vdocuments.net/reader030/viewer/2022032703/56649d235503460f949f90a4/html5/thumbnails/48.jpg)
Example
![Page 49: Chapter 5 Decisions. Outline and Objectives Relational and Logical Operators If Blocks Select Case Blocks](https://reader030.vdocuments.net/reader030/viewer/2022032703/56649d235503460f949f90a4/html5/thumbnails/49.jpg)
what is the output of the following code
a = 3b = 4Select Case a = b
Case 3Print "A"Case 4Print "B"Case 1Print "C"Case 0Print "D"
End Select
Answer: D
![Page 50: Chapter 5 Decisions. Outline and Objectives Relational and Logical Operators If Blocks Select Case Blocks](https://reader030.vdocuments.net/reader030/viewer/2022032703/56649d235503460f949f90a4/html5/thumbnails/50.jpg)
what is the output of the following code
a = "hello"Select Case a
Case "apple" To "orange"Print "A"Case "Hello"Print "B"Case Is <= "z"Print "C"Case "hello"Print "D"
End SelectAnswer: A
![Page 51: Chapter 5 Decisions. Outline and Objectives Relational and Logical Operators If Blocks Select Case Blocks](https://reader030.vdocuments.net/reader030/viewer/2022032703/56649d235503460f949f90a4/html5/thumbnails/51.jpg)
what is the output of the following code
a = 0Select Case a
Case 3.5Print "A"Case a > 5Print "B"Case a - 1 To a + 1Print "C"Case 3Print "D"
End SelectAnswer: B
![Page 52: Chapter 5 Decisions. Outline and Objectives Relational and Logical Operators If Blocks Select Case Blocks](https://reader030.vdocuments.net/reader030/viewer/2022032703/56649d235503460f949f90a4/html5/thumbnails/52.jpg)
References
Chapter 5 - Visual Basic Schneider