Logic begins with simple sentences that are either true or false. By joining them with a few connectives we can build and analyse statements of any complexity.

Propositions

A proposition is a declarative sentence that is either true or false, but not both. "Delhi is the capital of India" is a true proposition, and "5 is an even number" is a false proposition. A question such as "What time is it?", a command such as "Close the door", and an open sentence such as "x + 2 = 5" are not propositions, since they have no fixed truth value. Propositions are named by letters p, q, r, and their truth values are written T and F, or 1 and 0. A proposition that cannot be broken into smaller ones is atomic, and one built from others with connectives is compound.

Definition: Proposition. A declarative sentence that has exactly one truth value, either true or false.

Logical Connectives

Negation, ¬p (not p), is true when p is false. Conjunction, p ∧ q (p and q), is true only when both p and q are true. Disjunction, p ∨ q (p or q), is true when at least one of p and q is true. Exclusive or, p ⊕ q, is true when exactly one is true. The conditional, p → q (if p then q), is false only when p is true and q is false. Here p is the hypothesis and q the conclusion. The biconditional, p ↔ q (p if and only if q), is true when p and q have the same truth value. The order of precedence is ¬, then ∧, then ∨, then →, then ↔, and brackets are used to make the order clear.

Constructing Truth Tables

A truth table lists every combination of truth values of the atomic propositions and the resulting value of the compound statement. With n atomic propositions there are 2^n rows. We write the atomic columns first, then add a column for each sub-expression, working from the inside outward, until the final column gives the value of the whole statement. Truth tables give a mechanical test for the meaning of any statement, and they are the basis of the design of digital circuits, in which T and F become 1 and 0 and the connectives become gates.

Table 1.1: Truth table of the basic connectives

p q ¬p p ∧ q p ∨ q p → q p ↔ q
T T F T T T T
T F F F T F F
F T T F T T F
F F T F F T T

Example 1.1: Truth table of a compound statement

Problem: Construct the truth table of (p ∨ q) ∧ ¬p.

Solution:

  1. List the four rows of p and q.
  2. Find p ∨ q: T, T, T, F.
  3. Find ¬p: F, F, T, T.
  4. Take the AND of the two columns: F, F, T, F.

Result: (p ∨ q) ∧ ¬p is true only when p is false and q is true.

from itertools import product
for p, q in product([True, False], repeat=2):
    print(p, q, (p or q) and not p)

Output

True True False
True False False
False True True
False False False

Common mistakes

  • Treating questions, commands or open sentences as propositions.
  • Thinking that p → q is false when p is false. It is true in that case.
  • Forgetting that a statement with n variables needs 2^n rows.

Key takeaways

  • A proposition is either true or false.
  • ∧ is true only when both parts are true, ∨ when at least one is true.
  • p → q is false only when p is true and q is false.
  • A truth table with n variables has 2^n rows.

Practice questions

  1. Is "x + 3 = 7" a proposition?
  2. When is p → q false?
  3. How many rows does the truth table of a statement with 3 variables have?

Answers to the practice questions

  1. No. Its truth depends on x, so it has no fixed truth value.
  2. Only when p is true and q is false.
  3. 2^3 = 8 rows.

Sources

  • Official BCA syllabus Mathematics Foundations to Computer Science II, course code 25BCA402DS01, Unit I