Getting Started
Computers are powerful because they can make decisions. A video game needs to know if a player has leveled up, a banking app must decide if a password is correct, and a navigation system has to determine the fastest route. All of these complex decisions are built from simple true-or-false questions, which are evaluated using Boolean expressions.
What You Should Be Able to Do
Evaluate expressions that use relational operators (e.g.,
>,<,=).Evaluate expressions that use logical operators (
AND,OR,NOT).Trace the evaluation of a compound expression that combines multiple operators.
Explain how the result of a Boolean expression can control the flow of a program.
Key Concepts & Application
The Core Idea
At the heart of every decision a computer makes is a simple concept: the Boolean value. A Boolean value can only be one of two things: true or false. Think of it like a light switch (on or off) or a simple yes/no answer. There is no middle ground.
An expression is a combination of values, variables, and operators that a computer evaluates to produce a single new value. A Boolean expression is any expression that results in a true or false value. For example, the question "Is the score greater than 100?" can be written as the expression score > 100. If score is 101, the expression evaluates to true; if score is 99, it evaluates to false. Programs use these results to choose which path to take, forming the basis of all logic and control.
Logic & Application
To build Boolean expressions, we use two types of operators: relational and logical.
Relational Operators
A relational operator is a symbol used to compare two values. The result of this comparison is always a Boolean value.
| Operator | Meaning | Example (a is 5, b is 10) | Result |
|---|---|---|---|
= | Equal to | a = 5 | true |
≠ | Not equal to | a ≠ b | true |
> | Greater than | a > b | false |
< | Less than | a < b | true |
≥ | Greater than or equal to | a ≥ 5 | true |
≤ | Less than or equal to | b ≤ a | false |
Logical Operators
A logical operator is used to modify or combine existing Boolean values to form more complex expressions.
| Operator | Logic | Example (a is true, b is false) | Result |
|---|---|---|---|
AND | Returns true if both operands are true. | a AND b | false |
OR | Returns true if at least one operand is true. | a OR b | true |
NOT | Returns the opposite of the operand. | NOT b | true |
Annotated Pseudocode Examples
Boolean expressions are most commonly used to control selection statements, such as an IF block. The code inside the IF block only runs if its condition evaluates to true.
temperature <- 85
isRaining <- false
// This condition uses both a relational (>) and a logical (AND) operator.
// It checks if it's hot AND it's not raining.
IF (temperature > 80 AND isRaining = false)
{
// This block will execute because both parts of the condition are true.
DISPLAY("It's a great day for the beach!")
}
playerLevel <- 12
hasKey <- false
// This condition uses a logical OR operator.
// It checks if the player has a high enough level OR has the key.
IF (playerLevel > 20 OR hasKey = true)
{
// This block will NOT execute because both parts of the condition are false.
DISPLAY("You may enter the castle.")
}
ELSE
{
DISPLAY("You are not ready to enter the castle.")
}
Tracing & Analysis
When expressions have multiple operators, they are evaluated in a specific order: NOT is evaluated first, then AND, and finally OR. Parentheses () can be used to force a different order of evaluation, just like in mathematics.
Logic Trace
Let's trace the evaluation of a complex expression. Assume the following variable values:
score <- 88
isVeteran <- false
priorityLevel <- 4
Now, evaluate the expression: (score > 90 OR isVeteran = true) AND priorityLevel < 5
Evaluate expressions inside parentheses first.
score > 90becomes88 > 90, which isfalse.isVeteran = truebecomesfalse = true, which isfalse.The parenthetical expression is now
(false OR false).false OR falseevaluates tofalse.
Substitute the result back into the main expression.
- The expression is now:
false AND priorityLevel < 5
- The expression is now:
Evaluate the remaining operators.
priorityLevel < 5becomes4 < 5, which istrue.The expression is now:
false AND true.
Calculate the final result.
false AND trueevaluates tofalse.
The entire Boolean expression evaluates to false.
Key Terminology & Logic
| Operator / Keyword | Purpose |
|---|---|
a = b | Compares a and b for equality. |
a ≠ b | Compares a and b for inequality. |
a > b, a < b | Compares a and b to see which is greater or less. |
a ≥ b, a ≤ b | Compares a and b including equality. |
a AND b | Evaluates to true only if both a and b are true. |
a OR b | Evaluates to true if a is true, or if b is true, or if both are true. |
NOT a | Inverts the value of a; NOT true is false, NOT false is true. |
Core Concepts & Terminology
Boolean Value: A data type that can hold only one of two values:
trueorfalse. It is the fundamental unit of logic in computing.Expression: A segment of code that combines values, variables, and operators and is evaluated to produce a single resulting value.
Relational Operator: An operator (such as
=,>, or≤) that tests the relationship between two values and produces a Boolean result.Logical Operator: An operator (
AND,OR,NOT) that modifies or combines Boolean values to create more complex logical expressions.Core Logic (Selection): Boolean expressions are the foundation of selection, which allows a program to execute different code blocks based on a condition.
IF (condition) { // This code runs only if the 'condition' expression evaluates to true. }The
conditionmust be a Boolean expression.
Core Skill Check
Logic Tracing: Given
a <- 10andb <- 20, what is the final value of the expression(a > 5) AND (b = 10 OR a ≠ 10)?Debugging: A program to check if a user is a teenager has a bug. Identify the logic error:
age <- 13,IF (age > 13 AND age < 19){ DISPLAY("User is a teen.") }.Application: Describe a real-world rule for a streaming service that would require an
ANDoperator in its logic (e.g., for allowing a user to watch a movie).
Common Misconceptions & Clarifications
Confusing
=(Comparison) with<-(Assignment): In pseudocode,=asks "are these two things equal?" and results intrueorfalse. In contrast,<-takes the value on the right and assigns it to the variable on the left."OR means only one can be true": In logic,
ORis inclusive.true OR trueevaluates totrue. It means "at least one is true."Forgetting Order of Operations: Without parentheses,
NOTis always evaluated first, thenAND, thenOR. The expressionNOT false AND falseevaluates totrue AND false, which isfalse.Mixing up
>with≥: A common source of errors is forgetting to include the "or equal to" case.score > 90is false ifscoreis exactly 90, whilescore ≥ 90is true.
Summary
Boolean expressions are the building blocks of programmatic decision-making. They use relational operators to compare values and logical operators to combine or modify true/false conditions. By evaluating these expressions, a program can determine which actions to take, enabling it to respond dynamically to different inputs, states, and user interactions. Mastering the evaluation of these expressions is fundamental to understanding how algorithms control the flow of execution and create complex, intelligent behavior from simple, binary logic.