Example

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   hear noise   go to next page

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