Booleans, Relational and Logical Operators
C does NOT have a Bool data type
but the ideas of TRUE and FALSE are necessary in any language.
In C, 0 is false, and anything other than 0 is true.
Variables that will only contain "true" or "false" are
called Boolean variables and are sometimes called "flags" (ugh!)
Boolean operators
Operators on boolean values consist of:
- relational operators: >, <, >=, <=, ==, !=
- logical operators: !, &&, ||
Truth Tables for AND, OR and NOT
We define boolean operators by truth tables
which give the desired output for every possible combination of
inputs.
Truth table - (P and Q)
P | Q | P && Q |
FALSE | FALSE | FALSE |
FALSE | TRUE | FALSE |
TRUE | FALSE | FALSE |
TRUE | TRUE | TRUE |
Truth table - (P or Q)
p | Q | P || Q |
FALSE | FALSE | FALSE |
FALSE | TRUE | TRUE |
TRUE | FALSE | TRUE |
TRUE | TRUE | TRUE |
Truth table -
not P
P | ! P |
FALSE | TRUE |
TRUE | FALSE |