Why This Matters
Every if statement you write, every database query you filter, and every access
control rule you define is built on propositional logic.
When you write if (age is at least 18 and hasLicense), you are combining two propositions
with a logical connective. Understanding the
formal rules behind these expressions lets you reason about complex conditions
without getting lost in nested logic.
Propositional logic is the bedrock of computer science. Circuit design, boolean algebra, type systems, and formal verification all rest on propositions and their connectives. When a condition does not behave the way you expect, it is almost always because the underlying logical structure was misunderstood. Learning to think in terms of negation, conjunction, and disjunction gives you a precise vocabulary for debugging and designing correct programs.
Logic is also the gateway to proof techniques. Once you understand how propositions combine and when they evaluate to true or false, you can begin writing formal arguments that guarantee correctness, not just test for it.
Define Terms
Visual Model
The full process at a glance. Click Start tour to walk through each step.
Propositions are true-or-false statements combined with logical connectives to form compound expressions.
Code Example
// Propositions are boolean expressions
const p = true; // "It is raining"
const q = false; // "I have an umbrella"
// Negation: NOT
console.log("NOT p:", !p); // false
console.log("NOT q:", !q); // true
// Conjunction: AND
console.log("p AND q:", p && q); // false
// Disjunction: OR
console.log("p OR q:", p || q); // true
// Implication: if p then q
// p => q is equivalent to (!p) || q
function implies(a, b) {
return !a || b;
}
console.log("p => q:", implies(p, q)); // false
// Compound proposition: (p AND (NOT q)) OR q
const compound = (p && !q) || q;
console.log("(p AND NOT q) OR q:", compound); // true
// Evaluate a compound proposition from variables
function evaluate(p, q) {
const notQ = !q;
const pAndNotQ = p && notQ;
const result = pAndNotQ || q;
console.log(`p=${p}, q=${q} => (p AND NOT q) OR q = ${result}`);
return result;
}
evaluate(true, true); // true
evaluate(true, false); // true
evaluate(false, true); // true
evaluate(false, false); // falseInteractive Experiment
Try these exercises:
- Write expressions for "it is NOT the case that both P and Q are true." Verify the result for all four combinations of P and Q.
- Check whether
!(p && q)gives the same results as!p || !qfor all input combinations. This is De Morgan's Law. - Evaluate the implication
p => qwhen p is false. Notice that it is always true regardless of q. - Build a compound proposition with three variables (P, Q, R) and evaluate it for all 8 combinations.
- Write a real-world condition like "user is admin OR (user is logged in AND page is public)" and test it with different inputs.
Quick Quiz
Coding Challenge
Write a function called `evaluateCompound` that takes two boolean values p and q and returns the result of the compound proposition: (p AND q) OR (NOT p AND NOT q). This expression checks whether p and q have the same truth value.
Real-World Usage
Propositional logic is everywhere in software:
- Conditional branching: Every
if/elsestatement evaluates a proposition. Complex conditions chain AND, OR, and NOT together. - Database queries: SQL WHERE clauses like
WHERE age above 18 AND status = 'active'are compound propositions filtered over rows. - Access control: Permission systems use logic like "user is admin OR (user owns resource AND resource is not locked)."
- Circuit design: Digital circuits are built from AND, OR, and NOT gates. CPUs are literally propositional logic in silicon.
- Search filters: E-commerce filters ("brand is Nike AND price under 100 OR on sale") are propositional expressions.