/ Learning to code

What Boolean Logic Is & How It’s Used In Programming

What Boolean Logic Is & How It’s Used In Programming

Boolean logic is a key concept in any programming language, whether you’re creating a video game with C++, developing the next best app in Swift, searching through relational databases in SQL, or working with big data in Python. In this article, we’ll cover what Boolean logic is, how it works, and how to build your own Boolean expressions.

What is Boolean logic?

Boolean logic is a type of algebra in which results are calculated as either TRUE or FALSE (known as truth values or truth variables). Instead of using arithmetic operators like addition, subtraction, and multiplication, Boolean logic utilizes three basic logical operators: AND, OR, and NOT.

TRUE and FALSE: There can only be two

Behind Boolean logic are two very simple words: TRUE and FALSE.

Note that a Boolean TRUE or FALSE is very different from typing the strings “True” and “False” into your code. In fact, programming languages put these two Boolean values into their own object type separate from integers, strings, and floating-point numbers. But while there can be practically infinite possibilities for a numerical or string value in programming, there are only two possible Boolean values: TRUE and FALSE.

How does this work? Boolean logic looks at a reported relationship between things and determines whether the relationship holds. For example, let’s take the equation:

2 + 2 = 4

Here, we have two parts, 2 + 2 and 4, and we’re reporting that those two parts are equal to each other. Is that right? Yes it is, so the Boolean result of this would be TRUE. In this example, the combination of the two parts 2 + 2 and 4, together with the relationship (= equals), is called a Boolean expression.

Let’s look at another Boolean expression:

10 – 4 = 5

Here, we’re reporting that 4 subtracted from 10 is the same as 5. Is that right? Of course not, and that’s why this Boolean expression would return a value of FALSE.

Boolean Expression

Result

2 + 2 = 5

FALSE

5 – 1 = 4

TRUE

4 * 5 > 10

TRUE

40 / 10 < 1

FALSE

Building Boolean expressions

Keep in mind that Boolean logic only works when an expression can be TRUE or FALSE. For example, the expression 3 + 8 isn’t a Boolean expression because it’s not being compared or related to something else. But the expression 3 + 8 = 10 is a Boolean expression because we can now evaluate each side and see if the reported relationship between them is TRUE or FALSE (in this case, it’s FALSE).

We can build Boolean expressions with all kinds of data, including variables. For example, let’s suppose that we’ve assigned these values to variables x and y somewhere in our code:

x is 7
y is 3

Now, we can build Boolean expressions using our variables:

Boolean Expression

Result

x + y < 11

FALSE

x * y = 21

TRUE

x / y > 10

FALSE

x + (2 * y) = 13

TRUE

Boolean expressions can also determine whether two strings are identical. Just remember that most programming languages are case-sensitive.

Boolean Expression

Result

“I love Codecademy” = “I love Codecademy”

TRUE

“I love Codecademy” = “Codecademy”

FALSE

“I love Codecademy” = “I LOVE Codecademy”

FALSE

There are many other ways to build Boolean expressions, depending on the programming language. For example, you could use a Boolean expression to determine whether a number is contained within a list in Python or whether a text string is within a SQL database table.

Boolean operators

Now that you understand the basics of Boolean expressions, let’s look at another key aspect of Boolean logic: Boolean operators. There are three basic Boolean operators, AND, OR, and NOT.

To better understand how Boolean operators work, let’s suppose for a moment that we’re in an ice cream shop. Say we’re going to put together a two-scoop sundae with different flavors. But I’m a bit of a picky eater, so I may not accept every sundae combination that I get. We can use Boolean expressions and Boolean operators to figure out whether I’ll eat a sundae or not.

AND

The Boolean AND operator is used to confirm that two or more Boolean expressions are all true.

For example, in my sundae, I want the first flavor to be chocolate and the second flavor to be vanilla. We could turn this into a Boolean expression with an AND operator that looks something like this:

Flavor_1 = Chocolate AND Flavor_2 = Vanilla

This means that both the first flavor must be chocolate and the second flavor must be vanilla. Otherwise, I won’t eat the sundae.

