### Boolean expressions, part 2: Logical operators

• Previously discussed

• Recall that there are 2 types of operators that return a boolean result (true or false):

• Compare operators:

• A compare operator compares 2 numerical expressions and return a Boolean result.

• Example:

 the compare operation 3 > 1 returns the Boolean value true

• Logical operators:

• A logical operator compares 2 Boolean (logical) expressions and return a Boolean result.

• Example:

 the logical operation true AND false returns the Boolean value false

• Logical operators

• Logical operators in Java:

Operator symbol   Meaning   Comment
&&   The logical AND operator     Binary operator
||   The logical OR operator   Binary operator
!   The logical NOT operator   Unary operator

• The logical AND operator &&

• The && operator only operate on the Boolean values true and false

• The results are as follows:

Operation     Result
true && true     true
true && false     false
false && true     false
false && false     false

• Example 1:

 ``` (3 > 1) && (3 < 5) = true && true = true ```

• Example 2:

 ``` (3 > 1) && (3 > 5) = true && false = false ```

• The logical OR operator ||

• The || operator only operate on the Boolean values true and false

• The results are as follows:

Operation     Result
true || true     true
true || false     true
false || true     true
false || false     false

• Example 1:

 ``` (3 < 1) || (3 < 5) = false || true = true ```

• Example 2:

 ``` (3 < 1) || (3 > 5) = false || false = false ```

• Priority of the logical operators

• Here is a table of the priority of all operators in Java:

Operator Description Level Associativity
[]
.
()
++
--
access array element
access object member
invoke a method
post-increment
post-decrement
1 left to right
++
--
+
-
!
~
pre-increment
pre-decrement
unary plus
unary minus
logical NOT
bitwise NOT
2 right to left
()
new
cast
object creation
3 right to left
*
/
%
multiplicative 4 left to right
+ -
+
string concatenation
5 left to right
<< >>
>>>
shift 6 left to right
```<  <=
>  >=
```
instanceof
relational
type comparison
7 left to right
==
!=
equality 8 left to right
& bitwise AND 9 left to right
^ bitwise XOR 10 left to right
| bitwise OR 11 left to right
&& conditional AND 12 left to right
|| conditional OR 13 left to right
?: conditional 14 right to left
```  =   +=   -=
*=   /=   %=
&=   ^=   |=
<<=  >>= >>>=
```
assignment 15 right to left

Note:

• Nobody in the right mind memorizes this table.

Besides, the priority of the operators will be different in a different language.

