Node:Logical operators, Next:Questions for Chapter 7, Previous:Comparisons and logic, Up:Expressions and operators
Comparisons are often made in pairs or groups. For example, you might
want to ask a question such as, "Is variable a
greater than
variable b
and is variable b
greater than variable
c
?" The word "and" in the question above is represented in
C by the logical operator (an "operator on Boolean values")
&&
, and the whole comparison above might be represented by the
following expression:
(a > b) && (b > c)
The main logical operators in C are as follows:
&&
||
!
Here is another example. The question, "Is the variable a
greater than the variable b
, or is the variable a
not greater than the variable c
?" might be written:
(a > b) || !(a > c)