Completion requirements
This chapter introduces relational and logical operators. It also discusses the use of these operators to write Boolean expressions.
22. Example
Answer:
if ( !(cost < 50) )
System.out.println("Reject these shoes");
else
System.out.println("Acceptable shoes");
(There are other ways to write this fragment. See below.)
Example
Example
It is important to put parentheses around the entire expression so the NOT is applied correctly. Say that you are considering a pair of $35 shoes. Evaluation proceeds like this:
! ( cost < 50 )
! ( 35 < 50 )
-----+----
|
! ( T )
------+--------
|
F
The entire condition evaluates to false and so the false branch of the if
statement is selected. The program prints out "Acceptable shoes".
Question 23:
Is the following program fragment correct?
if ( !cost < 50 )
System.out.println("Reject these shoes");
else
System.out.println("Acceptable shoes");