๐Ÿ”ฅ Class Notes: Truth Tables in Java (Boolean Logic)

High school-friendly notes connecting truth tables to real Java if statements and Boolean expressions.

๐ŸŽฏ Learning Objectives

๐Ÿง  What is a Truth Table?

A truth table is a chart that shows:

Boolean values:

true
false

Truth tables help programmers understand how logical conditions behave.

โš™๏ธ Boolean Logic in Java

Java uses Boolean expressions inside:

Example:

if(score > 70 && attendance > 90)
{
    System.out.println("Pass");
}

๐Ÿ”‘ Java Logical Operators

Operator Name Meaning
&& AND Both must be true
|| OR At least one true
! NOT Reverse the value
Operator precedence in Java: ! first, then &&, then ||.

๐Ÿ“Š Truth Table: AND Operator (&&)

Rule: TRUE only when BOTH conditions are true.

A B A && B
truetruetrue
truefalsefalse
falsetruefalse
falsefalsefalse

Java Example

boolean result = (x > 5 && y < 10);

๐Ÿ“Š Truth Table: OR Operator (||)

Rule: TRUE when at least ONE condition is true.

A B A || B
truetruetrue
truefalsetrue
falsetruetrue
falsefalsefalse

Java Example

boolean result = (age >= 18 || hasPermission);

๐Ÿ“Š Truth Table: NOT Operator (!)

Rule: Reverse the Boolean value.

A !A
truefalse
falsetrue

Java Example

boolean result = !(score >= 70);

๐Ÿš€ Combining Operators (Advanced)

Java allows combining multiple logical operators.

Example:

boolean result = (x > 10 && y < 5) || z == 3;
How to evaluate:
  1. Evaluate inside parentheses first
  2. Apply AND before OR (unless parentheses change it)

๐Ÿงฉ Truth Table Example (Combined Expression)

Expression:

(A && B) || C
A B C A && B (A && B) || C
TTTTT
TTFTT
TFTFT
TFFFF
FTTFT
FTFFF
FFTFT
FFFFF

๐Ÿ”ฅ Real Java Example

boolean passedExam = true;
boolean completedHomework = false;
boolean teacherOverride = true;

boolean canPass = (passedExam && completedHomework) || teacherOverride;

Result: TRUE because teacherOverride is true.

๐ŸŽ“ Why Truth Tables Matter

Truth tables help programmers:

๐Ÿงช Step-by-Step Method for Students

When building a truth table:

  1. List variables (A, B, Cโ€ฆ)
  2. Write ALL combinations of true/false
  3. Evaluate smaller parts first
  4. Fill in columns step by step
  5. Compute final result

๐Ÿ’ฅ Common Student Mistakes

โŒ Mixing up AND vs OR

AND: Both true

OR: At least one true

โŒ Forgetting parentheses

Harder to read:

x > 5 && y < 10 || z == 3

Better:

(x > 5 && y < 10) || z == 3
โŒ Ignoring precedence: Java evaluates ! then && then ||. Use parentheses to make your intention clear.

๐Ÿ”ฅ Advanced Example (AP CSA Level)

Expression:

!(A || B) && C
A B C A || B !(A || B) Final
TTTTFF
TTFTFF
TFTTFF
TFFTFF
FTTTFF
FTFTFF
FFTFTT
FFFFTF

๐Ÿงฑ Real Programming Use Cases

Truth tables are useful when:

๐Ÿ‘จโ€๐Ÿซ Teacher Tip: Break complex conditions into smaller columns when building a truth table.