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

Answer:

Yes. Sometimes boolean variables like reject are used to record a decision that might be used in several places in the program.


Precedence of NOT

The NOT operator has high precedence. It is done before arithmetic and relational operators unless you use parentheses. Examine the following:

!speed > 2000   &&   memory > 512
------  
illegal: can't use ! on an arithmetic variable

Since ! has high precedence, the above says to apply it to speed. This which won't work, because speed is an integer and ! applies only to boolean values.

When parentheses are used

!(speed > 2000   &&   memory > 512)

the ! is the last operation done and is applied to the boolean value of the expression inside parentheses.

Expressions that involve a NOT operator are often hard to read. A confusing expression can be sometimes be rewritten to eliminate the NOT.


QUESTION 7:

Look at the original fragment:

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

Does the following do the same thing?

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

Use some example values for speed and memory and figure out what each fragment does.


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