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
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 letters A= 65 a=97 sp=32 0=48 Using Relational Operators on Strings
8 Punctuation < Capitals letters < Small letters
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.
Example of Comparing Character Strings “Hope” < “Hopeful” “Chase” < “Chaz”True “ Cat” < “Cat”True “Pay” < “Pay “True “Jones” <> “James”True
Example… Determine whether the following condition is true or false (assume that c = “hello” and d = “bye”) c < (“good” & d) False
Logical Operators The result of a logical operator is also True or False The three Logical Operators are: Not And Or
Not Not: Negates a single expression Example: Suppose answ = “Y” Not (answ = “y”) is True XNot X FT TF
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 XYX And Y FFF FTF TFF TTT
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 XYX Or Y FFF FTT TFT TTT
Truth Tables XYX And YX Or YNot X FFFFT FTFTT TFFTF TTTTF Chapter 5 - Visual Basic Schneider16 n Not ( X And Y ) = Not X OR Not Y n Not ( X Or Y ) = Not X And Not Y
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.
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+2) False Be careful about the priorities
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)
Are these conditions equivalent? Not(n m n>=m (a=b) and (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 Schneider22
Write an Exp equal to the negation of a>b a<b Or a=b a<=b (a 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 Schneider23
Types of Decision Structures If Block Statement Single alternative: If...Then Compound alternative: If Then...Else Select Case Statement
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
If cond1 Then If cond2 Then action(s) End If If cond1 And cond2 Then action(s) End If This is easier to understand A confusing If Block
Compound Alternative Decision Syntax If condition1 Then action1 ElseIf condition2 Then action 2 ElseIf condittion3 Then action3 Else action4 End 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.
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
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
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
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.