Boolean expression

In computer science, a Boolean expression is an expression used in programming languages that produces either true or false when evaluated.[1] Boolean expressions are often used by conditionals in computer programs to decide which code to run.

Most Boolean expressions will contain at least one variable (<syntaxhighlight lang="text" enclose="none">X > 3</syntaxhighlight>), and often more (<syntaxhighlight lang="text" enclose="none">X > Y</syntaxhighlight>) so that the expression can be useful for various values of X and Y.

Boolean operators

Boolean operators may be represented by words such as OR, AND and NOT.

Some programming languages use symbols such as "||" (double pipe character) for OR, "&&" (double ampersand) for AND, and "!" for NOT. Other programming languages may use other symbols.

Other Boolean operators may be available, too, for example XOR (exclusive OR).

Boolean expressions can also be represented as Logic gates in electronic circuit diagrams.

See Truth tables for a summary of the effects of common Boolean operators.

Examples

  • The expression <syntaxhighlight lang="text" enclose="none">5 > 3</syntaxhighlight> evaluates as true.
  • The expression <syntaxhighlight lang="text" enclose="none">3 > 5</syntaxhighlight> evaluates as false.
  • The expression <syntaxhighlight lang="text" enclose="none">(X > 3) AND (X < 5)</syntaxhighlight> evaluates as true for any value of X between 3 and 5.
  • The expression <syntaxhighlight lang="text" enclose="none">(X <= 3) OR (X >= 5)</syntaxhighlight> evaluates as true for any value of X which less than or equal to 3 or is greater than or equal to 5, in other words any value of X which is not between 3 and 5.
  • The English language expression "any value of X which is not between 3 and 5" can be represented as <syntaxhighlight lang="text" enclose="none">NOT ((X > 3) AND (X < 5))</syntaxhighlight>.

An exercise for the reader

  • Try evaluating <syntaxhighlight lang="text" enclose="none">NOT ((X > 3) AND (X < 5))</syntaxhighlight> using X=4 and again using X=6.
    • For X=6, it is true that X is not between 3 and 5.
    • For X=4, it is false that X is not between 3 and 5 because 4 is between 3 and 5.

References

  1. Gries, David; Schneider, Fred B. (1993), "Chapter 2. Boolean Expressions", A Logical Approach to Discrete Math, Monographs in Computer Science, Springer, p. 25ff, ISBN 9780387941158.

Other websites

  • The Calculus of Logic, by George Boole, Cambridge and Dublin Mathematical Journal Vol. III (1848), pp. 183–98.