|
click here for complete presentation
The boolean type
The boolean type keeps track of whether something is true or false Declaration of a boolean variable is like:
boolean value;
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 ! Operator
The NOT operator Changes a true into a false or a false into a true
x |
!x |
true |
false |
false |
true |
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
x |
y |
x && y |
true |
true |
true |
true |
false |
false |
false |
true |
false |
false |
false |
false |
The || Operator
The "|" 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
x |
y |
x || y |
true |
true |
true |
true |
false |
true |
false |
true |
true |
false |
false |
false |
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 Law
DeMorgan was a British mathematician who showed the importance of several logic rules.
Two of these:
!(A && B) is equivalent to !A || !B !(A || B) is equivalent to !A && !B
These come in very handy and are often tested on the AP Exam
Absorption Law
A || (A && B) = A A && (A || B) = A
Distributive Law
A && (B || C) = A && B || A && C
A || (B && C) = (A || B) (A || C)
|
Boolean Type
Credit: A Slides are modified with permission from Barry Wittman at Elizabethtown College This work is licensed under an Attribution-NonCommercial-ShareAlike 3.0 Unsupported License
|