Boolean expressions

Published

2023-08-01

Boolean logic and Boolean expressions

Boolean expressions and Boolean logic are widely used in mathematics, computer science, computer programming, and philosophy. These take their name from the 19th century mathematician and logician George Boole. His motivation was to systematize and formalize the logic that philosophers and others used, which had come down to us from ancient Greece, primarily due to Aristotle.

The fundamental idea is really quite simple: we have truth values—true or false—and rules for simplifying compound expressions.

It’s easiest to explain by example. We’ll start with informal presentation, and then formalize things a little later.

Say we have this sentence “It is raining.” Now, either it is, or it is not raining. If it is raining, we say that this sentence is true. If it is not raining, we say that this sentence is false.

We call a sentence like this a proposition.

Notice that there is no middle ground here. From our perspective, a proposition like this is either true or false. It can’t be 67% true and 33% false, for example. We call this the law of the excluded middle.

Another way of stating this law is that either a proposition is true, or its negation is true. That is to say, either “It is raining” is true or its negation, “It is not raining” (or “It is not the case that it is raining”) is true. One or the other.1

What does this mean for us as computer programmers? Sometimes we want our code to do one thing if a certain condition is true, and do something different if that condition is false (not true). Without this ability, our programs would be very inflexible.

But before we get to coding, let’s learn a little more about Boolean expressions.

Boolean expressions

What is a Boolean expression? Well, true and false are both Boolean expressions. We can build more complex expressions using the Boolean connectives not, and, and or.

We often use truth tables to demonstrate. Here’s the simplest possible truth table. We usually abbreviate true and false as T and F, respectively, but here we’ll stick with the Python Boolean literals True and False.

Expression Truth value
True True
False False

True is true, and false is false (big surprise, I know).

Now let’s go crazy and mix it up. We’ll begin with the Boolean connective not. not simply negates the value or expression which follows.

Expression Truth value
True True
False False
not True False
not False True

Now let’s see what happens with the other connectives, and and or. Some languages have special symbols for these (for example, Java uses && and || for and and or respectively). In Python, we simply use and and or. When we use these connectives, we refer to the expressions being connected as clauses.

Expression Truth value
True and True True
True and False False
False and True False
False and False False

So when using the conjunctive and, the expression is true if and only if both clauses are true. In all other cases (above) the expression is false.

Here’s or.

Expression Truth value
True or True True
True or False True
False or True True
False or False False

You see, in the case of or, as long as one clause is true, the entire expression is true. It is only when both clauses are false that the entire expression is false.

We refer to clauses joined by and as a conjunction. We refer to clauses joined by or as a disjunction. Let’s try this out in the Python shell:

>>> True
True
>>> False
False
>>> not True
False
>>> not False
True
>>> True and True
True
>>> True and False
False
>>> False and True
False
>>> False and False
False
>>> True or True
True
>>> True or False
True
>>> False or True
True
>>> False or False
False

Now, we don’t usually use literals like this in our code. Usually, we want to test some condition to see if it evaluates to True or False. Then our program does one thing if the condition is true and a different thing if the condition is false. We’ll see how this works in another section.

De Morgan’s Laws

When working with Boolean expressions De Morgan’s Laws provide us with handy rules for transforming Boolean expressions from one form to another. Here we’ll use a and b to stand in for arbitrary Boolean expressions.

not (a or b) is the same as (not a) and (not b)
not (a and b) is the same as (not a) or (not b)

You can think of this as a kind of distributive law for negation. We distribute the not over the disjunction (a or b), but when we do, we change the or to and. By the same token, we distribute not over the conjunction (a and b), but when we do, we change the and to or.

You’ll see these may come in handy when we get to input validation (among other applications).

Supplemental information

If you’d like to explore this further, here are some good resources:

Original author: Clayton Cafiero < [given name] DOT [surname] AT uvm DOT edu >

No generative AI was used in producing this material. This was written the old-fashioned way.

This material is for free use under either the GNU Free Documentation License or the Creative Commons Attribution-ShareAlike 3.0 United States License (take your pick).

Footnotes

  1. There are some logicians who reject the law of the excluded middle. Consider this proposition called the liar paradox or Epimenides paradox: “This statement is false.” Is this true or false? If it’s true it’s false, if it’s false it’s true! Some take this as an example of where the law of the excluded middle fails. Thankfully, we don’t need to worry about this in this textbook, but if you’re curious, see: Law of the excluded middle (Wikipedia). There’s even an episode in the original Star Trek television series, in which Captain Kirk and Harry Mudd defeat a humanoid robot by confronting it with the liar’s paradox. You can view it on YouTube: https://www.youtube.com/watch?v=QqCiw0wD44U.↩︎