Boolean Operators

Exercise 7: fill in the following table:
A
B
    A && B    
    A || B    
        !A        
false false      
false true      
true false      
true true      

Example 4: The following outputs a message when y >= x >= 0.
   if (y >= x && x >= 0)
      System.out.println("y >= x >= 0");

Exercise 8: Rewrite the following using a single if and boolean operators. Write a sentence describing the qualifications for the offer. What kind of variable is single?
   if (single)
     if (gender == 'M')
           if (age >= 18 && age <= 26)
               System.out.println("This person qualifies for our special offer");
Ans: The offer applies to single males between the ages of 18 and 26 inclusive. The single variable is a boolean.
   if (single && gender == 'M' && age >= 18 && age <= 26) 
      System.out.println("This person qualifies for our special offer");

There are often several ways of translating a statement.

Example 5: If grade is outside the interval [0, 100], indicate an error. Otherwise output the grade.
Version 1: Translate using ||:
   if ( grade < 0 || grade > 100 ) 
      System.out.println("illegal grade"); 
   else 
      System.out.println("the grade is " + grade);

Version 2: Translate using && :
   if ( grade >= 0 && grade <= 100 ) 
      System.out.println("the grade is " + grade); 
   else 
      System.out.println("illegal grade"); 

De Morgan's laws state some relationships between boolean operations.

   !( a && b ) ==  ( !a ) || ( !b )
   !( a || b ) == ( !a ) && ( !b ) 

Strategies for simplifying expressions using De Morgan's laws:

Example 6: The following three expressions are equivalent:


Precedence of operators


The following table gives the operator precedence from highest (performed first) to lowest (performed last).

Operators
Descriptions
Method call
 
Type cast
 
!,+, -, ++, --
unary operators: logical not, plus, minus, increment, decrement
*, /, %
arithmetic multiplication, division, and modulus
+, -
arithmetic binary addition and binary subtraction
<, <=, > , >=
relational inequality
==, !=
equal, not equal
&&
logical AND
||
logical OR
=
assignment operators


Comparing characters

Besides comparing numbers, you can compare characters and strings using the relational and equality operators. The order of character comparisons is based on the Unicode position of each character. (See Appendix A of the textbook.)

Expression
Value
'a' < 'b' true
'X' <= 'A' false
'3' > '4' false
'3' <= '4' true
'a' > 'A' true
('A' <= ch) && (ch <= 'Z') true if ch contains an uppercase letter; otherwise false


Short-circuit evaluation of boolean expressions

Short-circuit evaluation means that in a multiple clause boolean expression, evaluation stops as soon as the answer is determined. For example, true || X is always true so there is no point finding X. Similarly, false && Y is always false regardless of Y's value. Short-circuit evaluation greatly simplifies logic in many cases.

Exercise 9: How would you use short-circuit evaluation to simplify the following?

   if ( numscores > 0 ) 
     if ( scoretotal/numscores > 90.0 ) 
        System.out.println("excellent!  Very good work "); 
Ans: Apply short-circuit evaluation using &&:
   if ( numscores > 0 &&  scoretotal/numscores > 90.0 ) 
      System.out.println("excellent!  Very good work "); 
The division only occurs if numscores is greater than 0.


Boolean assignment

Boolean expressions can be used in assignment statements

Example 7:
   boolean same; 
   boolean inRange; 
   boolean isLetter; 
   same = true;    // same is true
   same = (x==y);  // same is true if x equals y otherwise false 
                   // inRange is true if -10 < n < 10 otherwise false
   inRange = (n > -10 && n < 10);  // inRange is true if -10 < n < 10 otherwise false 
                   // isLetter is true if ch is an upper or lower case letter
   isLetter = ('A' <= ch && ch <='Z') ||  ('a' <= ch && ch <='z');