Click for Table of Contents
The C Primer
Book and Reference Collection
Copyright © 2024 by Rance D. Necaise
Table Of Contents
The C Primer
Copyright © 2024
Rance D. Necaise

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.

OperatorMath NotationDescription
> > greater than
>= greater than or equal
< < less than
<= less than or equal
== = equal
!= not equal
Table 1: The relational operators in C.

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

x * 20 < y * 5

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

letter == 'A'

which determines if the character contained in the char variable {:letter:} is the uppercase letter A.

Question 1

Which of the following conditions evaluate to true, provided a is 3 and b is 4?

Select the correct answer by clicking on the appropriate button.
  1. a + 1 <= b

    Because a + 1 is 4, the condition reduces to 4 <= 4, which is true.

  2. a > b - 1
    Because b - 1 is 3, the condition reduces to 3 > 3, which is not true. The two sides are equal.
  3. a >= b - 1
    Both sides of the relational operator have a value of 3. Thus, they are equal.
  4. a * 2 != b + 2
    Both a * 2 and b + 2 are equal to 6. Thus both sides of the relational operator are equal.
Question 2

Provide the logical expression for each of the given tasks.

Task Condition Explanation

Give the opposite of the condition avg > 90

avg <= 90 Note that the opposite of > is <= and not <.

A programmer wrote the condition value = 50 to test whether the variable contains 50. Fix the error.

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

OperatorDescription
! Boolean not
&& Boolean and
|| Boolean or
Table 2: The Boolean operators in C.
NOTE
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 bool data type and the named literal values true and false did not exist. Instead, C assumed a value of 0 is false and any non-zero value is true and that is still the case in the current version of the language. Today, however, you can use the literal Boolean values since internally false has a value of 0 and true has a value of 1.

Question 3

Assume that n is 5 and k is 2. What is the Boolean result of each of the following logical expressions?

Select the correct answer by clicking on the appropriate button.
  1. n >= 0 || n < k
    The first condition is true.
  2. n >= 0 && k > 0
    Both conditions are true.
  3. !(n <= 5)
    The logical expression n <= 5 is true, but when we take the Boolean not of the result, it is inverted and becomes false.
  4. !(n < 5)
    The logical expression n < 5 is false and the Boolean not of false is true.
  5. !(n >= 0 && n <= k)
    The logical expression n >= 0 && n <= k is false since n is greater than k, but applying the Boolean not to the result of that expression produces an overall result of true.
  6. n && k > 0
    Both conditions are true because C treats a non-zero value as true and since n contains 5, the left hand side of the Boolean and operation is true.
  7. 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)).
Question 4

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.
Page last modified on February 10, 2024, at 02:07 PM