Completion requirements
This chapter introduces relational and logical operators. It also discusses the use of these operators to write Boolean expressions.
4. &&
Answer:
flour >= 4 true
sugar >= 2 false
&&
Examine this part of the program:
// check that there is enough of both ingredients
if ( flour >= 4 && sugar >= 2 )
System.out.println("Enough for cookies!" );
else
System.out.println("sorry...." );
For you to have enough ingredients, both relational expressions must be true. This is the role of the &&
(and-operator) between the two relational expressions. The &&
requires that both
flour >= 4
and
sugar >= 2
are true for the entire expression to be true. The entire question must be true in order for the true branch to execute.
The and operator &&
is a logical operator. A logical operator examines two true/false values and outputs a single true/false value.
Question 4:
What is printed if the user enters 6 for flour
and 4 for sugar
?