Building and Reading True-or-False Conditions

Boolean expressions let you describe conditions precisely: whether a temperature is acceptable, whether a password matches, or whether two requirements are both satisfied. A Boolean expression is an expression whose value is either true or false. You can evaluate one using the current values of its variables without deciding what a program should do next. This lesson focuses on building and reading those expressions.

Comparisons produce Boolean values

A comparison relates two values and produces a Boolean result. Common comparison operators are >, <, >=, <=, ==, and !=. They mean greater than, less than, greater than or equal to, less than or equal to, equal to, and not equal to, respectively.


These examples use language-neutral pseudocode: == tests equality, and AND, OR, and NOT combine Boolean values. Programming languages may spell these operators differently, but the logical relationships remain the same.


Example: Suppose a storage bin contains 14 items. The expression items > 10 is true, while items == 10 is false. The expression items != 10 is true because the stored value differs from 10.


An inclusive limit needs an equality component. If a maximum capacity is 30 items, items <= 30 includes a bin containing exactly 30 items; items < 30 excludes it. Read phrases such as “at least” and “at most” carefully before choosing the operator.



Combining values with AND, OR, and NOT

AND produces true when both inputs are true. OR produces true when at least one input is true. NOT takes one Boolean input and reverses its value.


A truth table lists every possible combination of Boolean inputs and the resulting outputs. For two inputs, there are four combinations:


A

B

A AND B

A OR B

NOT A

false

false

false

false

true

false

true

false

true

true

true

false

false

true

false

true

true

true

true

false


Logical OR includes the case where both inputs are true. Everyday uses of “or” sometimes suggest choosing exactly one alternative, but the logical operator shown here does not impose that restriction.


Comparisons can supply the inputs to these operators. To test whether a value lies between two inclusive limits, use:


inRange=(x≥L) AND (x≤U)\text{inRange}=(x\ge L)\ \text{AND}\ (x\le U)


Both comparisons must be true. Using OR instead would accept values that satisfy just one bound, including values outside the intended interval.

Group and check the whole expression

Parentheses identify which pieces belong together. In (p OR q) AND (NOT r), first determine the value of p OR q and the value of NOT r. Those two results become the inputs to AND.


A tree groups p OR q and NOT r as the two inputs to AND.


Apply NOT to the entire group it precedes. The expression NOT (p AND q) reverses the combined result. It is false only when both inputs are true. By contrast, (NOT p) AND q reverses only p before combining it with q.


Negating a group also gives two useful equivalences: NOT (p AND q) means (NOT p) OR (NOT q), and NOT (p OR q) means (NOT p) AND (NOT q). You can verify each relationship across the four rows of a truth table.



What to remember

  • Comparisons produce true or false; check whether equality belongs at each boundary.
  • AND requires both inputs; OR requires at least one.
  • NOT reverses its input, which may be a whole parenthesized expression.
  • Parentheses make grouping explicit, and truth tables check every input combination.

Example: Checking a climate-cabinet reading

A school laboratory accepts a climate-cabinet reading when its temperature is between 18 °C and 26 °C inclusive, the sensor is ready, and maintenance mode is off. The variables are temperature, measured in degrees Celsius, and the Boolean values sensorReady and maintenanceMode.


A compound condition combines simpler Boolean expressions into one Boolean result. Use the language-neutral operators AND, OR, and NOT throughout this example.


a) Write one Boolean expression named valid that represents the acceptance rule.


The lower temperature requirement is temperature >= 18. The upper requirement is temperature <= 26. Both are required, so join them with AND and group the range test.


The sensor requirement is represented directly by sensorReady. Maintenance mode must be off, so its requirement is NOT maintenanceMode. Join all three requirements with AND.


The final expression is valid = (temperature >= 18 AND temperature <= 26) AND sensorReady AND NOT maintenanceMode.


b) Evaluate valid when the temperature is 22 °C, sensorReady is true, and maintenanceMode is false.


First compare the temperature with each endpoint. The result of 22 >= 18 is ________, and the result of 22 <= 26 is ________. The grouped range test therefore becomes true AND true, whose value is ________.


The sensor is ready, so sensorReady is true. Maintenance mode is false, making NOT maintenanceMode equal to ________.


Substitute these results into the complete expression:


valid=true AND true AND true=true\text{valid}=\text{true}\ \text{AND}\ \text{true}\ \text{AND}\ \text{true}=\text{true}


The final result is valid = true, so this reading satisfies every acceptance requirement.


c) A technician replaces the AND between the temperature comparisons with OR. Compare the original and modified expressions at 31 °C when sensorReady is true and maintenanceMode is false.


At 31 °C, the lower-bound comparison 31 >= 18 gives ________, while the upper-bound comparison 31 <= 26 gives ________. The temperature exceeds the permitted maximum.


The original range test is true AND false, which is false. The other requirements remain true, so the original expression becomes false AND true AND true and evaluates to false.


The modified range test is true OR false, which is true. Its complete expression becomes true AND true AND true and evaluates to true.


A range with both a lower and an upper requirement needs both comparisons to hold. The modified expression incorrectly accepts this reading because OR needs only one comparison to be true.


The final results are original: false; modified: true.

A package scanner stores a package's mass in kilograms in the variable mass. Which Boolean expression tests whether the mass is at least 5.0 kg, including exactly 5.0 kg?

Choose an answer

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

A student is reviewing the vocabulary used to describe conditions in a program. Match each term on the left with the definition that describes it exactly.

Match each term
A.

Stopping work on a compound condition as soon as its overall result is already decided

B.

A keyword such as and, or or not that combines or inverts true-or-false values

C.

A symbol such as <= or != that compares two values and produces a true-or-false result

D.

Any expression that evaluates to exactly one of the two values True or False

E.

The rule that negating a conjunction gives the disjunction of the separate negations, and the reverse

F.

The fixed ranking that decides which operation is applied first when brackets are absent

Logical operator

Operator precedence

De Morgan's law

Relational operator

Short-circuit evaluation

Boolean expression

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

A backup fan records two Boolean inputs: mains is true when mains power is available, and battery is true when battery power is available. Complete every output cell with the word true or false, using inclusive OR.

mainsbatterymains AND batterymains OR batteryNOT mains
falsefalse
falsetrue
truefalse
truetrue

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors