Decision making remains an integral portion of even the most simple web applications. In order to design a program that provides accurate information, it's imperative that we web developers provide the computer with accurate commands. In this series of tutorials entitled "Introduction to Logic", we will dissect logical operators and learn how to properly apply them to our programs. This tutorial will define the three most basic logical operators: Negation, Conjunction, and Disjunction.
Statements
Before we begin with the explication of logic, we must note that logic does not tolerate ambiguity or subjectivity. Logic is composed of a series of statements, where statement is defined as a sentence that can evaluate to true or false.
Examples of statements include:
- 2 + 3 = 12 (Note that something not true can still be a statement)
- The capital of Texas is Austin
- 3 is a prime number
Examples of non statements include:
- Today is a pretty day (subjective)
- Justin's beard is quite handsome (subjective)
Negation
For a statement p, the
negation of p (denoted !p) is the logical opposite of p. The statement !p can be read "not p".
Examples
p: 2 + 3 = 5
!p: 2 + 3 ≠ 5
p: Austin is the capital of Texas
!p: Austin is not the capital of Texas
Some examples using
De Morgan's laws:
p: 5 - 1 > 2
!p: 5 - 1 ≤ 2
p: 2 + 8 ≥ 10
!p: 2 + 8 < 10
Truth Table
Truth tables are tools used to view the possible logical outcomes of every possible combination of statements. In truth tables, T stands for a true statement while F stands for a false statement.
The truth table for negation is as follows:
As described earlier, the negation of a statement has the opposite truth value of the original statement.
Conjunction (AND) Statements
The
conjunction of statements p and q, denoted p && q, is read "p AND q", and is true only when p and q are both true.
Examples
p: 2 + 3 = 5
q: The earth is larger than the Moon
p && q: 2 + 3 = 5 and the earth is larger than the moon.
p && q is true.
p: Bicycles have three wheels
q: 5 < 10
p && q: Bicycles have three wheels and 5 < 10
p && q is false since Bicycles do not have three wheels.
Truth Table
As you can see, the only instance in which p && q is true is when p and q are both individually true.
Disjunction (OR) Statements
The
disjunction of statements p and q, denoted p || q, is read "p OR q", and is true when at least one of p or q is true.
Examples
p: 2 + 3 = 5
q: 5 + 3 = 2
p || q: 2 + 3 = 5 OR 5 + 3 = 2
p || q is true since at 2 + 3 = 5.
p: Africa is a country
q: 1 > 2
p || q: Africa is a country or 1 > 2.
p||q is false since neither p nor q is true
p: Tyler is in Texas
q: Texas is in the United States
p || q: Tyler is in Texas or Texas is in the United States
p || q is true since Tyler is in Texas and Texas is in the United States
The previous example brings up an important point about the disjunction operator. This particular "or" is known as an "and/or", in contrast to an "exclusive or" (xor for short). With the disjunction operator, if p && q is true, p || q is also true. However, in an "exclusive or", p xor q evaluates to true only when there exists at least one statement that is true but p&&q is false.
Truth Table
As you can see, the only instance in which p || q is false is when p and q are both individually false.
November 01, 2010