This chapter introduces relational and logical operators. It also discusses the use of these operators to write Boolean expressions.
5. AND Operator
How much flour do you have? 6 How much sugar do you have? 4 Enough for cookies!
When execution gets to the
ifstatement, it finds that
flour >= 4is true, because 6 >= 4. And
sugar >= 2is true, because 4 >= 2 Both sides are true, so AND gives true.
The and operator requires that both sides are true:
this side must be true && this side must be true
If both sides are true, the entire expression is true. If either side (or both) are false, the entire expression is false. && is a logical operator because it combines two true/false values into a single true/false value.
Here is what && does:
true && true = true
false && true = false
true && false = false
false && false = false
Use and when every requirement must be met.
Look at the boolean expression:
flour >= 4 && sugar >= 2What will the expression give us if
flouris 2 and