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

Answer:

Usually there are many equivalent ways to write an if statement.


Equivalent Statements

Here is the original fragment, which uses AND:

if (  !(speed > 2000 && memory > 512)  )
  System.out.println("Reject this computer");
else
  System.out.println("Acceptable computer");

Here is an equivalent fragment that uses OR:

if (  speed <= 2000  ||  memory <= 512 )
  System.out.println("Reject this computer");
else
  System.out.println("Acceptable computer");

Yet another equivalent fragment reverses the order of the true and false branches of the statement.

if (  speed > 2000  &&  memory > 512 )
  System.out.println("Acceptable computer");
else
  System.out.println("Reject this computer");

The last fragment is probably the best choice because it is the easiest to read. It follows the pattern:

if ( expression that returns "true" for the desired condition )
  perform the desired action
else
  perform some other action

Generally, people find statements that involve NOT to be confusing. Avoid using using NOT, if you can. If NOTs are needed, try to apply them to small subexpressions, only.


QUESTION 10:

Rewrite the following natural language statement into an equivalent easily understood statement.

Not always are robins not seen when the weather is not fair.

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