Logical operators
There are three logical operators that result in a true or false condition. The logical operators are:
- "&" - AND operator, logical conjunction.
A & B - True if A and B are true, false otherwise.
- "|" - OR operator, logical disjunction.
A | B - False if A and B are false, true otherwise.
- "!" - NOT operator, logical negation.
! A - True if A is false, false if A is true.
THE LOGICAL CONJUNCTION: And
The "&" operator performs a logical conjunction (And), resulting in true if both operands are true, false otherwise. Since it is a binary operator and each operand has two values, there are four possible combinations.
True & true = true
False & false = false
False & true = false
True & false = false
The operator implied between instructions is always the AND operator. This means that if the logical operator is omitted, it is implied that it will always be AND. This means that writing "A & B" is equivalent to writing "A B".
THE LOGICAL DISJUNCTION: Or
The "|" operator (Vertical bar) performs a logical disjunction (OR), resulting in true if at least one of the operands is true, false if both operands are false. Since it is a binary operator and each operand has two values, there are four possible combinations.
True & true = true
False & false = false
False & true = true
True & false = true
LOGICAL NEGATION: Not
The operator "!" (exclamation) is used to perform logical negation (Not). The logical negation of a Boolean value results in the opposite value. If an instruction returns a true value, then "! Instruction" returns a false value, and vice versa. Since logical negation is a unary operator, there are only two possible values for its one operator: true or false.
Examples:
A & !B - It is a true condition only if A is true and B is false. It can also be written as: A !B - If statement A is true and (And) the negation of statement B is true, the condition results true, otherwise false.
A | !B - The condition is true if A is true or B is false (logical negation), otherwise it is false.
! A - The condition is true only if A is false.
! A ! B - The condition is true only if A and B are false, since we have used logical negation in both.
! (A B) - As the previous one, but with the use of parentheses to group blocks of instructions