Boolean expression (logic and programming)
A Boolean expression is a formula that evaluates to true or false. Used in logic, mathematics, programming, and digital circuits, it combines variables and operators to make decisions and describe conditions.
Overview
A Boolean expression is a syntactic construct that yields one of two truth values: true or false. In computer science and formal logic the term refers to formulas built from Boolean variables, constants and operators. In practical programming contexts—see programming languages—such expressions are evaluated at runtime to control branching, looping and other decision-making behavior.
Core components
Typical elements of a Boolean expression include:
- variables or predicates (for example, X > 3 or isEmpty(list)),
- logical operators such as AND, OR and NOT (often written &&, ||, !),
- constants true and false, and
- parentheses to group subexpressions.
Compound expressions combine these parts; for example, (X > 3) && (Y <= 10) produces true only when both subconditions hold.
Behavior and evaluation
Evaluation rules determine how an expression reduces to a truth value. Many languages implement short-circuiting: in A || B, B is not evaluated if A is already true; in A && B, B is not evaluated if A is false. Some operators (bitwise & and |) and custom predicates may evaluate all operands and can produce side effects. Boolean expressions can be transformed using algebraic laws—commutativity, associativity, distributivity—and De Morgan's rules are commonly used to simplify or invert expressions.
History and theory
The mathematical framework for Boolean expressions traces back to Boolean algebra, developed in the 19th century as a way to formalize logical reasoning. Over time these ideas became foundational for digital circuit design and for the semantics of conditional computation in programming languages.
Uses and examples
Boolean expressions appear in conditionals (if, while), assertions, filter queries and search engines, and as logic gates in hardware. Common examples include X > Y, !(flag), and (A && (B || !C)). A simple truth table for A AND B can be written as a list: A=true, B=true → true; A=true, B=false → false; A=false, B=true → false; A=false, B=false → false.
Notable distinctions
It is useful to distinguish between a Boolean expression (an evaluable formula) and a Boolean data type (the storage type representing true/false). Another distinction is between logical operators (which operate on truth values) and bitwise operators (which operate on binary representations). Normal forms—conjunctive (CNF) and disjunctive (DNF)—are standard ways to rewrite expressions for analysis or automated reasoning.
Understanding Boolean expressions is essential for writing correct control flow, optimizing queries, and designing reliable hardware and software systems.
Related articles
Author
AlegsaOnline.com Boolean expression (logic and programming) Leandro Alegsa
URL: https://en.alegsaonline.com/art/13014