- Sometimes, whether a statement is executed is determined by a combination of several conditions.
- You can use logical operators to combine these conditions.
- Logical operators are known as Boolean operators or bitwise logical operators.
- The boolean operator operates on boolean values to create a new boolean value.
- The bitwise logical operators are “&”, “|”, “^”, and “~” or “!”.
The following table shows the outcome of each operation.
a
|
b
|
a&b
|
a|b
|
a^b
|
~a
or !a
|
true(1)
|
true(1)
|
true
|
true
|
false
|
false
|
true(1)
|
false(0)
|
false
|
true
|
true
|
false
|
false(0)
|
true(1)
|
false
|
true
|
true
|
true
|
false(0)
|
false(0)
|
false
|
false
|
false
|
true
|
Operator | Description | Example |
---|---|---|
&& (logical and) | Called Logical AND operator. If both the operands are non-zero, then the condition becomes true. | (A && B) is false |
|| (logical or) | Called Logical OR Operator. If any of the two operands are non-zero, then the condition becomes true. | (A || B) is true |
! (logical not) | Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false. | !(A && B) is true |