We could organize this situation into a table of possibilities like this:

Flavor_1

Flavor_2

Eat Sundae?

Chocolate

Vanilla

Yes

Chocolate

Strawberry

No

Mango

Vanilla

No

Mango

Strawberry

No

Tables like this are used in Boolean logic all the time. They’re called truth tables, and we can put one together for our sundae example. If something meets my picky sundae flavor conditions, then it’s TRUE. If not, then it’s FALSE.

Here’s how the table above would look like in truth table form:

Flavor_1

Flavor_2

Result

TRUE

TRUE

TRUE

TRUE

FALSE

FALSE

FALSE

TRUE

FALSE

FALSE

FALSE

FALSE

OR

The Boolean OR operator checks that either one condition or another is true.

For example, if I wanted either the first flavor to be strawberry or the second flavor to be mango, then the Boolean expression would be:

Flavor_1 = Strawberry OR Flavor_2 = Mango

We could organize the possibilities as:

Flavor_1

Flavor_2

Eat Sundae?

Strawberry

Mango

Yes

Strawberry

Cherry

Yes

Cherry

Mango

Yes

Vanilla

Raspberry

No

And the truth table would look like this:

Flavor_1

Flavor_2

Result

TRUE

TRUE

TRUE

TRUE

FALSE

TRUE

FALSE

TRUE

TRUE

FALSE

FALSE

FALSE

Note that the OR operator returns TRUE if one of the two Boolean expressions is true, but also when both expressions are true. For this reason, this OR operator is also known as an inclusive OR operator. Alternatively, there’s also the XOR (exclusive or) operator, which only returns TRUE when one of the two expressions is true.

NOT

The Boolean NOT operator is different from AND and OR as it only takes one argument. It tests if a value is FALSE or not. Put another way, it changes TRUE values to FALSE and FALSE values to TRUE.  For example, I hate Rum Raisin and absolutely will not eat anything with Rum Raisin in it. How might that look in table form?

Flavor

Eat Ice Cream?

Chocolate

Yes

Rum Raisin

No

The truth table looks like this:

Flavor

Result

FALSE

TRUE

TRUE

FALSE

You can combine Boolean expressions to express my sundae preference when Rum Raisin is involved like so:

NOT (Flavor_1 = Rum Raisin OR Flavor_2 = Rum Raisin)

Flavor_1

Flavor_2

Eat Sundae?

Chocolate

Raspberry

Yes

Peach

Rum Raisin

No

Rum Raisin

Mint

No

Rum Raisin

Rum Raisin

No

The OR clause within the parentheses will return TRUE for anything with Rum Raisin in it. Applying the NOT to it will change the OR expression’s value from TRUE to FALSE and vice versa. Much like operators in arithmetic, Boolean operators have an order of precedence: elements within a parentheses are addressed first, then the NOT operator, AND, and lastly OR. Our new truth table looks like this:

Flavor_1

Flavor_2

Result

FALSE

FALSE

TRUE

FALSE

TRUE

FALSE

TRUE

FALSE

FALSE

TRUE

TRUE

FALSE

Applying your Boolean logic

So, what’s next after learning the basics of Boolean logic?

Boolean logic is critical to creating code that helps your program quickly make decisions about data and inputs, so try putting your Boolean knowledge to use with an online programming course. And if you’re not sure which course to try next, take a look at our developer career paths. We’ll help you focus on the best skills you need to succeed in your desired role.


Code Foundations Courses & Tutorials | Codecademy
Interested in learning how to code, but unsure where to start? Our Code Foundations domain provides an overview of the main applications of programming and teaches important concepts that you’ll find in every programming language. This content will prepare you to chart a course to a more technical c…

Get more practice, more projects, and more guidance.

Michael Klein

Michael Klein is a freelancer with a love for statistics, data visualization, and his cat. When he’s not writing for Codecademy, he enjoys geostatistics with R and playing with agent-based models.

Read More
What Boolean Logic Is & How It’s Used In Programming
Share this