# Boolean operators

• Introduction

• Arithmetic operators operates on numeric values and produce numeric values

E.g.: 3 + 5 = 8

• Boolean operators operates on boolean values (true and false) and produce a boolean value

E.g.: true AND true = true

• Boolean operators

• A boolean operator operates on boolean values (true and false) and produce a boolean value

• There are 3 boolean operations defined:

Operator Symbol Operation
&& Logical AND operation
|| Logical OR operation
! Logical NOT operation

• Truth Table for the AND operation

• The following table gives the outcome of the logical AND operation:

 A B A && B true true true true false false false Any false

• Example:
```            5             100
----------+--------------+------------------
|<----  x ---->|
```
To test if a variable x is between 5 and 100, the following logical expression can be used:

 ``` if ( x >= 5 && x <= 100 ) ... ```

• Truth Table for the OR operation

• The following table gives the outcome of the logical OR operation:

 A B A || B true Any true false true true false false false

• Example:
```            5             100
----------+--------------+------------------
<---------|              |----------------->
```
To test if a variable x is outside the range of 5 and 100 (i.e., less than 5 or greater than 100), the following logical expression can be used:

 ``` if ( x < 5 || x > 100 ) ... ```

• Truth Table for the NOT operation

• The following table gives the outcome of the logical NOT operation:

 A ! A true false false true

• Example: using the NOT operation

 ``` boolean married; ... if ( ! married ) ... ```

• Self Check

• When does the statement
```	System.out.println (x > 0 && x <= 0);
```
print ?

false

• If x = 1 and y = 2, what is the value of:

• x * x + y * y < 4

false

• x < 4 && y > 6

false

• x < 4 || y > 6

true

• !(x < 4)

false