go to previous page   go to home page   go to next page hear noise highlighting

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

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");

go to previous page   go to home page   go to next page