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

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.

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

FAQs

What is Boolean logic and how is it used in programming? ›

In computing, the term Boolean means a result that can only have one of two possible values: true or false. Boolean logic takes two statements or expressions and applies a logical operator to generate a Boolean value that can be either true or false.

How can Booleans be used to make decisions in programming? ›

Computer programs also make decisions, using Boolean expressions (true/false) inside conditionals (if/else). Thanks to conditionals, programs can respond differently based on different inputs and parameters.

What is a Boolean expression in programming? ›

What is the meaning of Boolean expression? Boolean expressions are the expressions that evaluate a condition and result in a Boolean value i.e true or false. Ex: (a>b && a> c) is a Boolean expression. It evaluates the condition by comparing if 'a' is greater than 'b' and also if 'a' is greater than 'c'.

What does Boolean logic use ________________ in searching? ›

"What is a Boolean Operator?" Boolean Operators are simple words (AND, OR, NOT or AND NOT) used as conjunctions to combine or exclude keywords in a search, resulting in more focused and productive results.

Why is Boolean logic important in programming? ›

Boolean logic provides a means to display the operations on input signals and produce a result in mathematical terms using AND, NAND, OR, NOR, EX-OR, EX-NOR, and NOT logical operations. Truth tables provide a means to display the operations on input signals and produce a result in table format.

What programming language uses Boolean? ›

Discussion
LanguageReserved WordTrue
C++booltrue
C#bool or Booleantrue
Javabooltrue
JavaScriptBoolean()true
2 more rows

How do you use Boolean logic? ›

Boolean logic defines logical relationships between terms in a search. The Boolean search operators are and, or and not. You can use these operators to create a very broad or very narrow search. And combines search terms so that each search result contains all of the terms.

How do you use the Boolean method? ›

The boolean method converts the value of object1 to Boolean, and returns true or false. The exists method checks whether a value is present in object1. If a value is present, it returns Boolean true; otherwise, it returns Boolean false. The false method always returns Boolean false.

What is a boolean data type example? ›

A boolean is a data type with two possible values: true (1) or false (0). The two values help represent truth conditions found in logic control structures. The name comes from a branch of mathematics called Boolean algebra, named after George Bool.

What is Boolean logic for dummies? ›

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.

What is Boolean explained easy? ›

Boolean logic close Boolean logicA form of logical algebra which works only with two values, true or false. is a form of algebra where all values. are either True or False. These values of true and false are used to test the conditions. A computation depends on whether a condition equates to true or false.

Is Boolean a programming language? ›

Boolean Algebra laid the foundation for the information age and computer science. All computers function using the basic principles of Boolean Algebra, where 1, or true, is on, and 0, or false, is off. Because of this, many programming languages include boolean data types and operators.

What are the three Boolean searches? ›

Boolean operators are the words "AND", "OR" and "NOT". When used in library databases (typed between your keywords) they can make each search more precise - and save you time!

What is an example of a Boolean value? ›

A Boolean variable has only two possible values: true or false. It is common to use Booleans with control statements to determine the flow of a program. In this example, when the boolean value "x" is true, vertical black lines are drawn and when the boolean value "x" is false, horizontal gray lines are drawn.

What is Boolean in Python? ›

In general, a Boolean variable can have only two values - True or False. Or in other words, if a variable can have only these two values, we say that it's a Boolean variable. It's often used to represent the Truth value of any given expression. Numerically, True is equal to 1 and False is equal to 0.

What is Boolean logic examples? ›

For example, suppose the pet retailer is trying to build more business in Canada. To find this audience, they can use the Boolean construction “(cat owners OR dog owners OR bird owners) AND Canada.” This search would yield anyone who owns cats, dogs or birds and lives in Canada.

How do you use Boolean? ›

The Boolean search operators are and, or and not. You can use these operators to create a very broad or very narrow search. And combines search terms so that each search result contains all of the terms. For example, travel and Europe finds articles that contain both travel and Europe.

Top Articles
Latest Posts
Article information

Author: Foster Heidenreich CPA

Last Updated:

Views: 6642

Rating: 4.6 / 5 (56 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Foster Heidenreich CPA

Birthday: 1995-01-14

Address: 55021 Usha Garden, North Larisa, DE 19209

Phone: +6812240846623

Job: Corporate Healthcare Strategist

Hobby: Singing, Listening to music, Rafting, LARPing, Gardening, Quilting, Rappelling

Introduction: My name is Foster Heidenreich CPA, I am a delightful, quaint, glorious, quaint, faithful, enchanting, fine person who loves writing and wants to share my knowledge and understanding with you.