click here for complete presentation ## The boolean typeThe boolean type keeps track of whether something is true or false ## Operations on booleans- Why would we want to do operations on booleans?
- Like numerical types, we can combine booleans in various ways.
- You might be familiar with these operations if you have taken a course in logic.
## The ! OperatorThe NOT operator
## The && Operator- The AND operator
- It gives back true only if both things being combined are true
- If I can swim AND the pool is not filled with acid, then I will survive
## The || OperatorThe "|" is above the enter key - The OR operator
- It gives back true if either or both things being combined are true
- If I get punched in the face OR kicked in the stomach, then I will be in pain
## Short circuit evaluation- In some circumstances, Java does not check the whole expression:
- (true || (some complicated expression))
- Ignores everything after || and gives back true
- (false && (some complicated expression))
- Ignores everything after && and gives back false
## Laws of Boolean Algebra## DeMorgan's LawDeMorgan was a British mathematician who showed the importance of several logic rules. Two of these: !(A && B) is equivalent to !A || !B ## Absorption LawA || (A && B) = A ## Distributive LawA && (B || C) = A && B || A && C |
## Boolean TypeCredit: A Slides are modified with permission from Barry Wittman at Elizabethtown College |
|||||||||||||||||||||||||||||||||||||