Two Boolean values: true and false
Used to control flow of control in programs
C has no Boolean data type--we substitute "boolean-valued" integer variables.
Two kinds of operators in C return boolean values:
1. Arithmetic:
Relational operators | |
< | less than |
<= | less than or equal to |
== | equal to |
>= | greater than or equal to |
> | greater than |
!= | not equal |
Example
int x, y;
... /* initialize x and
y somehow */
The expression x < y is a boolean expression; it returns true if the value stored in x
is less than the value stored in y.
The operands for relational operands are typically numeric (int or float), but we also use them to compare characters.
Two kinds of operators in C return boolean values:
2. Logical:
Logical operators | |
&& | AND |
|| | OR |
! | NOT |
Logical operators take boolean operands and return a boolean result.
&& and || take two operands
! takes one operand
The operands for logical operators can be:
Examples:
x > y && y > z /* x and y are numeric types */
x && y /* x and y are boolean-valued variables */
(x && y) || !z /* x, y and z are boolean-valued variables */
Truth tables for logical operators:
|
|
|
Simplification of Boolean Expressions
Simplification requires the use of equivalences:
Example:
!((!x && y) && (x || !y)
Û !(!x && y) || ! (x || !y) (by g)
Û !(!x) || !y || !(x || !y) (by f)
Û !(!x) || !y || !x && !(!y) (by g)
Û x || !y || !x && y (by h, applied twice)