• What I do is memorize some useful ones:

 Assignment operators are dead last Compute goes before comparing (I.e.: %, *, /. +, - before <, <=, >, >=, ==, !=) Compare goes before logical (&&, ||) Unary operators goes before binary operators In all other case (where I don't have a clue which of the operator has a higher priority), I use braces !!!

• Example 1:

 ``` boolean a; Statement: a = 3 > 1 && 4 < 5 ; Operators in statement: = > && < Executed as follows: a = 3 > 1 && 4 < 5 ; // > and < have higher priority a = true && true ; a = true; ```

• Example 2:

 ``` boolean a; Statement: a = 3 > 1 && ! 4 < 5 Operators in statement: = > && ! < Executed as follows: a = 3 > 1 && ! 4 < 5 // ! has the higher priority Result: error Logical NOT operator (!) cannot be applied to a number ```

• Example 3:

 ``` boolean a; Statement: a = 3 > 1 && ! (4 < 5) Operators in statement: = > && ! ( < ) Executed as follows: a = 3 > 1 && ! (4 < 5); // ( ... ) has the higher priority a = 3 > 1 && ! true; // ! has the higher priority now a = 3 > 1 && false; // > has the higher priority now a = true && false; // && has the higher priority now a = false; ```

• Programming trick: test if a number is between 2 numbers

• Problem description:

 Write a Java program that reads in a number a The program prints "yes" when 10 ≤ a ≤ 20

• Wrong solution:

 ``` if ( 10 <= a <= 20 ) System.out.println("yes"); ```

Because 10 <= a <= 20 is evaluated as follows:

 ``` Expression: 10 <= a <= 20 Operators: <= <= Evaluated as: 10 <= a <= 20 (10 <= a is either true or false) true <= 20 or false <= 20 ```

It is illegal to use the comparison operator "<=" on a Boolean value and a number

• Correct solution:

 ``` if ( 10 <= a && a <= 20 ) System.out.println("yes"); ```

The condition is evaluated as follows:

 ``` Expression: 10 <= a && a <= 20 Operators: <= && <= (<= has higher priority, see above) Evaluated as: 10 <= a && a <= 20 Result: && operates on 2 logical values ```

Effect: only numbers that are between 10 and 20 will satisfy the condition 10 <= a && a <= 20

• Java program:

 ``` import java.util.Scanner; public class Between01 { public static void main(String[] args) { int a; Scanner in = new Scanner(System.in); // Construct Scanner object a = in.nextInt(); // Read in number into a if ( 10 <= a && a <= 20 ) { System.out.println("Number is between 10 and 20"); } } } ```

• Example Program: (Demo above code)

How to run the program:

 Right click on link and save in a scratch directory To compile:   javac Between01.java To run:          java Between01

• Converting lower case to upper case (and vice versa)

• Previously discussed:

• Converting lower case character to the same upper case character: ( click here )

 ``` public class Char05a { public static void main(String[] args) { char x; char y; x = 'a'; y = (char) (x - 'a' + 'A'); // (subtract 32) System.out.println("x = '" + x + "', y = '" + y + "'"); x = 'b'; y = (char) (x - 'a' + 'A'); System.out.println("x = '" + x + "', y = '" + y + "'"); x = 'c'; y = (char) (x - 'a' + 'A'); System.out.println("x = '" + x + "', y = '" + y + "'"); } } ```

• There is a caveat:

 The conversion will only work if we use a character code of a lower case character

Reason: see the ASCII code table

 ``` 0 NUL 1 SOH 2 STX 3 ETX 4 EOT 5 ENQ 6 ACK 7 BEL 8 BS 9 HT 10 NL 11 VT 12 NP 13 CR 14 SO 15 SI 16 DLE 17 DC1 18 DC2 19 DC3 20 DC4 21 NAK 22 SYN 23 ETB 24 CAN 25 EM 26 SUB 27 ESC 28 FS 29 GS 30 RS 31 US 32 SP 33 ! 34 " 35 # 36 \$ 37 % 38 & 39 ' 40 ( 41 ) 42 * 43 + 44 , 45 - 46 . 47 / 48 0 49 1 50 2 51 3 52 4 53 5 54 6 55 7 56 8 57 9 58 : 59 ; 60 < 61 = 62 > 63 ? 64 @ 65 A 66 B 67 C 68 D 69 E 70 F 71 G 72 H 73 I 74 J 75 K 76 L 77 M 78 N 79 O 80 P 81 Q 82 R 83 S 84 T 85 U 86 V 87 W 88 X 89 Y 90 Z 91 [ 92 \ 93 ] 94 ^ 95 _ 96 ` 97 a 98 b 99 c 100 d 101 e 102 f 103 g 104 h 105 i 106 j 107 k 108 l 109 m 110 n 111 o 112 p 113 q 114 r 115 s 116 t 117 u 118 v 119 w 120 x 121 y 122 z 123 { 124 | 125 } 126 ~ 127 DEL ```

If we arbitrarily subtract 32 from any ASCII code, the result is not always "convert the lower case character to the same upper case character"

• We can use an if-statement to ensure that we will only convert a lower case character:

 ``` public class ConvertChar01 { public static void main(String[] args) { char x; x = 'a'; if ( x >= 'a' && x <= 'z' ) x = (char) (x - 'a' + 'A'); // Convert only if character code // is a lower case letter System.out.println("'a': " + x); x = 'H'; if ( x >= 'a' && x <= 'z' ) x = (char) (x - 'a' + 'A'); System.out.println("'H': " + x); x = '%'; if ( x >= 'a' && x <= 'z' ) x = (char) (x - 'a' + 'A'); System.out.println("'%': " + x); } } ```

Output:

 ``` 'a': A 'H': H '%': % ```

• Example Program: (Demo above code)

How to run the program:

 Right click on link and save in a scratch directory To compile:   javac ConvertChar01.java To run:          java ConvertChar01