Files
freeCodeCamp/guide/english/logic/logical-operators/index.md

84 lines
2.7 KiB
Markdown
Raw Normal View History

2018-10-12 15:37:13 -04:00
---
title: Logical Operators
---
# Logical Operators
2018-10-12 15:37:13 -04:00
## Conjunction a.k.a. "AND" (&&)
P&&Q returns True if both P and Q are True. If either P or Q (or both) are False, then P&&Q is False.
2018-10-12 15:37:13 -04:00
| P | Q | AND(P,Q) |
|---|---|---|
| T | T | T |
| T | F | F |
| F | T | F |
| F | F | F |
2018-10-12 15:37:13 -04:00
## Disjunction a.k.a. "OR" (||)
P||Q returns True if at least one of P or Q (or both P and Q) is True. Only returns False if P and Q are both False.
2018-10-12 15:37:13 -04:00
| P | Q | OR(P,Q) |
2018-10-12 15:37:13 -04:00
|---|---|---|
| T | T | T |
| T | F | T |
| F | T | T |
| F | F | F |
2018-10-12 15:37:13 -04:00
## Negation a.k.a. "NOT" (!)
Returns the opposite value. Ex. if P is true, then !P is false, and if P is false, then !P is true. This is the only logical operator that works on only one input, which makes it a unary operator.
2018-10-12 15:37:13 -04:00
| P | Q | NOT(P) | NOT(Q) |
2018-10-12 15:37:13 -04:00
|---|---|---|---|
| T | T | F | F |
| T | F | F | T |
| F | T | T | F |
| F | F | T | T |
2018-10-12 15:37:13 -04:00
## XOR ("eXclusive or")
Is known as **exclusive or**. Similar to OR, but returns False if both P and Q are true. That is, XOR returns true if one and only one of P or Q is True.
2018-10-12 15:37:13 -04:00
Note: XOR is unique among the logical operators because, by combining several together, you can form all of the other logical operators using only XOR.
| P | Q | XOR(P,Q) |
2018-10-12 15:37:13 -04:00
|---|---|---|
| T | T | F |
| T | F | T |
| F | T | T |
| F | F | F |
2018-10-12 15:37:13 -04:00
## Implication (P -> Q)
Read as "if P, then Q" or "P implies Q".
2018-10-12 15:37:13 -04:00
Only returns False when P is True and Q is False. Otherwise the implication is True.
Note: Implications are often used for direct mathematical proofs. P represents the hypothesis, while Q is the conclusion.
2018-10-12 15:37:13 -04:00
The only time the conditional is false is when a true value leads to a false value.
| P | Q | IF(P,Q) |
2018-10-12 15:37:13 -04:00
|---|---|---|
| T | T | T |
| T | F | F |
| F | T | T |
| F | F | T |
2018-10-12 15:37:13 -04:00
## Logical Equivalence (iff: if and only if)
"P if and only if Q" is the same as "P implies Q AND Q implies P". In other words, the truth tables for P and Q are identical for all truth values.
This is known as the biconditional. It is the equivalent of P -> Q **AND** Q -> P. It means that both conditionals must be satisfied in order for the biconditional to be true.
2018-10-12 15:37:13 -04:00
You can easily see that the output of the IFF operator in the truth table is the same as the ANDing of columns 3 and 4.
2018-10-12 15:37:13 -04:00
| P | Q | IF(P,Q) | IF(Q,P) | IFF(P,Q) |
2018-10-12 15:37:13 -04:00
|---|---|---|---|---|
| T | T | T | T | T |
| T | F | F | T | F |
| F | T | T | F | F |
| F | F | T | T | T |
2018-10-12 15:37:13 -04:00
## Additional Resources
- [Logical Operators in Javascript](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators)
- [Logical Operators in PHP](http://php.net/manual/en/language.operators.logical.php)
- [Logical Operators in C++](http://en.cppreference.com/w/cpp/language/operator_logical)