The Boolean (bool) Datatype
- We will next learn how to allow our program to make simple yes/no decisions,
- But before that, we need a datatype to represent yes and no.
- This is the bool datatype.
- The bool datatype is a c++ datatype, did not exist in c
- Range: true, false
- As a carryover from c, 0 is false and anything else is true.
- you will frequently see people use 0 and 1 for false and true.
- Any c functions that deal with booleans will return int
- See isalpha for example.
- Operations
- and, or, not
- &&, ||, ! are equivalent
- &, | are NOT
- Input and output are not supported
- This is actually a subtype of the integers.
- There are a number of operators (6) which produce booleans
- exp1 == exp2, note two = signs.
- exp1 != exp2
- exp1 < exp2
- exp1 <= exp2
- exp1 > exp2
- exp1 >= exp2
- Integers and characters are easy with these expressions.
- Strings:
- compare the first character, then second ....
- If one string ends then it is less than the other.
- ASCII order
- Floats are tough
- x == y is problematic
- fabs(x-y) < ACCURACY_LEVEL is better.
- We don't always store boolean values, but we need to know the type.
- Write a program that prints the and table
- and.cpp