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

Answer:

    if (  cash < price )
    {
      System.out.println("You can't buy the sweater." );
      System.out.println("You need " + (price-cash) + " more cents." );
    }
    else
      System.out.println("You can buy the sweater." );

You (hopefully) picked a relational expression that was true when the user COULD NOT pay for the sweater.


Opposites

In the first program, the boolean expression is:

    if (  cash >= price )

because we want the true branch to execute when the user has enough money.

In the second program, the boolean expression is:

    if (  cash < price )

because we want the false branch to execute when the user has enough money.

The operator in the first program includes the "equals". The operator in the second program does not.

In a sense, >= and < are opposites. Sometimes it is convenient to rearrange the contents of the true and the false branch. But be sure to change the boolean expression correctly!

Often programs are written so the true branch is the desirable outcome and the false branch is not. But this is a matter of human psychology, not logic.


QUESTION 9:

What is the "opposite" of   <= ?      


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