In this guide I’m going to discuss one of the most fundamental concepts in the world of computer science: boolean logic. Despite its importance, many developers have shelved topics such as boolean logic to the archives of boring math concepts.

However, the concept of boolean logic is used so often in programming, that it’s a mistake to ignore it.

## Boolean Logic Definition

From a programming perspective, boolean logic is:

The concept of breaking complex decisions down into a series of true and false questions.

Essentially every time you utilize an if/else conditional in a program, you are working with boolean logic.

## Origin of Boolean Logic

While software development is a relatively new practice, boolean logic has been around since the 1800’s. It was initially proposed by the renowned mathematician: George Boole. Boole theorized that decisions could be broken down into components, with each decision yielding either a true or false value.

His theory wasn’t used for nearly a century after he died. However boolean logic became the critical component used in circuit design when the world’s first computers were invented. Early computer scientistsĀ realized that electronic circuits could be designed in a manner so that each circuit followed a true/false pattern.

## Binary Code

This early design pattern evolved into the binary code system that all computers are based on today.

In this diagram you can see a basic circuit diagram for the electronic signals that computers use to make our modern computer age possible.

As you can see. This circuit shows a series of and/or/not conditionals that combine to give computers the ability to have dynamic behavior.

## Boolean Logic Math for Developers

The world of mathematics has a bad reputation for being boring. And for offering tedious solutions for simple problems. In fact, when I told people that I was going to write an algorithm course tailored to developers, I had several computer scientists tell me that it wasn’t possible to make the math easy enough to understand for non mathematicians. However I vehemently disagree with this opinion. I believe that computer science math can be understood by anyone, assuming that it’s described properly.

## Truth Tables

With that in mind I am going to describe the mathematical concept of truth tables.

### Basic Example

When it comes to illustrating how truth tables can explain boolean logic, the base case is a scenario with a single value. In this case we have a variable P that is either true or false. This can be translated to code with the following example:

p = true if p == true puts "true" else puts "false" end # > true

Pretty easy, right?

Boolean logic also allows you to find the opposite value for an item. This process is called *logical negation* and is illustrated in this truth table. As you can see, logical negation is simply the process of finding the opposite value of a scenario. For example, when `P`

is `true`

, `not P`

equals `false`

. This is a common programming process and you can see how it looks with the following Ruby code:

p = true if !p == true puts "true" else puts "false" end # > false

Most programming languages use the `!`

to represent logical negation, as shown in the Ruby code example. As you can see in the program, the `!p`

statement flipped the value of `p`

. If read in plain language, the code `if !p == true`

says:

If

`not`

p is equal to true

### Logical Conjunction

Now that you know how to read a basic truth table, let’s start combining values. This illustration adds the variable `Q`

along with a set of true and false scenarios. Additionally there is a third column that contains the statement: `Pā§Q`

. In boolean logic this can be read as:

P AND Q

This statement will look at the value or `P`

and `Q`

and will only result in a final value of `true`

if both the variables are `true`

.

In code this looks like:

p = true q = false if p && q puts "true" else puts "false" end # > false

Most programming languages use the double `&&`

to represent the concept of `and`

. In order for this program to result in a `true`

value, the variable `q`

would have to change to `true`

:

p = true q = true if p && q puts "true" else puts "false" end # > true

This may seem like a very basic example. However you’d be shocked at how many bugs arise in applications by misunderstanding how logical conjunction works.

For a practical example, I once had a time tracking application that was supposed to alert users when they hadn’t logged in to add their time for the previous day. The alerts were only supposed to run Monday through Friday, but not on the weekends. I made the mistake of adding a snippet of code that combined Saturday `AND`

Sunday, which resulted in the alerts never being sent out because no day existed that was both Saturday `AND`

Sunday. This may seem obvious, however a large number of bugs can be attributed to improperly utilizing logical conjunction.

### Logical Disjunction

Now that you’re familiar with the concept of **conjunction** it should be relatively straightforward to understand **logical disjunction**. As you can see from the truth table, when using disjunction, the only time when the end result will be `false`

is when all the items being analyzed are `false`

. You can read a command that utilizes disjunction as:

P OR Q

Disjunction can be utilized in programs to give a flexible interface for conditionals. Let’s take a look at how disjunction can be used in a Ruby program:

p = true q = false if p || q puts "true" else puts "false" end # > true

In order to implement disjunction, most programming languages utilize the double pipe character: `||`

. The key component to be aware of when working with this type of process is that many language will skip over values as soon as a `true`

value has been found. In analyzing our example, when the program saw that the value of `p`

was `true`

, it didn’t even check the value of `q`

. Why’s this a big deal? Mainly because it can lead to some confusing bugs down the road. Let’s imagine that you entered a typo into a program, such as the program below:

p = true if p || x puts "true" else puts "false" end # > true

There’s a pretty big problem with this program. Do you notice how I included the variable `x`

as the second conditional? In this particular program the variable `x`

was never created, so you might think that the code should throw an error. However, because the value of `p`

was `true`

, Ruby completed skipped over the typo. This can cause confusing bugs because watch what happens if we change the value of `p`

to `false`

:

p = false if p || x puts "true" else puts "false" end # > NameError: undefined local variable or method `x' for main:Object from (irb):11 from /Users/admin/.rvm/rubies/ruby-2.3.0/bin/irb:11:in `<main>'

Because of this behavior, it’s important to always use caution when working with logical disjunction.

### Logical Implication

I saved the most confusing item related to boolean logic for last. In looking at the truth table you’ll see a rather confusing trend. The only time when the result is `false`

is when `P`

is `true`

and `Q`

is `false`

. The concept of **logical implication** can be tricky to understand for new computer science students. However I’ve discovered that analyzing a real world scenario is the easiest way to understand the process.

Let’s assign our variables to example values, we’ll imagine:

`p`

= I’m in the pool

And:

`q`

= My body is wet

So with these situations in mind we can use them to ask questions related to each line item in the truth table, such as:

If

`p`

and`q`

are both`true`

, then we can say:If I’m in the pool then my body is wetwhich we know is`true`

.

So far, so good? Now let’s move down to the next line in the truth table:

If

`p`

is`true`

and`q`

is`false`

, then we can say:If I’m in the pool then my body is not wetwhich we know is`false`

.

This makes sense because we know we can’t be in the pool and not get wet. Moving down the table:

If

`p`

is`false`

and`q`

is`true`

, then we can say:If I’m not in the pool then my body is wetwhich we know from a logical perspective can be`true`

.

Imagine a real world scenario, your body could be wet by walking in the rain or swimming in the ocean. Moving to the final truth table line item:

If

`p`

is`false`

and`q`

is`false`

, then we can say:If I’m not in the pool then my body is not wetwhich we know is`true`

.

## Summary

In this guide we discussed the algorithm processes associated with boolean logic. The key to remember with boolean logic is that it sets the foundation for computer science and algorithm development. At the end of the day, all algorithms can be boiled down to a series of `true`

and `false`

decisions. And this concept is important to remember, especially when you start working with complex algorithms. Because the most important key to understanding challenging concepts is being able to simplify them into small, easy to manage components. And thankfully there aren’t too many decisions easier than deciding if a value is true or false.