Boolean
Expressions
Master the logic that powers every decision a computer makes: TRUE, FALSE, and the operators that combine them.
What is a Boolean?
A Boolean is a data type that holds exactly one of two possible values: TRUE or FALSE. Named after mathematician George Boole, Booleans are the foundation of all computer logic and decision-making.
What is a Boolean Expression?
A Boolean expression is any expression that evaluates to either TRUE or FALSE. They are built using relational operators (comparisons) and logical operators (AND, OR, NOT).
\(age \ge 16\) // evaluates to TRUE or FALSE
\(score == 100\) // equality check
\(name \ne \text{"Alice"}\) // inequality check
true // literal Boolean value
Why Booleans Matter
Boolean expressions are the gatekeepers of program flow. Without them, programs would execute every line in order with no decisions, no repetition, and no intelligence.
Relational Operators
Relational operators compare two values and produce a Boolean result. These are the building blocks of Boolean expressions.
| Operator | Meaning | Example | Result |
|---|---|---|---|
| \(==\) | Equal to | \(5 == 5\) | TRUE |
| \(\ne\) | Not equal to | \(5 \ne 3\) | TRUE |
| \(>\) | Greater than | \(7 > 10\) | FALSE |
| \(<\) | Less than | \(3 < 8\) | TRUE |
| \(\ge\) | Greater or equal | \(5 \ge 5\) | TRUE |
| \(\le\) | Less or equal | \(9 \le 4\) | FALSE |
Logical Operators: AND, OR, NOT
Logical operators combine or modify Boolean values. They let you build compound Boolean expressions from simpler ones.
Truth Tables: AND and OR
The NOT Operator
NOT is a unary operator: it takes a single Boolean value and returns its opposite. It is the simplest logical operator but one of the most powerful for controlling program flow.
goOutside()
}
// Goes outside when raining is FALSE
Assignment vs Comparison
One of the most important distinctions in programming: the difference between assigning a value and comparing values. Confusing them is a common source of bugs.
| Feature | \(=\) (Assignment) | \(==\) (Comparison) |
|---|---|---|
| Purpose | Stores a value in a variable | Checks if two values are equal |
| Returns | The stored value | TRUE or FALSE |
| Example | \(x = 5\) | \(x == 5\) |
| Used in | Variable declarations | if statements, loops |
Boolean Order of Operations
Just like arithmetic has PEMDAS, Boolean logic has its own precedence rules. Understanding these prevents unexpected results in compound expressions.
How to Evaluate a Boolean Expression
Example 1: Simple Expression
Example 2: Compound Expression
Example 3: Using NOT
Common Mistakes
Summary: Boolean Logic at a Glance
| Concept | Symbol | Returns TRUE when |
|---|---|---|
| AND | AND | Both operands are TRUE |
| OR | OR | At least one operand is TRUE |
| NOT | NOT | The operand is FALSE |
| Equal | \(==\) | Left equals right |
| Not equal | \(\ne\) | Left does not equal right |
| Greater than | \(>\) | Left is larger than right |
| Less than | \(<\) | Left is smaller than right |
Question 1 of 5
If \(x = 8\) and \(y = 3\), what does the expression \((x > 5) \text{ AND } (y > 5)\) evaluate to?
Question 2 of 5
What is the value of \(\text{NOT } (\text{TRUE AND FALSE})\)?
Question 3 of 5
Given \(a = 5\), \(b = 12\), evaluate:
\((a < 10) \text{ OR } (b < 10) \text{ AND } (a == 5)\)
Question 4 of 5
Which of the following is equivalent to \(\text{NOT } (A \text{ OR } B)\) according to De Morgan's Laws?
Question 5 of 5
Consider the following code segment:
age ← 17
hasPermission ← true
IF \((\text{age} \ge 18) \text{ OR } (\text{hasPermission AND age} \ge 16)\)
DISPLAY("Approved")
ELSE
DISPLAY("Denied")
What will the code display?