Conditional Operators

     

There are operators used to express boolean relationships between expressions (see Table 7-2).

7-2. Boolean Operators

O PERATION

O PERATOR

E XAMPLE

AND

&&

int x = 1;

   

int y = 10;

   

( (x == 1) &&

   

(y==10) )

OR

( (y >2) (true) )

NOT

!

( ! x )


The boolean operators are used in expressions to reduce to a boolean value of true or false .

The operators short circuit, which means that if it is not necessary for the second expression to be evaluated, it is not evaluated. In the following example, the second expression is never looked at by the runtime:

 

 if ( (false) && (true) ) {} 

Because the first expression is false, we know that both the first expression AND the second expression evaluate to true. So we don't go there.

By the same token, the second expression will not be evaluated in this example.

 

 if ( true  false) 

In a boolean OR operation, only one of the terms needs to be true for the statement to evaluate to true, so because the first one is true, we already know the whole thing must be true.



Java Garage
Java Garage
ISBN: 0321246233
EAN: 2147483647
Year: 2006
Pages: 228
Authors: Eben Hewitt

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net