5.1. Relational and Logical OperatorsIn Chapter 2, we discussed the two logical programming constructs decision and loop. In order to make a decision or control a loop, you need to specify a condition that determines the course of action. A condition is an expression involving relational operators (such as <and= ) that is either true or false. Conditions also may incorporate logical operators (such as And, Or, and Not). ANSI values determine the order used to compare strings with the relational operators. Boolean variables and literals can assume the values True or False. ANSI ValuesEach of the 47 different keys in the center typewriter portion of the keyboard can produce two characters, for a total of 94 characters. Adding 1 for the space character produced by the space bar makes 95 characters. These characters have numbers ranging from 32 to 126 associated with them. These values, called the ANSI (or ASCII) values of the characters, are given in Appendix A. Table 5.1 shows a few of the values.
Most of the best known fonts, such as Courier New, Microsoft San Serif, and Times New Roman, are essentially governed by the ANSI standard, which assigns characters to the numbers from 0 to 255. Table 5.2 shows a few of the higher ANSI values.
If n is a number between 0 and 255, then Chr(n) is the string consisting of the character with ANSI value h. If str is any string, then Asc(str) is the ANSI value of the first character of str. For instance, the statement txtBox.Text = Chr(65) displays the letter A in the text box, and the statement lstBox.Items.Add(Asc("Apple")) displays the number 65 in the list box. Concatenation can be used with Chr to obtain strings using the higher ANSI characters. For instance, with one of the fonts that conforms to the ANSI standard, the statement txtBox.Text = "32" & Chr(176) & " Fahrenheit" displays 32° Fahrenheit in the text box. The quotation-mark character (") can be placed into a string by using Chr(34). For example, after the statement txtBox.Text = "George " & Chr(34) & "Babe" & Chr(34) & " Ruth" is executed, the text box contains George "Babe" Ruth The relational operator less than(<) can be applied to both numbers and strings. The number a is said to be less than the number b if a lies to the left of b on the number line. For instance, 2 < 5, -5 < -2, and 0 < 3.5. The string a is said to be less than the string b if a precedes b alphabetically when using the ANSI table to alphabetize their values. For instance, "cat" < "dog", "cart" < "cat", and "cat" < "catalog". Digits precede uppercase letters, which precede lowercase letters. Two strings are compared working from left to right, character by character, to determine which one should precede the other. Therefore, "9W" < "bat", "Dog" < "cat", and "Sales-99" < "Sales-retail". Table 5.3 shows the different mathematical relational operators, their representations in Visual Basic, and their meanings.
Example 1. |
Determine whether each of the following conditions is true or false.
Solution
Conditions also can involve variables, numeric operators, and functions. To determine whether a condition is true or false, first compute the numeric or string values and then decide if the resulting assertion is true or false. |
Suppose the numeric variables a and b have values 4 and 3, and the string variables c and d have values "hello" and "bye". Are the following conditions true or false?
Solution
|
Programming situations often require more complex conditions than those considered so far. For instance, suppose we would like to state that the value of a numeric variable, n, is strictly between 2 and 5. The proper Visual Basic condition is
(2 < n) And (n < 5)
The condition (2 < n) And (n < 5) is a combination of the two conditions 2 < n and n < 5 with the logical operator And.
The three main logical operators are And, Or, and Not. If cond1 and cond2 are conditions, then the condition
cond1 And cond2
is true if both cond1 and cond2 are true. Otherwise, it is false. The condition
cond1 Or cond2
is true if either cond1 or cond2 (or both) is true. Otherwise, it is false. The condition
Not cond1
is true if cond1 is false, and is false if cond1 is true.
Suppose the numeric variable n has value 4 and the string variable answ has value "Y". Determine whether each of the following conditions is true or false.
Solution
|
A statement of the form
txtBox.Text = condition
will display either True or False in the text box, depending on the truth value of the condition. Any variable or expression that evaluates to either True or False is said to have a Boolean data type. The following lines of code display False in the text box.
Dim x As Integer = 5 txtBox.Text = (3 + x) < 7
A variable is declared to be of type Boolean with a statement of the form
Dim varName As Boolean
The following lines of code will display True in the text box.
Dim boolVar as Boolean Dim x As Integer = 2 Dim y As Integer = 3 boolVar = x < y txtBox.Text = boolVar
The answer to part (i) of Example 3 can be confirmed to be "false" by executing the following lines of code.
Dim n as Integer = 4 Dim answ as String = "Y" txtBox.Text = (n = 2) And ((n = 7) Or (answ = "Y"))
A condition involving numeric variables is different from an algebraic truth. The assertion (a + b) < 2*a, considered in Example 2, is not a valid algebraic truth because it isn't true for all values of a and b. When encountered in a Visual Basic program, however, it will be considered true if it is correct for the current values of the variables.
Conditions evaluate to either true or false. These two values often are called the possible truth values of the condition.
A condition such as 2 < n < 5 should never be used, because Visual Basic will not evaluate it as intended. The correct condition is (2 < n) And (n < 5).
A common error is to replace the condition Not (n < m) by the condition (n > m). The correct replacement is (n >= m).
1. | Is the condition "Hello" = "Hello" true or false? | |||||||||||||||||||||||||
2. | Complete Table 5.4.
|
In Exercises 1 through 6, determine the output displayed in the text box.
1. | txtBox.Text = Chr(104) & Chr(105) |
2. | txtBox.Text = "C" & Chr(35) |
3. | txtBox.Text = "The letter before G is" & Chr(Asc("G") - 1) |
4. | txtBox.Text = Chr(Asc("B")) 'The ANSI value of B is 66 |
5. |
Dim quote, person, qMark As String quote = "We're all in this alone." person = "Lily Tomlin" qMark = Chr(34) txtBox.Text = qMark & quote & qMark & "_" & person |
6. |
Dim letter As String letter = "D" txtBox.Text = letter & " is the " & (Asc(letter) - Asc("A") + 1) & _ "th letter of the alphabet." |
In Exercises 7 through 18, determine whether the condition is true or false. Assume a = 2 and b = 3.
7. | 3 * a = 2 * b |
8. | (5 - a) * b < 7 |
9. | b <= 3 |
10. | a^b = b^a |
11. | a^(5 - 2) > 7 |
12. | 3E-02 <.01 * a |
13. | (a < b) or (b < a) |
14. | (a * a < b) Or Not (a * a < a) |
15. | Not ((a < b) And (a < (b + a))) |
16. | Not (a < b) Or Not (a < (b + a)) |
17. | ((a = b) And (a * a < b * b)) Or ((b < a) And (2 * a < b)) |
18. | ((a = b) Or Not (b < a)) And ((a < b) Or (b = a + 1)) |
In Exercises 19 through 30, determine whether the condition is true or false.
19. | "9W" < > "9w" |
20. | "Inspector" < "gadget" |
21. | "Car" < "Train" |
22. | "J" > = "J" |
23. | "99" > "ninety-nine" |
24. | "B" > "?" |
25. | ("Duck" < "pig") And ("pig" < "big") |
26. | "Duck" < "Duck" & "Duck" |
27. | Not (("B" = "b") Or ("Big" < "big")) |
28. | Not ("B" = "b") And Not ("Big" < "big") |
29. | (("Ant" < "hill") And ("mole" > "hill")) Or Not (Not ("Ant" < "hill") Or Not ("Mole" > "hill")) |
30. | (7 < 34) And ("7" > "34") |
In Exercises 31 through 40, determine whether or not the two conditions are equivalentthat is, whether they will be true or false for exactly the same values of the variables appearing in them.
31. | a <= b; (a < b) Or (a = b) |
32. | Not (a < b); a > b |
33. | (a = b) And (a < b); a <> b |
34. | Not ((a = b) Or (a = c)); (a <> b) And (a <> c) |
35. | (a < b) And ((a > d) Or (a > e)); ((a < b) And (a > d)) Or ((a < b) And (a > e)) |
36. | Not ((a = b + c) Or (a = b)); (a <> b) Or (a <> b + c) |
37. | (a < b + c) Or (a = b + c); Not ((a > b) Or (a > c)) |
38. | Not (a >= b); (a <= b) Or Not (a = b) |
39. | Not (a >= b); (a <= b) And Not (a = b) |
40. | (a = b) And ((b = c) Or (a = c)); (a = b) Or ((b = c) And (a = c)) |
In Exercises 41 through 45, write a condition equivalent to the negation of the given condition. (For example, a <> b is equivalent to the negation of a = b.)
41. | a > b |
42. | (a = b) Or (a = d) |
43. | (a < b) And (c <> d) |
44. | Not ((a = b) Or (a > b)) |
45. | (a <> "") And (a < b) And (a.Length < 5) |
46. | Rework Exercise 20 by evaluating a Boolean expression. |
47. | Rework Exercise 21 by evaluating a Boolean expression. |
48. | Rework Exercise 22 by evaluating a Boolean expression. |
49. | Rework Exercise 23 by evaluating a Boolean expression. |
1. | False. The first string has six characters, whereas the second has five. Two strings must be 100% identical to be called equal. | |||||||||||||||||||||||||
2. |
|