Logical Operators
The logical operators are used to combine results of several comparisons, as required, to evaluate a single result. The logical operators also result in logical value either 'true' or 'false'. C provides three logical operators AND, OR, and NOT.
Logical AND (&&): Binary Operator
Operates on two values or conditional expressions, and results in 'true' only when both of the conditions expressions evaluate to true. The expressions are combined as:
(Expression1) && (Expression2)
The truth table of && operator:
A
|
B
|
A && B
|
T
|
T
|
T
|
T
|
F
|
F
|
F
|
T
|
F
|
F
|
F
|
F
|
Logical OR (||): Binary Operator
Operates on two values or conditional expressions, and results in 'true' when both of the conditional expressions or either of the two conditional expressions are evaluated to true. The expression syntax is:
(Expression1) || (Expression2)
The truth table of || operator:
A
|
B
|
A || B
|
T
|
T
|
T
|
T
|
F
|
T
|
F
|
T
|
T
|
F
|
F
|
F
|
Logical NOT (!): Unary Operator
Operates on a single value or expression and converts the evaluated value 'true' to 'false' and vice-versa. The expression syntax is:
! (Expression1)
The truth table of ! operator:
Precedence for logical operators:
Logical NOT (!)
Logical AND (&&)
Logical OR (||)