|
Chapter 3. Selection |
3.1 Logical Expressions
Decisions in a programming language are made based on the Boolean result (true or false) of a logical expression. Logical expressions are constructed using logical operators, which can be divided into two groups: relational operators and Boolean operators. The logical expressions are used with selection statements to make decisions.
Relational Operators
Relational operators are used to determine if a given relationship exists between pairs of values of the same data type. For example, suppose we need to determine if a student has a failing grade where failing is any average below 60. The logical expression for that evaluation would be written as
avg < 60
the result of which is a Boolean value of either true or false. In C, the relational operators can only be used with the primitive numerical data types. Table 1 list the six relational operators available in C.
Operator | Math Notation | Description |
---|---|---|
> |
> | greater than |
>= |
≥ | greater than or equal |
< |
< | less than |
<= |
≤ | less than or equal |
== |
= | equal |
!= |
≠ | not equal |
Mathematical Expressions
The relational operators can be used with any combination of variables and values, including the results of mathematical expressions. For example, assuming the integer variables x
and y
have been declared, the logical expression below compares the results of two mathematical operations
<codeblock> x * 20 < y * 5 </codeblock>
Here, the value of x
is multiplied by 20 and the result is compared against the result of multiplying y
by 5.
Comparing Characters
As you learned earlier, characters in C are stored as 1-byte integer values, with each character represented by an ASCII value. Thus, characters can also be compared using the relational operators. Consider the following expression
<codeblock> letter == 'A' </codeblock>
which determines if the character contained in the char
variable {:letter:} is the uppercase letter A.
Which of the following conditions evaluate to true, provided a
is 3 and b
is 4?
a + 1 <= b
Because
a + 1
is 4, the condition reduces to4 <= 4
, which is true.a > b - 1
Becauseb - 1
is 3, the condition reduces to3 > 3
, which is not true. The two sides are equal.a >= b - 1
Both sides of the relational operator have a value of 3. Thus, they are equal.a * 2 != b + 2
Botha * 2
andb + 2
are equal to 6. Thus both sides of the relational operator are equal.
Provide the logical expression for each of the given tasks.
Task | Condition | Explanation |
---|---|---|
Give the opposite of the condition
|
avg <= 90 |
Note that the opposite of > is <= and not < . |
A programmer wrote the condition
|
value == 50 |
You need to use == , not = , to compare whether two values are equal. |
Test whether the char variable response does not contain the uppercase letter Y . |
The == operator would be used to test whether a variable contains a specific value, thus, the != is used to test for the opposite, that it does not contain a specific value. |
Boolean Operators
Operator | Description |
---|---|
! |
Boolean not |
&& |
Boolean and |
|| |
Boolean or |
NOTE
| |
![]() | Technically, the result of a relational or Boolean operator is a Boolean value of either true or false. In C, the relational and Boolean operators actually return a value of 0 for false and 1 for true. In the original version of the C language, there was no |
Assume that n
is 5 and k
is 2. What is the Boolean result of each of the following logical expressions?
n >= 0 || n < k
The first condition is true.n >= 0 && k > 0
Both conditions are true.!(n <= 5)
The logical expressionn <= 5
is true, but when we take the Boolean not of the result, it is inverted and becomes false.!(n < 5)
The logical expressionn < 5
is false and the Boolean not of false is true.!(n >= 0 && n <= k)
The logical expressionn >= 0 && n <= k
is false sincen
is greater thank
, but applying the Boolean not to the result of that expression produces an overall result of true.n && k > 0
Both conditions are true because C treats a non-zero value as true and sincen
contains 5, the left hand side of the Boolean and operation is true.n > 5 && n < k || k < 10
The&&
operator has a higher precedence than||
. The expression is evaluated in the order((5 < n && n < k) || (k < 10))
.
Suppose the integer variables x
and y
contain previously assigned values. Provide a logical expression, using the &&
and ||
operators, for each of the following tests. Do not use the Boolean !
operator. But you can use the not equal !=
relational operator to check whether a number is not equal to zero. Only use parentheses if needed to override the order of operator precedence.
Test | Expression | Explanation |
---|---|---|
Both x and y are zero. |
x == 0 && y == 0 |
The and operator indicates that both conditions must be true. |
At least one of the two variables them is zero. | x == 0 || y == 0 |
The || operator indicates that at least one condition must be true. |
At most one of the two variables is zero. | x != 0 || y != 0 |
If at most one of them is zero, at least one of them must be onzero. |
Neither of them is zero. | x != 0 && y != 0 |
Both of them must be nonzero. |
x is zero, but y is not zero. |
x == 0 && y != 0 | Both conditions must be true, thus, the && operator is required. |
Next ⮊ |